YouTube pauser

Pauses youtube videos when they start

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name       YouTube pauser
// @namespace  http://dannywhittaker.com
// @version    0.1
// @description  Pauses youtube videos when they start
// @match      https://www.youtube.com/watch*
// @copyright  2014, You
// ==/UserScript==

(function() {
    // Build a worker from an anonymous function body
    var blobURL = URL.createObjectURL(new Blob(['(',

        function() {
            var interval;

            self.addEventListener('message', function(e) {
                switch (e.data) {
                    case 'start':
                        interval = setInterval(function() {
                            self.postMessage('tick');
                        }, 100);
                        break;
                    case 'stop':
                        clearInterval(interval);
                        break;
                };
            }, false);
        }.toString(),

        ')()'
    ], {
        type: 'application/javascript'
    }));
    
    var worker = new Worker(blobURL);

    // Won't be needing this anymore
    URL.revokeObjectURL(blobURL);

    var playerReference;

    var start = new Date().getTime();
    var interval = 50;

    function getPlayerReference() {
        if (!playerReference) {
            playerReference = document.getElementById('movie_player');
        }

        return playerReference;
    }

    function windowIsHidden() {
        return document.hidden;
    }

    function clearInterval() {
        worker.postMessage('stop');
    }

    function fnInterval() {
        if (new Date().getTime() - start > 1000 * 10) {
            clearInterval();
        } else if (!windowIsHidden()) {
            clearInterval();
        } else {
            try {
                var ref = getPlayerReference();
                if (ref && ref.pauseVideo) {
                    ref.pauseVideo();
                }
            } catch (ex) {}
        }
    }

    worker.addEventListener('message', fnInterval);
    worker.postMessage('start');
})();