1

我无法理解。为什么我的函数在使用 "page-mod" 添加到 firefox 扩展后执行三次。

这是我的代码:[ 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); 

并在内容脚本 [ hello.js ]

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

所以这个警报被称为三次。如何制止这种行为。

4

1 回答 1

2

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.

于 2013-10-27T08:53:50.780 回答