2016-11-22 79 views
1

如何打开新选项卡并在其中创建一个新的HTML文档?最好使用旧的重新启动所需的API,例如Components.classes,Components.interfaces东西,但任何工作方式都很好。用“hello world”打开一个新选项卡

+0

什么工作*对你*将取决于你正在写的附加组件类型。你所说的意味着你正在编写一个Overlay/XUL附加组件。那是对的吗?如果是这样,你应该认真**考虑编写一个WebExtension或基于附加SDK的插件。请参阅[文档中的Firefox插件简介](http://stackoverflow.com/documentation/firefox-addon/3235/introduction-to-firefox-add-ons/13574/introduction#t=201609290133319078047)。 – Makyen

+0

是的,我有Overlay/XUL插件。我考虑过移动它们,但没有足够的时间这样做。为了迁移,我需要知道的事情是(1)是否可以通过新扩展中的浏览器控制台设置全局变量/访问扩展状态?和(2)在更新的API中是否还有nsIStreamListener/TracingListener的替代方案? – Vibok

+0

在Add-on SDK中,您可以在JavaScript中执行几乎任何操作,您可以在Overlay或Bootstrap插件(显然不是* chrome.manifest *'overlay's)中执行任何操作。附加SDK是一个带有包装的引导加载项。当你想要做的事情不能被附加SDK SDK直接支持时,你可以跳出包装。 – Makyen

回答

0

在我的附加组件之一,我用下面的代码在任何一个标签或窗口中打开一个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; 
    } 
} 

以上应覆盖工作,并自举加载项。它也可以在附加SDK中工作,取消注释附加组件SDK的代码并注释覆盖/自举代码(获得activeWindow的行)。但是,对于附加SDK,最好使用特定于SDK的API。

如果你想让它显示“Hello World”的新选项卡,然后在你的chrome/content目录提供一个HTML文件,并使用适当的URL为它(如chrome://[as defined in your chrome.manifest]/content/helloWorld.html),为您的附加在content line定义在你的chrome.manifest