Hide YouTube Shorts

Hides Shorts sections on YouTube

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Hide YouTube Shorts
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Hides Shorts sections on YouTube
// @author       iairu
// @match        https://*.youtube.com/*
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    const hideShorts = () => {
        [...document.querySelectorAll("h2")]
            .filter(h2 => h2.innerText.includes("Shorts"))
            .forEach(shortsHeading => {shortsHeading.parentElement.parentElement.style.display = "none"});
    };

    // Handle initial page load
    hideShorts();

    // Handle navigation via History API
    const originalPushState = history.pushState;
    const originalReplaceState = history.replaceState;

    history.pushState = function() {
        originalPushState.apply(this, arguments);
        setTimeout(hideShorts, 75);
    };

    history.replaceState = function() {
        originalReplaceState.apply(this, arguments);
        setTimeout(hideShorts, 75);
    };

    // Handle navigation via popstate event
    window.addEventListener('popstate', () => {
        setTimeout(hideShorts, 75);
    });

    // Create mutation observer to detect DOM changes
    const observer = new MutationObserver((mutations) => {
        hideShorts();
    });

    // Start observing the document with configured parameters
    observer.observe(document.body, {
        childList: true,
        subtree: true
    });
})();