YouTube - Hide Recommended Popups

Removes experimental and featured recommendation sections from YouTube

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         YouTube - Hide Recommended Popups
// @description  Removes experimental and featured recommendation sections from YouTube
// @namespace    http://tampermonkey.net/
// @icon         https://cdn-icons-png.flaticon.com/64/2504/2504965.png
// @supportURL   https://github.com/5tratz/Tampermonkey-Scripts/issues
// @version      0.0.8
// @author       5tratz
// @match        https://www.youtube.com/*
// @license      MIT
// @grant        none
// ==/UserScript==

(() => {
    'use strict';

    const REMOVED_FLAG = 'data-yt-cleaned';

    /* ---------------------------------------------------
       CSS FIX (prevents vertical stacking + flicker)
       EXACTLY AS YOU HAD IT
    --------------------------------------------------- */
    const style = document.createElement('style');
    style.textContent = `
        ytd-rich-shelf-renderer #items,
        ytd-reel-shelf-renderer #items {
            display: flex !important;
            flex-direction: row !important;
            overflow-x: auto !important;
        }
    `;
    document.head.appendChild(style);

    /* ---------------------------------------------------
       TEXT MATCHING
       ADDED "Latest from" to block YouTube posts
    --------------------------------------------------- */
    const BLOCKED_TEXT = [
        'explore more topics',
        'shorts',
        'people also watched',
        'for you',
        'latest from'  // <-- ADDED THIS LINE
    ];

    function hasBlockedText(el) {
        return BLOCKED_TEXT.some(t =>
            el.textContent?.toLowerCase().includes(t)
        );
    }

    /* ---------------------------------------------------
       DETECT UNWANTED RENDERERS
       EXACTLY AS YOU HAD IT
    --------------------------------------------------- */
    function isShorts(el) {
        return (
            el.tagName === 'YTD-RICH-SHELF-RENDERER' &&
            (
                el.hasAttribute('is-shorts') ||
                el.querySelector('ytd-reel-shelf-renderer') ||
                el.querySelector('a[href^="/shorts"]')
            )
        );
    }

    function isExploreOrNudge(el) {
        return (
            el.tagName === 'YTD-RICH-SHELF-RENDERER' ||
            el.tagName === 'YTD-RICH-SECTION-RENDERER' ||
            el.tagName === 'YTD-FEED-NUDGE-RENDERER'
        ) && hasBlockedText(el);
    }

    function isUnwanted(el) {
        if (el.hasAttribute(REMOVED_FLAG)) return false;

        return (
            isShorts(el) ||
            isExploreOrNudge(el) ||
            el.querySelector?.('ytd-talk-to-recs-flow-renderer') ||
            el.querySelector?.('#big-yoodle') ||
            el.tagName === 'YTD-STATEMENT-BANNER-RENDERER'
        );
    }

    /* ---------------------------------------------------
       REMOVE
       EXACTLY AS YOU HAD IT
    --------------------------------------------------- */
    function remove(el) {
        el.setAttribute(REMOVED_FLAG, 'true');
        el.remove();
        console.debug('[YT Cleaner] Removed:', el.tagName);
    }

    /* ---------------------------------------------------
       SCAN
       EXACTLY AS YOU HAD IT
    --------------------------------------------------- */
    function scan(node) {
        if (node.nodeType !== 1) return;

        const targets = [
            'ytd-rich-shelf-renderer',
            'ytd-rich-section-renderer',
            'ytd-feed-nudge-renderer',
            'ytd-statement-banner-renderer'
        ].join(',');

        if (node.matches?.(targets) && isUnwanted(node)) {
            remove(node);
        }

        node.querySelectorAll?.(targets).forEach(el => {
            if (isUnwanted(el)) remove(el);
        });
    }

    /* ---------------------------------------------------
       INIT - WITH MINIMAL, SAFE IMPROVEMENTS
    --------------------------------------------------- */

    // 1. Initial scan
    scan(document.body);

    // 2. SPA Navigation fix - YouTube loads new content without page reload
    let lastUrl = location.href;
    const checkForNavigation = () => {
        const currentUrl = location.href;
        if (currentUrl !== lastUrl) {
            lastUrl = currentUrl;
            // YouTube changed pages, rescan everything
            scan(document.body);
        }
    };

    // Check for navigation every second
    setInterval(checkForNavigation, 1000);

    // 3. Your original observer - unchanged
    const observer = new MutationObserver(mutations => {
        for (const m of mutations) {
            for (const node of m.addedNodes) {
                scan(node);
            }
        }
    });

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

    // 4. Periodic scan as backup - every 5 seconds
    setInterval(() => {
        scan(document.body);
    }, 5000);
})();