JS throttling with Chrome Extension

37 views Asked by At

I have a chrome extension that injects a content script to a web page that has an MM:SS stopwatch and some buttons which change a user's "state" and resets the stopwatch. The content script reads the value of the clock every second and, when it hits a certain value, it automatically clicks a button. Content script does this with a setInterval set to repeat every 1s.

Everything works while the window is visible and not obstructed. Once it is obstructed, I do see a difference immediately in the console log that prints what it sees from the clock each second. It starts to skip a second for a little while and then starts printing the value exactly every 1 minute. I understand this is Chrome optimization at play.

I've found that "Throttle Javascript timers in background" was an option 3 years ago but it is no longer present in chrome://flags/, but even that would not be a good fit as the performance tradeoff during heavy use would be tremendous. I have found that background scripts do not have the throttling problem but I'm not sure if utilizing that would mitigate the issue as I assume the page is throttled the same amount regardless of what is running but I can easily be wrong on this.

Is there a way to overcome this optimization and make sure the script runs smoothly every second when the page is obstructed, preferably only for this web page? Any input would be greatly appreciated, thank you!

Edit: Disabling "Calculate window occlusion on Windows" in chrome://flags/ seems to fix the issue entirely but this raises performance concerns, this does not seem to be able to turn on for only a certain web page.

0

There are 0 answers