简体   繁体   English

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

[英]Firefox Compile Simple Load Script - browser.xul

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.我正在尝试通过特权 chrome 向每个 dom 页面添加一个脚本标记,到目前为止我能够获得选项卡的第一个页面加载,但是在那之后,脚本什么都不做,我使用的是 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 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) 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 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 mozilla-central/browser/base/jar.mn

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

You'll want to use loadFrameScript with argument of true to listen to future pages.您将需要使用参数为 true 的loadFrameScript来收听未来的页面。 Here are examples: https://github.com/mdn/e10s-example-addons/tree/master/run-script-in-all-pages以下是示例: 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);

This is documented here: https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XPCOM/Reference/Interface/nsIFrameScriptLoader#loadFrameScript%28%29这在此处记录: https : //developer.mozilla.org/en-US/docs/Mozilla/Tech/XPCOM/Reference/Interface/nsIFrameScriptLoader#loadFrameScript%28%29

To stop it from loading in new tabs, then you have to use removeDelayedFrameScript要阻止它在新选项卡中加载,则必须使用removeDelayedFrameScript

That github link also shows an example of how to do it with addon-sdk content-scripts.该 github 链接还显示了如何使用 addon-sdk content-scripts 执行此操作的示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM