Skip to content

Instantly share code, notes, and snippets.

@gwarser
Last active September 18, 2023 10:22
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save gwarser/3b47b61863bffcfebe4498c77b2301cd to your computer and use it in GitHub Desktop.
Save gwarser/3b47b61863bffcfebe4498c77b2301cd to your computer and use it in GitHub Desktop.
"Disable Page Visibility API" scriptlet for uBO
/// disable-pageview-api.js
// Based on: https://addons.mozilla.org/firefox/addon/disable-page-visibility/
// License: http://www.opensource.org/licenses/bsd-license.php
(function(){
// visibilitychange events are captured and stopped
document.addEventListener("visibilitychange", function(e) {
e.stopImmediatePropagation();
}, true);
// document.visibilityState always returns false
Object.defineProperty(Document.prototype, "hidden", {
get: function hidden() {
return false;
},
enumerable: true,
configurable: true
});
// document.visibilityState always returns "visible"
Object.defineProperty(Document.prototype, "visibilityState", {
get: function visibilityState() {
return "visible";
},
enumerable: true,
configurable: true
});
})()
/// video-bg-play.js
// Based on: https://github.com/mozilla/video-bg-play
// License: https://github.com/mozilla/video-bg-play/blob/master/LICENSE (MIT)
(function(){
const IS_YOUTUBE = window.location.hostname.search(/(?:^|.+\.)youtube.com/) > -1 ||
window.location.hostname.search(/(?:^|.+\.)youtube-nocookie.com/) > -1;
const IS_MOBILE_YOUTUBE = window.location.hostname == 'm.youtube.com';
const IS_DESKTOP_YOUTUBE = IS_YOUTUBE && !IS_MOBILE_YOUTUBE;
const IS_VIMEO = window.location.hostname.search(/(?:^|.+\.)vimeo.com/) > -1;
const IS_ANDROID = window.navigator.userAgent.indexOf('Android') > -1;
// Page Visibility API
if (IS_ANDROID || !IS_DESKTOP_YOUTUBE) {
Object.defineProperties(document,
{ 'hidden': {value: false}, 'visibilityState': {value: 'visible'} });
}
window.addEventListener(
'visibilitychange', evt => evt.stopImmediatePropagation(), true);
// Fullscreen API
if (IS_VIMEO) {
window.addEventListener(
'fullscreenchange', evt => evt.stopImmediatePropagation(), true);
}
// User activity tracking
if (IS_YOUTUBE) {
const refreshInterval = 5 * 60 * 1000; // every 5 minutes
waitForYoutubeLactInit(() => refreshLact(), refreshInterval);
}
function waitForYoutubeLactInit(aCallback, aCallbackInterval, aDelay = 1000) {
let pageWin = window;
if (pageWin.hasOwnProperty('_lact')) {
window.setInterval(aCallback, aCallbackInterval);
} else {
window.setTimeout(() => waitForYoutubeLactInit(aCallback,
aCallbackInterval,
aDelay * 2),
aDelay);
}
}
function refreshLact() {
window._lact = Date.now();
}
})()
@julienreszka
Copy link

Thx

@Zazcallabah
Copy link

Did this suddenly stop working for anyone else?

@julienreszka
Copy link

julienreszka commented Sep 22, 2020

I just downloaded an old apk of firefox (fennec-68.11.0) (must prevent it from auto updating in play store)
https://ftp.mozilla.org/pub/mobile/releases/68.11.0/android-api-16/multi/

@gwarser
Copy link
Author

gwarser commented Sep 22, 2020

@Zazcallabah
Copy link

no i know about the config workaround, i'm saying it stopped working from one moment to the next.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment