YouTube pauser

Pauses youtube videos when they start

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

You will need to install an extension such as Tampermonkey to install this script.

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

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