Youtube loop state keeper

To Loop or not To Loop is Yes

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