开发者

How to listen to page loads from Fennec extension?

I'm working on a simple extension for Fennec, which must add special HTML element to every loaded page. I've created this simple overlay.js:

var MyAddon = {
    onLoad: function(aEvent){
        var appcontent = document.getElementById("appcontent"); // Firefox
        if (!appcontent) {
            appcontent = document.getElementById("browsers"); // Fennec
        }
        if (appcontent) {
 开发者_如何学JAVA           appcontent.addEventListener("DOMContentLoaded", MyAddon.onDocumentLoad, true);
        }
    },

    onUnLoad: function(aEvent){
        var appcontent = document.getElementById("appcontent"); // Firefox
        if (!appcontent) {
            appcontent = document.getElementById("browsers"); // Fennec
        }
        if (appcontent) {
            appcontent.removeEventListener("DOMContentLoaded", MyAddon.onDocumentLoad, true);
        }
    },

    onUIReady: function(aEvent){
    },

    onUIReadyDelayed: function(aEvent) {
    },

    onDocumentLoad: function(aEvent) {
    alert("OK");
    }
};

window.addEventListener("load", MyAddon.onLoad, false);
window.addEventListener("unload", MyAddon.onUnLoad, false);
window.addEventListener("UIReady", MyAddon.onUIReady, false);
window.addEventListener("UIReadyDelayed", MyAddon.onUIReadyDelayed, false);

The problem is that alert is shown only one time when browser is started, I'd expect it to show on every page that loads. What am I doing wrong?

Fennec version: 4.0b5 (testing on Desktop version for Windows)

Thanks!


Unfortunately, this is more complex for Fennec. There is no such event as "DOMContentLoaded" coming from the content document. This is because the main window, where your overlay attached the Javascript, lives in a different process than the child windows (content windows)

You have to:

  1. load a script with each new tab:

    browser.messageManager.loadFrameScript("chrome://my_add_on/content/content.js", true);

  2. Inside content.js, listen to the event DOMContentLoaded:

    addEventListener("DOMContentLoaded", process, true); function process(event) { ... }

For more info, check this pages:

  • https://wiki.mozilla.org/Mobile/Fennec/Extensions/Electrolysis
  • https://hg.mozilla.org/mobile-browser/file/8167d57cab8e/chrome/content/browser.js#l1327
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜