本文介绍了当我在firefox扩展中使用page-mod添加内容。内容脚本功能被执行三次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

所以这个警报叫做三次。如何阻止这种行为。 您的内容脚本只能为每个加载自 http://xyz.com/ - 你的内容脚本的一个单独的实例将被注入到它们中的每一个中。因此,在运行此代码时看到三个警报可能意味着以下几点:


  • 有三个浏览器选项卡打开,其中 http://xyz.com/ 已加载。

  • 只有一个浏览器标签,但是 http:// xyz .com / 载入的内容包含两个框架,这些框架也是从 http://xyz.com/ 加载的。

  • 如果你把 window.alert('testing phase 1')改成 window.alert(location.href)你的困惑将被清除。


    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 .

    解决方案

    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.

    这篇关于当我在firefox扩展中使用page-mod添加内容。内容脚本功能被执行三次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 04:37