Firefox add-on script gets executed many times

Deadly 提交于 2019-12-13 01:27:43

问题


I want to create a simple add-on to edit an iframe at a certain page.

I read the MDN add-on SDK tutorial, found a tutorial which explains how to interact with a page, here is the index.js:

var pageMod = require("sdk/page-mod");

pageMod.PageMod({
  include: "http://www.kongregate.com/games/tfender/contract-wars",
  contentScriptFile: "./main.js"
});

Here is the data/main.js, the contentScriptFile:

window.__cw_fullscreen_started = false;

if (!__cw_fullscreen_started) {
    console.log('started!');
    __cw_fullscreen_started = true;
}

The last tutorial has an example showing how to use pageMod, here it is:

var pageMod = require("sdk/page-mod");

pageMod.PageMod({
  include: "*.org",
  contentScript: 'document.body.innerHTML = ' +
                 ' "<h1>Page matches ruleset</h1>";'
});

Everything's clear here: when i open some .org site, the script replaces the page content with <h1>Page matches ruleset</h1>. Seems the document.body links to a body of a document that belongs to a page the script is executed at.

But when i execute jpm run and visit http://www.kongregate.com/games/tfender/contract-wars, the page at which the main.js script should be executed, i see that 'started!' has been logged many times.

I look through all the tutorials at MDN again again, but i have no idea why does it happen.

So the question: why the script gets executed so many times?

PS: in the answer to similar question a guy said that "content script will run exactly once for each HTML document loaded from example.com", and said that logging location.href will clear up the confusion, but for me console.log(location.href, window.self == window.top) logs out "www.example.com false" many times, so the confusion is not cleared up.

来源:https://stackoverflow.com/questions/36484797/firefox-add-on-script-gets-executed-many-times

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