Created
August 25, 2020 22:33
-
-
Save julienreszka/cfd3129f0b27eee737a8c6445472ac17 to your computer and use it in GitHub Desktop.
"Disable Page Visibility API" scriptlet for uBO
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// 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 | |
}); | |
})() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// 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(); | |
} | |
})() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment