Youtube Music fix performance

Try to fix the performance issues that youtube music has

K instalaci tototo skriptu si budete muset nainstalovat rozšíření jako Tampermonkey, Greasemonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Userscripts.

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

K instalaci tohoto skriptu si budete muset nainstalovat manažer uživatelských skriptů.

(Už mám manažer uživatelských skriptů, nechte mě ho nainstalovat!)

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.

(Už mám manažer uživatelských stylů, nechte mě ho nainstalovat!)

// ==UserScript==
// @name         Youtube Music fix performance
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Try to fix the performance issues that youtube music has
// @author       Marco Pfeiffer <[email protected]>
// @match        https://music.youtube.com/*
// @icon         https://music.youtube.com/favicon.ico
// @run-at       document-start
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    const global = window.unsafeWindow ?? window;
    const setInterval = global.setInterval;
    const clearInterval = global.clearInterval;
    const intervals = new Set();

    global.setInterval = function(fn, ms) {
        const n = setInterval.call(this, fn, ms);
        console.log("setup interval", n, fn, ms, "still pending timers", Array.from(intervals));
        intervals.add(n);
        return n;
    };
    global.clearInterval = function (n) {
        if (!intervals.delete(n)) {
            console.log("failed cleanup of", n, "still pending timers", Array.from(intervals));
            if (intervals.size > 5) {
                const timersToCleanUp = Array.from(intervals).slice(0, -5);
                console.log("cleanup timers", timersToCleanUp);
                timersToCleanUp.forEach(clearInterval);
            }
        } else {
            console.log("cleanup interval", n, "still pending timers", Array.from(intervals));
        }
        return clearInterval.call(this, n);
    };
})();