When i add contentscript using page-mod in firefox extension. The content script functions are executed thrice

守給你的承諾、 提交于 2020-01-05 12:11:42

问题


I am unable to understand . Why my functions are executed thrice after addition in the firefox extension using "page-mod" .

This is my code : [main.js]

var pageModOptions = {
    include: ["http://xyz.com/*"],
    attachTo: ["top", "frame", "existing"],
    contentScriptWhen: "end",
    onAttach: function(worker){
        worker.port.on("Done", function(elementContent) {
            console.log("emitted by tarun :" + elementContent);
            worker.port.emit("start", htmlfilePath);
        });
    },
};

pageModOptions.contentScriptFile = [data.url("js/main.js"),data.url("js/jquery-1.8.2.min.js"),data.url("js/hello.js")];

//pageModOptions.contentScriptFile = data.url("js/jquery-1.8.2.min.js");
pageModOptions.contentStyleFile = data.url("js/main.css");
pageModOptions.contentScriptOptions = csOptions;
pageMod.PageMod(pageModOptions); 

and in the contentscript [hello.js]

function test(){
    window.alert('testing phase 1');
}
test();

So this alert is called thrice . How to stop this behaviour .


回答1:


Your content script will run exactly once for each HTML document loaded from http://xyz.com/ - a separate instance of your content script will be injected into each of them. So seeing three alerts when this code runs can mean the following things:

  • There are three browser tabs open with pages from http://xyz.com/ loaded.
  • There is only one browser tab but the page from http://xyz.com/ loaded there includes two frames which are also loaded from http://xyz.com/.

Most likely, if you change window.alert('testing phase 1') into window.alert(location.href) your confusion will be cleared up.



来源:https://stackoverflow.com/questions/19569298/when-i-add-contentscript-using-page-mod-in-firefox-extension-the-content-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!