YouTube Performance Booster

Optimizes YouTube for maximum performance with faster loading and instant navigation

目前為 2025-06-04 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         YouTube Performance Booster
// @version      1.2
// @description  Optimizes YouTube for maximum performance with faster loading and instant navigation
// @author       HYPERR.
// @match        *://*.youtube.com/*
// @grant        GM_addStyle
// @grant        GM_setValue
// @grant        GM_getValue
// @run-at       document-start
// @license      MIT
// @namespace https://greasyfork.org/users/1476487
// ==/UserScript==

(function() {
    'use strict';

    // Performance optimization settings
    const settings = {
        instantNavigation: true,
        aggressivePrefetch: true,
        disableComments: false,
        disableRelatedVideos: false,
        disableNotifications: true,
        disableAnimations: true,
        lightTheme: false,
        qualityPreset: 'auto' // auto, 720p, 480p, 360p
    };

    // Apply CSS optimizations immediately
    GM_addStyle(`
        /* Disable animations and transitions */
        ytd-app[disable-animations] * {
            transition: none !important;
            animation: none !important;
            scroll-behavior: auto !important;
        }
        
        /* Hide non-essential elements */
        ytd-app[disable-comments] #comments,
        ytd-app[disable-related] #related,
        ytd-app[disable-notifications] #notification-preference-button,
        ytd-app[disable-notifications] .notification-topbar-button {
            display: none !important;
        }
        
        /* Light theme optimization */
        ytd-app[light-theme] {
            --yt-spec-general-background-a: #ffffff !important;
            --yt-spec-base-background: #f8f9fa !important;
            --yt-spec-menu-background: #ffffff !important;
        }
        
        /* Performance-focused layout */
        #secondary.ytd-watch-flexy {
            display: none !important;
        }
        
        #primary.ytd-watch-flexy {
            max-width: 100% !important;
        }
        
        /* Remove visual fluff */
        ytd-masthead[is-focused] #container.ytd-masthead,
        ytd-thumbnail-overlay-time-status-renderer,
        .ytp-ce-element {
            opacity: 0.8 !important;
        }
    `);

    // Feature toggles from settings
    const app = document.querySelector('ytd-app') || document.documentElement;
    if (settings.disableAnimations) app.setAttribute('disable-animations', '');
    if (settings.disableComments) app.setAttribute('disable-comments', '');
    if (settings.disableRelatedVideos) app.setAttribute('disable-related', '');
    if (settings.disableNotifications) app.setAttribute('disable-notifications', '');
    if (settings.lightTheme) app.setAttribute('light-theme', '');

    // Instant Navigation System
    if (settings.instantNavigation) {
        let lastHoveredLink = null;
        let hoverTimer = null;
        const NAV_DELAY = 100; // ms

        document.addEventListener('mouseover', function(e) {
            const link = e.target.closest('a');
            if (!link || !link.href || link.href === lastHoveredLink) return;

            clearTimeout(hoverTimer);
            lastHoveredLink = link.href;

            hoverTimer = setTimeout(() => {
                if (settings.aggressivePrefetch) {
                    // Prefetch resources for instant loading
                    fetch(link.href, {
                        method: 'HEAD',
                        mode: 'cors',
                        cache: 'force-cache'
                    });
                }

                // Preload for instant navigation
                const preload = document.createElement('link');
                preload.rel = 'preload';
                preload.as = 'document';
                preload.href = link.href;
                document.head.appendChild(preload);
            }, NAV_DELAY);
        }, {capture: true, passive: true});

        document.addEventListener('click', function(e) {
            const link = e.target.closest('a');
            if (link && link.href) {
                e.preventDefault();
                window.location.href = link.href;
            }
        }, {capture: true});
    }

    // Performance optimization functions
    function optimizePlayer() {
        const player = document.querySelector('video');
        if (!player) return;

        // Set quality preferences
        const qualityMap = {
            'auto': 'default',
            '720p': 'hd720',
            '480p': 'large',
            '360p': 'medium'
        };
        
        const preferredQuality = qualityMap[settings.qualityPreset] || 'default';
        try {
            player.setPlaybackQualityRange(preferredQuality);
        } catch(e) {
            console.log('YT Performance: Quality setting unavailable');
        }

        // Player performance tweaks
        player.preload = 'auto';
        player.playsInline = true;
        player.disablePictureInPicture = true;
    }

    function optimizePage() {
        // Remove non-critical elements
        const elementsToRemove = [
            'ytd-comments',
            'ytd-watch-next-secondary-results-renderer',
            'ytd-rich-item-renderer:has([overlay-style="SHORTS"])',
            '#secondary',
            '#masthead-container',
            '#player-ads'
        ];

        elementsToRemove.forEach(selector => {
            document.querySelectorAll(selector).forEach(el => {
                if (el && el.parentNode) {
                    el.parentNode.removeChild(el);
                }
            });
        });

        // Disable resource-heavy features
        if ('requestIdleCallback' in window) {
            requestIdleCallback(() => {
                // Disable chat if present
                const liveChat = document.querySelector('ytd-live-chat-frame');
                if (liveChat) liveChat.remove();
                
                // Disable JS animations
                document.body.classList.add('no-animations');
            });
        }
    }

    // MutationObserver to apply optimizations as page changes
    const observer = new MutationObserver(mutations => {
        optimizePlayer();
        optimizePage();
    });

    observer.observe(document, {
        childList: true,
        subtree: true,
        attributes: false
    });

    // Initialize optimizations
    window.addEventListener('load', () => {
        optimizePlayer();
        optimizePage();
        
        // Set up service worker for caching (if supported)
        if ('serviceWorker' in navigator) {
            navigator.serviceWorker.register('sw.js')
                .then(reg => console.log('YT Performance: Service worker registered'))
                .catch(err => console.log('YT Performance: Service worker failed', err));
        }
    });

    // Console info
    console.log('YouTube Performance Booster is active!');
    console.log('Settings:', settings);
})();