Firefox 编译简单加载脚本 - browser.xul

Firefox Compile Simple Load Script - browser.xul

我正在尝试通过特权 chrome 向每个 dom 页面添加脚本标记,到目前为止,我能够获得选项卡的第一个页面加载,但在那之后,脚本什么都不做,我正在使用 Firefox Nightly 44.0.我做错了什么???

我正在关注的文档:

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(行: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)

您需要使用 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 link 还展示了如何使用 addon-sdk 内容脚本的示例。