3

是否可以使用间隔进行更新?我曾尝试过:如何刷新/更新pageMod contentScript?

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。

回答

3

如果保存页面-MOD

首先创建您可以直接更改contentScript属性你的页面-MOD和保存对象变量,像下面mod。然后在您的间隔中,您可以更改contentScript属性,如您在func2中看到的那样。一旦你改变了contentScript,你需要页面模式以某种方式重新评估页面,我使用了页面重新加载,因为它听起来像从你的描述中已经发生。

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);