LiteTube

Hide video recommendations from listing and remove annoying auto-mix playlists to reduce lag and improve performance

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         LiteTube
// @namespace    http://tampermonkey.net/
// @version      2.8.4
// @description  Hide video recommendations from listing and remove annoying auto-mix playlists to reduce lag and improve performance
// @author       Liminality Dreams
// @match        https://www.youtube.com/*
// @icon         https://img.icons8.com/?size=100&id=62852&format=png&color=228BE6
// @grant        none
// @run-at       document-start
// @license GNU 3.0
// ==/UserScript==

(function() {
    'use strict';

    let reloadAttempts = new Map();
    const MAX_RELOAD_ATTEMPTS = 3;
    const RELOAD_DELAY = 300;

    const AUTO_MIX_PATTERNS = /^RD/;

    function isAutoMix(listId) {
        return listId && AUTO_MIX_PATTERNS.test(listId);
    }

    function isWatchPage() {
        return window.location.pathname === '/watch';
    }

    function getVideoId() {
        const url = new URL(window.location.href);
        return url.searchParams.get('v');
    }

    function checkAndReload() {
        if (!isWatchPage()) return;

        const url = new URL(window.location.href);
        const listParam = url.searchParams.get('list');
        const videoId = getVideoId();

        if (!listParam || !isAutoMix(listParam)) return;

        const attemptKey = videoId || 'unknown';
        const attempts = reloadAttempts.get(attemptKey) || 0;

        if (attempts >= MAX_RELOAD_ATTEMPTS) {
            reloadAttempts.delete(attemptKey);
            return;
        }

        reloadAttempts.set(attemptKey, attempts + 1);

        url.searchParams.delete('list');
        url.searchParams.delete('start_radio');
        url.searchParams.delete('index');

        const cleanUrl = url.toString();

        if (window.location.href !== cleanUrl) {
            setTimeout(() => {
                window.location.replace(cleanUrl);
            }, RELOAD_DELAY);
        }
    }

    function removeElements(selectors) {
        selectors.forEach(selector => {
            document.querySelectorAll(selector).forEach(el => {
                el.remove();
            });
        });
    }

    function removeWatchPageSidebar() {
        if (!isWatchPage()) return;

        removeElements([
            '#secondary',
            'ytd-watch-next-secondary-results-renderer'
        ]);
    }

    function removeBloat() {
        removeElements([
            'ytd-rich-shelf-renderer[is-gaming]',
            'ytd-statement-banner-renderer',
            'ytd-banner-promo-renderer',
            'ytd-compact-promoted-video-renderer',
            'ytd-display-ad-renderer',
            'ytd-promoted-sparkles-web-renderer',
            'ytd-ad-slot-renderer',
            'ytd-in-feed-ad-layout-renderer',
            'ytd-promoted-video-renderer',
            'ytd-playlist-panel-renderer',
            '.ytp-playlist-menu'
        ]);
    }

    function injectStyles() {
        const style = document.createElement('style');
        style.textContent = `
            body[is-watch-page] #secondary,
            body[is-watch-page] ytd-watch-next-secondary-results-renderer,
            ytd-playlist-panel-renderer,
            .ytp-playlist-menu,
            ytd-ad-slot-renderer,
            ytd-in-feed-ad-layout-renderer {
                display: none !important;
                visibility: hidden !important;
            }
        `;
        (document.head || document.documentElement).appendChild(style);
    }

    function init() {
        injectStyles();
        checkAndReload();

        const observer = new MutationObserver(() => {
            removeWatchPageSidebar();
            removeBloat();
        });

        const startObserving = () => {
            if (document.body) {
                observer.observe(document.body, {
                    subtree: true,
                    childList: true
                });
                removeWatchPageSidebar();
                removeBloat();
            } else {
                setTimeout(startObserving, 100);
            }
        };

        if (document.readyState === 'loading') {
            document.addEventListener('DOMContentLoaded', startObserving);
        } else {
            startObserving();
        }

        window.addEventListener('yt-navigate-start', () => {
            const videoId = getVideoId();
            if (videoId) {
                reloadAttempts.delete(videoId);
            }
        });

        window.addEventListener('yt-navigate-finish', () => {
            checkAndReload();
            setTimeout(() => {
                removeWatchPageSidebar();
                removeBloat();
            }, 100);
        });

        window.addEventListener('yt-page-data-updated', () => {
            checkAndReload();
            removeWatchPageSidebar();
        });
    }

    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', init);
    } else {
        init();
    }
})();