YouTube pauser

Pauses youtube videos when they start

Du musst eine Erweiterung wie Tampermonkey, Greasemonkey oder Violentmonkey installieren, um dieses Skript zu installieren.

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

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

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

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

Sie müssten eine Skript Manager Erweiterung installieren damit sie dieses Skript installieren können

(Ich habe schon ein Skript Manager, Lass mich es installieren!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

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