YouTube CPU Tamer by AnimationFrame

Reduce Browser's Energy Impact for playing YouTube Video

目前為 2021-08-29 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         YouTube CPU Tamer by AnimationFrame
// @name:en      YouTube CPU Tamer by AnimationFrame 
// @name:jp      YouTube CPU Tamer by AnimationFrame 
// @name:zh-tw   YouTube CPU Tamer by AnimationFrame 
// @name:zh-cn   YouTube CPU Tamer by AnimationFrame 
// @namespace    http://tampermonkey.net/
// @version     2021.08.29.1
// @license     MIT License
// @description     Reduce Browser's Energy Impact for playing YouTube Video
// @description:en  Reduce Browser's Energy Impact for playing YouTube Video
// @description:jp  YouTubeビデオのエネルギーインパクトを減らす
// @description:zh-tw  減少YouTube影片所致的能源消耗
// @description:zh-cn  减少YouTube影片所致的能源消耗
// @author       CY Fung
// @include     https://www.youtube.com/*
// @include     https://www.youtube.com/embed/*
// @include     https://www.youtube-nocookie.com/embed/*
// @include     https://www.youtube.com/live_chat*
// @include     https://www.youtube.com/live_chat_replay*
// @icon         https://www.google.com/s2/favicons?domain=youtube.com
// @run-at      document-start
// @grant       none
// ==/UserScript==
(function $$() {
    'use strict';

    const window = new Function('return window;')();

    let hkey_script = 'nzsxclvflluv';
    if (window[hkey_script]) return;
    window[hkey_script] = true;

    //if (!document.documentElement) return window.requestAnimationFrame($$);

    let $$requestAnimationFrame = window.requestAnimationFrame.bind(window);
    let $$setTimeout = window.setTimeout.bind(window);

    let mi = 0;
    let sb = {};
    const sFunc = (prop) => {
        return (func, ms, ...args) => {
            mi++;
            sb[mi] = {
                handler: args.length > 0 ? func.bind(null, ...args) : func,
                isNative: (func + "").indexOf('[native code]') > 0,
                [prop]: ms,
                nextAt: Date.now() + (ms > 0 ? ms : 0)
            };
            return mi;
        }
    }
    const rm = (jd) => {
        let o = sb[jd];
        if (typeof o != 'object') return;
        for (let k in o) o[k] = null;
        o = null;
        sb[jd] = null;
        delete sb[jd];
    }
    window.setTimeout = sFunc('timeout');
    window.setInterval = sFunc('interval');
    window.clearInterval = window.clearTimeout = rm;

    const $busy = Symbol('$busy');

    // high energy for non-native API
    let pf = (handler =>
        new Promise(resolve => {
            if (!($busy in handler)) {
                handler[$busy] = true;
                handler();
                delete handler[$busy];
            }
            resolve();
        })
    );

    let dt = 0;
    let jf;
    (jf = $$requestAnimationFrame.bind(window, () => {
        let now = Date.now();
        if (now < dt) return jf();
        // ======= MarcoTask [requestAnimationFrame] =======
        let promises = [];
        for (let mi in sb) {
            const o = sb[mi];
            let {
                handler,
                timeout,
                interval,
                nextAt,
                isNative
            } = o;
            if (now < nextAt) continue;
            isNative ? handler() : promises.push(pf(handler));
            if (interval > 0) {
                o.nextAt += interval;
            } else {
                rm(mi);
            }
        }
        // ======= MarcoTask [requestAnimationFrame] =======
        if (promises.length > 0) {
            if (document.hidden) dt = Date.now() + 160; // lower task execution rate for background playing
            let ret1 = Promise.all(promises);
            let ret2 = new Promise(resolve => $$setTimeout(resolve, 16));
            let race = Promise.race([ret1, ret2]);
            race.then(jf);
        } else {
            jf();
        }
    }))();

    // Your code here...
})();