2011-05-02 23 views
1

有人可以告訴我如何編寫Firefox擴展程序,當用戶打開新選項卡或網頁時顯示本地HTML頁面(包含在擴展名中)。使用Firefox擴展程序顯示頁面

+0

您想在哪裏顯示此頁面? – Neil 2011-05-02 22:50:02

+0

@Neil在瀏覽器的空白頁面上(當用戶打開一個新標籤頁時)。 – Bruno 2011-05-03 12:06:23

回答

1

您將需要此功能來讀取文件的插件內

function Read(file) 
{ 
    var ioService=Components.classes["@mozilla.org/network/io-service;1"] 
     .getService(Components.interfaces.nsIIOService); 
    var scriptableStream=Components 
     .classes["@mozilla.org/scriptableinputstream;1"] 
     .getService(Components.interfaces.nsIScriptableInputStream); 

    var channel=ioService.newChannel(file,null,null); 
    var input=channel.open(); 
    scriptableStream.init(input); 
    var str=scriptableStream.read(input.available()); 
    scriptableStream.close(); 
    input.close(); 
    return str; 
} 

然後創建一個元素,並添加HTML內容。

gBrowser.addEventListener("DOMContentLoaded", function(e) { 
    var documentElement = e.originalTarget.defaultView.document; 
    var div = documentElement.createElement("div"); 
    div.innerHTML = Read("chrome://extensioname/content/file.html"); 
    documentElement.body.appendChild(div); 
}); 

使用e.originalTarget.defaultView.location.href篩選您想要的行爲。

+0

感謝您的回答,但我真的很新的Firefox附加組件,並且整天閱讀他們的教程,我不知道應該在Chrome清單,install.rdf等中放置什麼內容。請您發佈整個代碼或發送給我我的電子郵件代碼([email protected]) – Bruno 2011-05-02 14:58:11

+0

@布魯諾我現在不能這樣做。嘗試查看https://developer.mozilla.org/en/building_an_extension – BrunoLM 2011-05-02 14:59:44

+0

上提供的'Hello World'擴展http://mozilla.doslash.org/stuff/helloworld.zip我們在這裏提供幫助,不要免費勞動 – 2011-05-02 16:05:18

0

如果忽略BrowserOpenTab功能,你可以把它打開你所選擇的頁面而不是about:blank。這可能是您的擴展程序提供的頁面;理想情況下,你會爲​​它實現一個關於URI的替代方案。