Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chrome Extension: How to Disable Page Visibility API

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?

like image 691
RichardW Avatar asked Dec 05 '17 18:12

RichardW


1 Answers

Here's my solution:

for (event_name of ["visibilitychange", "webkitvisibilitychange", "blur"]) {
  window.addEventListener(event_name, function(event) {
        event.stopImmediatePropagation();
    }, true);
}

I added the blur event because the video I wanted to skip (everfi.net) used it to detect when I switched windows. Blocking that event along with visibilitychange and webkitvisibilitychange did the trick :)

I also modified the extension's manifest so that it works inside iframes.

Full code (chrome extension): https://github.com/NavinF/dont

Confirmed working with the following dog tags:

Google Chrome   63.0.3239.132 (Official Build) (64-bit)
Revision    2e6edcfee630baa3775f37cb11796b1603a64360-refs/branch-heads/3239@{#709}
OS  Mac OS X
JavaScript  V8 6.3.292.49
Command Line    /Applications/Google Chrome.app/Contents/MacOS/Google Chrome --flag-switches-begin --flag-switches-end
like image 165
Navin Avatar answered Oct 01 '22 05:10

Navin