是否可以使用间隔更新它?我试过:
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);
问题是它会在每个时间间隔开始注册 pageMod 几次。 假设我有 mystyle 的内部: alert("hello world");所以我将转到某个页面,30 秒后,当我刷新该页面时,它将在警报框内执行 3 次“hello world”。我希望它只执行一次并每 30 秒更新一次 mySTLe。
最佳答案
如果你保存了page-mod,你可以直接改变contentScript
属性
首先创建您的 page-mod 并将对象保存在变量中,例如下面的 mod
。然后在您的间隔期间,您可以像在 func2
中看到的那样更改 contentScript
属性。更改 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);
关于javascript - 如何刷新/更新 pageMod contentScript?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12758629/