Loop Fix

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

As of 11. 09. 2024. See the latest version.

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.

(I already have a user script manager, let me install it!)

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

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

window.setInterval(()=>{
    const vid1 = window.document.querySelector('video.html5-main-video');
    if (vid1) localStorage.removeItem('loopState');
    if (vid1 && vid1.readyState === window.HTMLMediaElement.HAVE_CURRENT_DATA && !vid1.paused && +vid1.currentTime.toFixed(0) >= +vid1.buffered.end(0).toFixed(0)) {
        localStorage.setItem('loopState', window.loopState);
        window.location.href = window.location.href.split('?')[0] + '?t=' + vid1.currentTime.toFixed(0) + '&' + window.location.href.split('?')[1];
    }

    const repeat = window.document.querySelectorAll('.ytp-menuitem[tabindex="-1"]')[0];
    if (repeat && repeat.onclick != clicking) {
        repeat.onclick = clicking
    }
    const vid = window.document.querySelector('video.html5-main-video');
    if (vid && vid.loop != window.loopState) {
        vid.loop = window.loopState;
    }
});