Hide YouTube Shorts

Hides Shorts sections on YouTube

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

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

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

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

你需要先安裝一款使用者腳本管理器擴展,比如 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
    });
})();