YouTube speed rememberer

Remembers playback speed.

Від 14.02.2018. Дивіться остання версія.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey 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 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.

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

(У мене вже є менеджер скриптів, дайте мені встановити його!)

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 speed rememberer
// @version      0.3.1
// @description  Remembers playback speed.
// @author       gvvad
// @match        *.youtube.com/*
// @run-at       document-end
// @namespace https://greasyfork.org/users/100160
// ==/UserScript==

(function() {
    'use strict';
    var PLAYER_CLASSNAME = "#movie_player";
    
    //set button on video player
    //_msg - lable string
    var setLabel = function(_msg, _mp) {
        var label = document.querySelector("#_ytp-label");
        if (_msg === undefined) {
            if (label) label.parentElement.removeChild(label);
            return;
        }
        if (label) {
            label.innerText = _msg;
            return;
        }
        var cls = document.querySelector(PLAYER_CLASSNAME).querySelector(".ytp-right-controls");

        var span = document.createElement('span');
        span.setAttribute('id','_ytp-label');
        span.setAttribute('class','ytp-button');
        span.onclick = function() {
            _mp.setPlaybackRate(1);
        };
        span.innerText = _msg;

        cls.insertBefore(span, cls.firstChild);
    };
    
    //set or remove button
    //_rate - play rate
    var setSpeedLabel = function(_rate, _mp) {
        if (_rate == 1) {
            setLabel();
        } else {
            setLabel('x' + _rate, _mp);
        }
    };

    var rate = 1.0;
    rate = parseFloat(localStorage.getItem("pl-rate"));
    rate = (isNaN(rate))? 1.0 : rate;

    //modificate player object and store play rate
    //mp - movieplayer object
    var worker = function(mp) {
        if (rate != 1.0) {
            mp.setPlaybackRate(rate);
            setSpeedLabel(mp.getPlaybackRate(), mp);
        }

        mp.addEventListener("onPlaybackRateChange", function(){
            localStorage.setItem("pl-rate", mp.getPlaybackRate());
            setSpeedLabel(mp.getPlaybackRate(), mp);
        });
    };

    //Watchdog for youtube video-player object
    var _count = 25;
    var hInterval = setInterval(function() {
        var buf = document.querySelector(PLAYER_CLASSNAME);
        if (buf || !(_count--)) {
            clearInterval(hInterval);
            worker(buf);
        }
    }, 200);
})();