Can someone please tell me how to write a Firefox extension that displays a local HTML page (contai开发者_运维知识库ned in the extension) when a user opens a new tab or web page.
You will need this function to read a file inside your addon
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;
}
Then create an element and add the content of the 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);
});
Use e.originalTarget.defaultView.location.href
to filter the pages you want this behavior.
You may find some info at https://developer.mozilla.org/en/Observer_Notifications and https://developer.mozilla.org/en/Code_snippets/Progress_Listeners and https://developer.mozilla.org/En/Code_snippets/On_page_load .
To display HTML content inside XUL content (though you can also cause a pure XHTML dialog to open), you can use a XUL iframe
If you override the BrowserOpenTab
function you can make it open the page of your choice rather than about:blank
. This could be a page provided by your extension; ideally you would implement an alternative about URI for it.
精彩评论