YouTube speed rememberer

Remembers playback speed.

Stan na 14-02-2018. Zobacz najnowsza wersja.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

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

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

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.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

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