Youtube Player perf

Optimizes animation calls for lower GPU/CPU consumption

Versione datata 28/07/2023. Vedi la nuova versione l'ultima versione.

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

// ==UserScript==
// @name         Youtube Player perf
// @version      0.3
// @description  Optimizes animation calls for lower GPU/CPU consumption
// @namespace    nopeless.github.io
// @author       nopeless
// @match        https://www.youtube.com/watch?v=*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=youtube.com
// @grant        none
// @run-at       document-start
// @license      MIT
// ==/UserScript==

/* global _yt_player */

(() => {
    "use strict";

    function modifyBase() {
        console.log("Overriding _yt_player methods");

        if (!window._yt_player) return console.error("YT player not avaliable, load order is wrong");

        const PV = _yt_player.PV;

        // save the original prototype
        const PVPrototype = _yt_player.PV.prototype;

        let dirty = false;

        function update(a) {
            for (var b = _yt_player.u(Object.keys(a)), c = b.next(); !c.done; c = b.next()) {
                c = c.value;

                if (this.__updateCache.get(c) !== a[c]) {
                    // console.log("updating", c, a[c]);
                    this.updateValue(c, a[c]);
                    dirty = true;
                    this.__updateCache.set(c, a[c]);
                }
            }
        }

        _yt_player.PV = function (...args) {
            // YouTube base.js update approx 2023-07-28
            // this -> args[0]
            const a = args[0];

            PV.call(this, ...args);

            a.__updateCache = new Map();

            // override update
            a.update = update;
        };

        _yt_player.PV.prototype = Object.create(PVPrototype);
        _yt_player.PV.prototype.constructor = _yt_player.PV;



        const Un = _yt_player.Un;
        let counter = 0; // up to 4

        // YouTube base.js update approx 2023-07-28
        // Nn => Un
        _yt_player.Un = (a, b, c) => {
            // don't do excessive progress bar updates
            if (b === "transform") {
                if (dirty) {
                    counter = 4;
                    // console.log("unmarking dirty");
                    dirty = false;
                }
                if (counter > 0) {
                    // console.log("updating bar");
                    counter--;
                } else {
                    return;
                }
            }
            Un(a, b, c);
        };
    }

    window.__modifyBase = modifyBase;

    const ob = new MutationObserver(mrs => {
        const l = mrs.map(mr => mr.addedNodes[0]).find(node => node && node.nodeName === "SCRIPT" && node.src && node.src.match(/\/base\.js$/));

        if (!l) return;

        l.setAttribute("onload", "__modifyBase()");

        ob.disconnect();
    });

    console.log("watching for script changes");
    ob.observe(document, { attributes: false, childList: true, subtree: true });
})();