Prevents YouTube and Vimeo from pausing videos when minimizing or switching tabs. Cross-browser port of https://addons.mozilla.org/en-US/firefox/addon/video-background-play-fix/
当前为
// ==UserScript==
// @name Video Background Play Fix
// @namespace https://greasyfork.org/en/users/50-couchy
// @version 1.1
// @description Prevents YouTube and Vimeo from pausing videos when minimizing or switching tabs. Cross-browser port of https://addons.mozilla.org/en-US/firefox/addon/video-background-play-fix/
// @author Couchy
// @match *://*.youtube.com/*
// @match *://*.vimeo.com/*
// @grant none
// @run-at document-start
// ==/UserScript==
(function() {
'use strict';
const IS_YOUTUBE = window.location.hostname.endsWith('youtube.com') || window.location.hostname.endsWith('youtube-nocookie.com');
const IS_VIMEO = window.location.hostname.endsWith('vimeo.com');
// Page Visibility API
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 REFRESH_INTERVAL = 60 * 1000;
const TIMEOUT_FUNC = function(){window._lact = Date.now();};
if (window.hasOwnProperty('_lact')) {
window.setInterval(TIMEOUT_FUNC, REFRESH_INTERVAL);
}
else {
// Set a "listener" for _lact property to be written the first time
Object.defineProperty(window, '_lact', {
configurable: true,
set: function(val){
Object.defineProperty(window, '_lact', {writable: true});
window._lact = val;
window.setInterval(TIMEOUT_FUNC, REFRESH_INTERVAL);
}
});
}
}
})();