YouTube Performance Booster

Optimizes YouTube for maximum performance with faster loading and instant navigation

当前为 2025-06-04 提交的版本,查看 最新版本

// ==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://gf.qytechs.cn/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);
})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址