为什么此内容脚本在firefox加载项中多次运行? [英] Why this contentscript runs various times in a firefox add-on?

查看:76
本文介绍了为什么此内容脚本在firefox加载项中多次运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习如何创建Firefox插件。我想做一个简单的插件,将脚本插入页面。我已经阅读了文档,但无法解决此问题。

I am learning how to create a Firefox add-on. I want to do a simple add-on that will inject a script in a page. I have read the documentation but I can't solve this problem.

在cfx运行日志中,我看到它在同一页面中多次运行脚本,当它应该只做一次。

in the cfx run logs, I can see that it runs the script various times in the same page when it should do it only once.

main.js

 
var pageMod = require('sdk/page-mod')
var data = require('sdk/self').data

pageMod.PageMod({
  include:  ['*'],
  contentScriptWhen: 'end',
  contentScriptFile: data.url('starter.js')
})
 

starter.js

starter.js

 
var script = document.createElement('script');

script.id = 'i-inject';
script.type = 'text/javascript';
script.src = 'http://localhost:8888/injectme.js';
document.getElementsByTagName('head')[0].appendChild(script);
console.log('injected');
 

您是否看到此console.log(注入)?在执行cfx和每次重新加载页面时,我都可以看到它在控制台中打印了5次。我不明白这种行为。

Do you see this console.log('injected') ? I can see that it's printed 5 times in the console when I do cfx run and each time I reload the page. I don't understand that behaviour.

任何帮助都非常感谢:)

Any help greatly appreciated :)

推荐答案

我刚刚问完一个问题,由于某种原因,直到现在,多次搜索才使我想到这个问题。

I just finished asking the same question, for some reason multiple searches didn't lead me to this question until now.

您发布的有关iFrames Lead的链接我对这个解决方案似乎很有效。

The link you posted concerning iFrames lead me to this solution, which seems to be working well.

在Firefox中,iFrame仍然是Pages,默认情况下,SDK不会区分iFrame和首页,从而导致内容脚本附加到任何加载的页面(或iFrame)上),满足您的pageMod匹配要求。除了使用page-mod之外,还可以使用制表符来注入所需的脚本和数据。

In Firefox, iFrames are still Pages, and by default the SDK doesn't discriminate between iFrames and top Pages, causing content scripts to attach to ANY loaded page ( or iFrame ) that meets your pageMod 'match' requirements. Rather than use page-mod, you can use tabs to inject the scripts and data you need.

var data = require("sdk/self").data;
var tabs = require('sdk/tabs');

tabs.on('ready', function(tab) {
     var worker = tab.attach({
          contentScriptOptions: {},
          contentScriptFile: [data.url('myScript.js')]
     });

     // now set up your hooks:        
     worker.port.on('someKey', function(data) {
          //do whatever with your data
     });

});

或者,您可以将PageMod的行为修改为仅应用于首页,这将阻止它

Alternatively, you can modify the behavior of PageMod to only apply to the top page, which will prevent it from running in any iFrames on the page.

var data = require("sdk/self").data;
var pageMod = require('sdk/page-mod');

pageMod.PageMod({
    match: [*],
    attachTo: ["top"],
    contentScriptOptions: {},
    contentScriptFile: [data.url('myScript.js')],
    onAttach: function(worker) {
        //function body
    }
});

有关如何进一步控制页面模式行为的更多详细信息,请参见其 API文档

More details on how to further control page-mod behavior can be found in their API docs

这篇关于为什么此内容脚本在firefox加载项中多次运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆