Loop Fix

Fixes youtube video repeating when watching in playlist + refreshing page if video stopped downloading

Stan na 18-09-2024. 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         Loop Fix
// @description  Fixes youtube video repeating when watching in playlist + refreshing page if video stopped downloading
// @version      0.1.16
// @author       0vC4
// @namespace    https://greasyfork.org/users/670183
// @match        *://*.youtube.com/watch/*
// @match        *://*.youtube.com/shorts/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=youtube.com
// @run-at       document-start
// @license      MIT
// @grant        none
// ==/UserScript==

// refresh if no reply during these actions
const maxLoadingTime = 500;
const maxSeekingTime = 1500;

(() => {
    function blockEvents(condition, ...events) {
        events = events.flat();
        const tag = window.EventTarget.prototype;
        tag._add = tag.addEventListener;
        tag.addEventListener = function (name, callback, options) {
            if (!events.includes(name)) return tag._add.call(this, name, callback, options);

            function cb(e) {
                if (!condition.call(this)) return;
                callback.call(this, e);
            };

            tag._add.call(this, name, cb, options);
        };
    }

    blockEvents(function() {
        return !this.loop; // have loop = block events
    }, 'pause', 'timeupdate', 'waiting');
})();

const log = window.console.log;
window.loopState = localStorage.getItem('loopState') ?? false;
function clicking(e) {
    window.loopState = JSON.parse(this.ariaChecked);
};

let created = false;
let refreshing = false;

let loaded = false;
let navigating = true;

let seeking = false;
let seekingId = 0;

const policy = window.trustedTypes && window.trustedTypes.createPolicy ? window.trustedTypes.createPolicy('timeout', {createScriptURL: str => str}) : {createScriptURL: str => str};
const timeout = delay => URL.createObjectURL(new Blob([`setTimeout(() => postMessage(0), ${delay});`]));
const refreshOnLoad = policy.createScriptURL(timeout(maxLoadingTime));
const seekingDebounce = policy.createScriptURL(timeout(500));

document.addEventListener('yt-navigate-start', () => {
    navigating = true;
    loaded = false;
});
document.addEventListener('yt-navigate-finish', () => {
    navigating = false;
});

window.setInterval(()=>{
    const vid = window.document.querySelector('video.html5-main-video');
    if (vid) {
        if (!created) {
            created = true;
            localStorage.removeItem('loopState');
            vid.addEventListener('seeking', () => {
                clearTimeout(seekingId);
                seeking = true;
                seekingId = setTimeout(() => {
                    seeking = false;
                }, maxSeekingTime);
            });

            // refresh if no data for half sec
            const callback = () => {
            debugger;
                if (!loaded && vid.readyState === window.HTMLMediaElement.HAVE_NOTHING) {
                    localStorage.setItem('loopState', window.loopState);
                    window.location.href = window.location.href;
                }
            };
            new Worker(refreshOnLoad).onmessage = callback;
        }

        // apply loopState after .src or page refresh
        if (vid.loop != window.loopState) vid.loop = window.loopState;

        // to prevent early page refresh
        if (vid.readyState === window.HTMLMediaElement.HAVE_CURRENT_DATA || vid.readyState === window.HTMLMediaElement.HAVE_ENOUGH_DATA) loaded = !navigating;

        const noData = vid.readyState === window.HTMLMediaElement.HAVE_CURRENT_DATA || vid.readyState === window.HTMLMediaElement.HAVE_METADATA;

        if (loaded && !refreshing && noData && !seeking && !vid.paused && +vid.currentTime.toFixed(0) >= +vid.buffered.end(0).toFixed(0) - 2 && +vid.currentTime.toFixed(0) < +vid.duration.toFixed(0) - 2) {
            refreshing = true;
            const callback = () => {
                if (seeking) {
                    refreshing = false;
                    return;
                }
                localStorage.setItem('loopState', window.loopState);
                window.location.href = window.location.href.split('?')[0] + '?t=' + (+vid.currentTime.toFixed(0) + 1) + '&' + window.location.href.split('?')[1];
            };
            new Worker(seekingDebounce).onmessage = callback;
        }

        // apply loopState after .src or page refresh
        if (vid.loop != window.loopState) vid.loop = window.loopState;

        // attach loop click detector
        const repeat = window.document.querySelectorAll('.ytp-menuitem[tabindex="-1"]')[0];
        if (repeat && repeat.onclick != clicking) {
            repeat.onclick = clicking;
        }
    }
});