YouTube Performance Mode

Heavy performance optimizer for YouTube: sidebar, comments, live chat, thumbnails, hover previews, animations, observers, memory, CPU usage.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         YouTube Performance Mode
// @namespace    https://github.com/
// @version      4.4
// @license MIT
// @description  Heavy performance optimizer for YouTube: sidebar, comments, live chat, thumbnails, hover previews, animations, observers, memory, CPU usage.
// @match        https://www.youtube.com/*
// @run-at       document-start
// @grant        none
// ==/UserScript==

(function() {
    "use strict";

    //////////////////////////////////////////////////////////////////////
    // GLOBAL UTIL
    //////////////////////////////////////////////////////////////////////
    const addCSS = (css) => {
        const s = document.createElement("style");
        s.textContent = css;
        document.documentElement.appendChild(s);
    };

    //////////////////////////////////////////////////////////////////////
    // 1. Disable heavy animations globally
    //////////////////////////////////////////////////////////////////////
    addCSS(`
        * {
            transition: none !important;
            animation: none !important;
        }
    `);

    //////////////////////////////////////////////////////////////////////
    // 2. Disable hover video preview (everywhere)
    //////////////////////////////////////////////////////////////////////
    addCSS(`
        ytd-thumbnail a {
            display: block !important;
            opacity: 1 !important;
            pointer-events: auto !important;
        }
    `);

    //////////////////////////////////////////////////////////////////////
    // 3. Force potato-mode thumbnails (bandwidth saving)
    //////////////////////////////////////////////////////////////////////
    const downgradeAllThumbnails = () => {
        const fixThumb = (img) => {
            if (!img.src) return;
            img.src = img.src
                .replace(/hq720|maxresdefault|hqdefault|sddefault/gi, "mqdefault")
                .replace(/\/hq\d+\.webp/gi, "/mqdefault.jpg")
                .replace(/\/maxresdefault\.jpg/gi, "/mqdefault.jpg");
        };
        document.querySelectorAll("img").forEach(fixThumb);
    };

    new MutationObserver(() => downgradeAllThumbnails())
        .observe(document.documentElement, { childList: true, subtree: true });

    //////////////////////////////////////////////////////////////////////
    // 4. COMMENT OPTIMIZER (keeps comments, but lightweight)
    //////////////////////////////////////////////////////////////////////
    addCSS(`
        ytd-comment-thread-renderer, ytd-comment-renderer {
            contain: content !important;
            will-change: auto !important;
            opacity: 1 !important;
        }
    `);

    // Remove expensive avatars + hover cards
    addCSS(`
        ytd-comment-renderer #author-thumbnail,
        ytd-comment-renderer #hovercard {
            display: none !important;
        }
    `);

    // Remove collapsing animation
    addCSS(`
        ytd-comment-renderer #body {
            transition: none !important;
        }
    `);

    //////////////////////////////////////////////////////////////////////
    // 5. LIVE CHAT OPTIMIZER (keeps chat but reduces jank)
    //////////////////////////////////////////////////////////////////////
    addCSS(`
        yt-live-chat-renderer,
        yt-live-chat-text-message-renderer {
            contain: content !important;
        }
        yt-live-chat-author-photo,
        yt-live-chat-message-input-renderer {
            display: none !important;
        }
    `);

    // Prevent YouTube from keeping hundreds of chat messages
    const pruneChat = () => {
        const chat = document.querySelector("yt-live-chat-item-list-renderer #items");
        if (!chat) return;
        while (chat.children.length > 40) {
            chat.removeChild(chat.firstChild);
        }
    };

    setInterval(pruneChat, 1500);

    //////////////////////////////////////////////////////////////////////
    // 6. SIDEBAR OPTIMIZATION (big performance win)
    //////////////////////////////////////////////////////////////////////

    // 6A: Force potato thumbnails in sidebar
    const patchSidebarImages = () => {
        document.querySelectorAll(
            'ytd-compact-video-renderer img, ytd-compact-radio-renderer img'
        ).forEach(img => {
            if (img.src.includes("hq") || img.src.includes("maxres")) {
                img.src = img.src.replace(/hq720|maxresdefault|hqdefault|sddefault/gi, "mqdefault");
            }
        });
    };

    new MutationObserver(patchSidebarImages)
        .observe(document.documentElement, { childList: true, subtree: true });

    // 6B: Disable hover effects in sidebar
    addCSS(`
        ytd-compact-video-renderer #thumbnail:hover *,
        ytd-compact-video-renderer #mouseover-overlay {
            display: none !important;
        }
    `);

    // 6C: Remove sidebar animations
    addCSS(`
        ytd-compact-video-renderer,
        ytd-compact-video-renderer * {
            animation: none !important;
            transition: none !important;
        }
    `);

    // 6D: Contain sidebar items to avoid layout thrash
    addCSS(`
        ytd-compact-video-renderer {
            contain: content !important;
        }
    `);

    // 6E: Reduce MutationObserver spam specifically for sidebar
    const OriginalMutationObserver = window.MutationObserver;
    window.MutationObserver = function(callback) {
        return new OriginalMutationObserver((mut, obs) => {
            if (mut.length > 30) return; // blocks huge batches
            callback(mut, obs);
        });
    };
    window.MutationObserver.prototype = OriginalMutationObserver.prototype;

    //////////////////////////////////////////////////////////////////////
    // 7. Disable expensive tracking scripts (safe)
    //////////////////////////////////////////////////////////////////////
    const blockExpensiveProps = [
        "ytLoggingLatencyTick_",
        "ytcsi.tick",
        "yt.logging.transport_"
    ];

    blockExpensiveProps.forEach(key => {
        Object.defineProperty(window, key, {
            value: () => {},
            writable: false,
            configurable: false
        });
    });

    //////////////////////////////////////////////////////////////////////
    // 8. Remove page-resizing observers that slow down Polymer Fixes
    //////////////////////////////////////////////////////////////////////
    const disableResizeObserver = () => {
        try {
            window.ResizeObserver = class {
                observe() {}
                unobserve() {}
                disconnect() {}
            };
        } catch (e) {}
    };
    disableResizeObserver();

})();