【问题标题】:How to refresh / update pageMod contentScript?如何刷新/更新页面添加内容脚本?
【发布时间】:2012-10-06 09:45:42
【问题描述】:
是否可以使用间隔更新它?我试过了:
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 秒更新一次谜题。
【问题讨论】:
标签:
javascript
firefox-addon
firefox-addon-sdk
content-script
【解决方案1】:
如果保存page-mod,可以直接更改contentScript属性
首先创建您的页面模块并将对象保存在变量中,例如下面的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);