打开一个带有 "hello world" 的新标签页

Open a new tab with "hello world" on it

如何打开新标签页,并在其中新建HTML文档?最好使用需要重新启动的旧 API,例如 Components.classesComponents.interfaces 之类的东西,但任何可行的方法都可以。

在我的一个附加组件中,我使用以下代码在选项卡或 window 中打开 URL:

/**
 * Open a URL in a window or a tab.
 */
 function openUrlInWindowOrTab(url, titleText, inWindow, makeTabActive) {
    // Default: in tab; tab not activated
    if(typeof (url) !== "string" ) {
        return;
    }//else
    //  Add/remove a "/" to comment/un-comment the code appropriate for your add-on type.
    /* Add-on SDK:
    let activeWindow = require('sdk/window/utils').getMostRecentBrowserWindow();
    //*/
    //* Overlay and bootstrap (from almost any context/scope):
    Components.utils.import("resource://gre/modules/Services.jsm"); //Services
    let activeWindow = Services.wm.getMostRecentWindow("navigator:browser");        
    //*/
    let gBrowser = activeWindow.gBrowser;

    if(inWindow) {
        // Set default title
        titleText = (typeof titleText === "string") ? titleText : "Opened by [Your add-on]";
        //Open a window
        return activeWindow.open(url, titleText);
    } else {
        //Open a tab
        let newTab = gBrowser.addTab(url);
        if(makeTabActive) {
            //Make the tab active
            gBrowser.selectedTab = newTab;
        }
        return newTab;
    }
}

以上应该适用于 Overlay 和 Bootstrapped 附加组件。它也可以在附加 SDK 中工作,方法是取消注释附加 SDK 的代码并注释掉 Overlay/bootstrapped 代码(获得 activeWindow 的行)。但是,对于 Add-on SDK,最好使用 SDK 特定的 API。

如果您希望它在新标签页中显示 "Hello World",请在您的 chrome/content 目录中提供一个 HTML 文件并为其使用适当的 URL(例如 chrome://[as defined in your chrome.manifest]/content/helloWorld.html),在 content line in your chrome.manifest.

中为您的附加组件定义