2016-11-22 77 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