Video Background Play Fix

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/

当前为 2019-05-21 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==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);
                }
            });
        }
    }
})();