Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to refresh / update pageMod contentScript?

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.

like image 808
Gorden Gram Avatar asked Jan 24 '26 21:01

Gorden Gram


1 Answers

You can directly change the contentScript property if you save the page-mod

First create your page-mod and save the object in a variable, like mod below. Then on your interval you can alter the contentScript property like you see in the func2. Once you've altered the contentScript you'll need the page-mod to re-evaluate the page somehow, I used a page reload since it sounded like from your description that was already happening.

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);
like image 137
Bryan Clark Avatar answered Jan 27 '26 16:01

Bryan Clark