Skip to content

Instantly share code, notes, and snippets.

@julienreszka
Forked from gwarser/disable-pageview-api.js
Created August 25, 2020 22:33
Show Gist options
  • Save julienreszka/cfd3129f0b27eee737a8c6445472ac17 to your computer and use it in GitHub Desktop.
Save julienreszka/cfd3129f0b27eee737a8c6445472ac17 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();
}
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment