I am writing a Chrome extension that needs to prevent webpages from triggering the document visibilitychange event. At the very least I need to be able to overwrite the document.visibilityState (even though it is a read-only property). If not possible, since this extension is for my purposes only and will not be on the Chrome extension store, is there a way I can config my Chrome Browser to achieve what I want? I only need to use this extension while Chrome "Developer Mode" is on, no other time.
I hope someone can think of a creative way to achieve this. Thank you.
Please note! There was a solution in an answer 4 years ago that no longer takes effect in newer versions of Chrome: Spoof or disable the Page Visibility API
Test it out yourself:
// This codes worked 4 years ago but not anymore
var c='(function(){var a=Node.prototype.addEventListener;Node.prototype.addEventListener=function(e){if(e=="visibilitychange"||e=="webkitvisibilitychange"){}else a.apply(this,arguments)}})()'
, E=document.documentElement;
E.setAttribute('onreset', c);
E.dispatchEvent(new CustomEvent('reset'));
E.removeAttribute('onreset');
// THIS WILL STILL LOG THE STATES EVEN WITH THE ABOVE CODE RUNNING
document.addEventListener("visibilitychange", function() {
console.log( document.visibilityState );
});
If its not possible in Chrome, is there Firefox/Safari/Opera Browser code that can achieve this?