3

Is it possible to update it using an interval? I have tried:

var timers = require("timers");
var pageMod = require("page-mod");
var mystyle = ....;

function func1(){
pageMod.PageMod({
  include: "*.org",
  contentScript: mystyle
});
}
func1();

function func2(){
   mysyle = http://......
}

timers.setInterval(func2,10000);

The problem is that it will start registering pageMod couple of times every interval. Let's say I have inside of mystyle: alert("hello world"); so I'll go to to some page, after 30 seconds it when I'll refresh that single page it will execute 3 times "hello world" inside of alert box. I want it to execute only once and updateing the mystle every 30 seoncds.

4

1 回答 1

3

contentScript保存page-mod就可以直接改属性

首先创建您的 page-mod 并将对象保存在变量中,如下mod所示。然后在您的时间间隔内,您可以更改contentScript属性,就像您在func2. 一旦你改变了,contentScript你将需要 page-mod 以某种方式重新评估页面,我使用了页面重新加载,因为它听起来像你的描述已经发生了。

var mod = require("page-mod").PageMod({
  include: ["*.org"],
  contentScript: "alert('the first alert');"
});

function func2(){
  // change the contentScript
  mod.contentScript = "alert('the second alert');";
  // cause the page-mod to re-evaluate
  require("tabs").activeTab.reload();
}

tab = require("tabs").open("http://mozilla.org");
require("timers").setInterval(func2, 5 * 1000);
于 2013-01-22T20:59:22.363 回答