1

I've encountered a very badly designed website, and most of the "dynamic" content on the site is changed within setTimeout/setInterval functions.

There is one very annoying setInterval function which checks page activity very quickly, and I would like to override this function.

What I tried is clearing all the setInterval values, one by one to check if the function stopped. Also, clearing all the possible setInterval values like this: for(let i = 0; i < 1000; i++) clearInterval(i);, stops the "dynamic" page rendering, WHICH I DONT WANT TO

Question: 1. is there any way to look into the interval or timeout function and clear them? 2. is there a better way?

My current solution: I'm overriding the setInterval and setTimeout before page loaded, and log the function .toString() within.

SOF_Sucks
  • 11
  • 3

2 Answers2

1

The short answer is no, you cannot determine anything about the setTimeout/setInterval callback given a timerId. It sounds as if you don't have access to edit the underlying Javascript to prevent this action. If you can get your JS to load before the code in the page, then what you can do is replace setInterval or setTimeout with your own code, where you can increase the interval to whatever you want. For example:

let setTimeoutOld = setTimeout;
setTimeout = (cb, interval) => {
    if (interval < 1000) {
        interval = 1000;
    }
    setTimeoutOld(cb, interval);
}

Now if the page does something like:

setTimeout(() => { console.log('fired') }, 1);

The timeout won't fire until at least 1000ms have passed. If you only wanted to modify a specific setTimeout based on the contents of the cb, you could do what you mentioned in your work-around: calling .toString() on the function and comparing it to some previously generated .toString() (partial or complete) and then doing whatever you wanted.

Mordred
  • 3,734
  • 3
  • 36
  • 55
  • If they can execute scripts before the page, then they could also try to log the signature of the guilty function (either its name if unique enough, either its toString). Once found, they might be able to change the timeout of only this function. – Kaiido Aug 16 '19 at 06:28
0

I assume you have access to code wherever it's setting timeout or interval. So just take a global array and push it's output to it. Then iterate that array to clear everything.

For ex.

timeouts = []; 

now, in code where there's timeout like

var timeout = setTimeout(<callback> , <interval> ); // push it's output to that global.
timeouts.push(timeout);

Then at the very end clear that.

   for(time of timeouts) {
     clearTimeout(time); 
   }

Similarly, for Interval.

Hope this is what you are looking for.

Community
  • 1
  • 1
shyammakwana.me
  • 5,562
  • 2
  • 29
  • 50