How to listen to page downloads from the Fennec extension?

I am working on a simple extension for Fennec that should add a special HTML element to every loaded page. I 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) { 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 the warning is displayed only once when the browser starts, I expect it to be displayed on every loaded page. What am I doing wrong?

Fennec Version: 4.0b5 (tested on the desktop version for Windows)

Thanks!

+4
source share
1 answer

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

You must:

  • load the script with each new tab:

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

  • Inside content.js, listening for the DOMContentLoaded event:

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

See the following pages for more information:

+4
source

Source: https://habr.com/ru/post/1342112/


All Articles