当我在火狐扩展中使用页面模组添加内容脚本时.内容脚本函数执行三次

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

本文关键字:脚本 函数 执行 三次 添加 扩展 火狐      更新时间:2023-09-26

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

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

并在内容脚本中 [你好.js]

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

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

对于

http://xyz.com/加载的每个 HTML 文档,您的内容脚本将只运行一次 - 内容脚本的单独实例将注入到每个文档中。因此,在此代码运行时看到三个警报可能意味着以下事项:

  • 打开了三个浏览器选项卡,其中加载了http://xyz.com/页面。
  • 只有一个浏览器选项卡,但加载http://xyz.com/页面包括两个框架,这些框架也是从http://xyz.com/加载的。

最有可能的是,如果您将window.alert('testing phase 1')更改为window.alert(location.href),您的困惑将被清除。