Firefox Compile Simple Load Script - browser.xul

时间:2015-09-29 00:46:45

标签: firefox firefox-addon onload

I'm trying to add a script tag to every dom page through privileged chrome, so far i'm able to get the first pageload of a tab, but after that, the script does nothing, I'm using Firefox Nightly 44.0. What am i doing wrong???

Documents I'm following:

https://developer.mozilla.org/en-US/Add-ons/Code_snippets/On_page_load https://developer.mozilla.org/en-US/Add-ons/Overlay_Extensions/XUL_School/Intercepting_Page_Loads

mozilla-central/browser/base/content/browser.xul (line: 74)

<script type="application/x-javascript" src="chrome://browser/content/yyy/x.js" />

chrome://browser/content/yyy/x.js

var myExtension = {
    init: function() {
        // The event can be DOMContentLoaded, pageshow, pagehide, load or unload.
        if(gBrowser) gBrowser.addEventListener("DOMContentLoaded", this.onPageLoad, false);
    },
    onPageLoad: function(aEvent) {
        var doc = aEvent.originalTarget; // doc is document that triggered the event
        var win = doc.defaultView; // win is the window for the doc
        // test desired conditions and do something
        // if (doc.nodeName != "#document") return; // only documents
        // if (win != win.top) return; //only top window.
        // if (win.frameElement) return; // skip iframes/frames
        alert("page is loaded \n" +doc.location.href);
    }
}
window.addEventListener("load", function load(event){
    window.removeEventListener("load", load, false); //remove listener, no longer needed
    myExtension.init();  
},false);

mozilla-central/browser/base/jar.mn

content/browser/yyy/x.js            (content/yyy/x.js)

1 个答案:

答案 0 :(得分:1)

您希望使用loadFrameScript参数为true来收听未来的网页。以下是示例:https://github.com/mdn/e10s-example-addons/tree/master/run-script-in-all-pages

globalMM.loadFrameScript("chrome://modify-all-pages/content/frame-script.js", true);

此处记录了这些内容:https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XPCOM/Reference/Interface/nsIFrameScriptLoader#loadFrameScript%28%29

要阻止它在新标签中加载,您必须使用removeDelayedFrameScript

github链接还显示了如何使用addon-sdk内容脚本执行此操作的示例。