Youtube loop state keeper

To Loop or not To Loop is Yes

Bu betiği kurabilmeniz için Tampermonkey, Greasemonkey ya da Violentmonkey gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

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

Bu betiği kurabilmeniz için Tampermonkey ya da Violentmonkey gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

Bu betiği kurabilmeniz için Tampermonkey ya da Userscripts gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

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

Bu komut dosyasını yüklemek için bir kullanıcı komut dosyası yöneticisi uzantısı yüklemeniz gerekecek.

(Zaten bir kullanıcı komut dosyası yöneticim var, kurmama izin verin!)

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.

(Zateb bir user-style yöneticim var, yükleyeyim!)

// ==UserScript==
// @name         Youtube loop state keeper
// @namespace    https//mmis1000.me/
// @version      0.1
// @description  To Loop or not To Loop is Yes
// @author       mmis1000
// @match        https://www.youtube.com/watch?*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';
    const NS = "mmis1000";

    let loopState = sessionStorage.getItem(NS + '_loopState');
    let videoElement = document.querySelectorAll('video')[0];

    if (loopState) {
        setTimeout(function () {
            try {
                let oldState = JSON.parse(loopState);

                if (oldState.loop) {
                    try {
                        // force the menu to populate
                        // as youtube only populate the menu after you do a right click
                        var e = videoElement.ownerDocument.createEvent('MouseEvents');

                        e.initMouseEvent('contextmenu', true, true,
                                         videoElement.ownerDocument.defaultView, 1, 0, 0, 0, 0, false,
                                         false, false, false,2, null);

                        videoElement.dispatchEvent(e);
                        videoElement.click()

                        let loopButton = Array.prototype.slice.call(document.querySelectorAll('.ytp-menuitem'), 0).filter((el)=>el.textContent.indexOf('循環播放') >= 0)[0];
                        loopButton.click()
                        console.log('restoring loop state...')
                    } catch (e) {
                        videoElement.loop = oldState.loop;
                        console.log('restoring loop state... (fallback to set video element loop attr)')
                    }
                }
            } catch(e) {}

            let prevState = videoElement.loop;

            videoElement.addEventListener("timeupdate", function () {
                if (prevState !== videoElement.loop) {
                    console.log('updating loop state...')
                    prevState = videoElement.loop;
                    sessionStorage.setItem(NS + '_loopState', JSON.stringify({loop: videoElement.loop}));
                }
            })
        }, 0);
    }
})();