YouTube - Shorts remover (homepage & subscriptions only)

Removes the shorts from your homepage + subscription page.

目前为 2023-08-14 提交的版本。查看 最新版本

// ==UserScript==
// @name        YouTube - Shorts remover (homepage & subscriptions only)
// @namespace   https://greasyfork.org/en/users/2755-robotoilinc
// @author      RobotOilInc
// @version     0.4.0
// @license     MIT
// @description Removes the shorts from your homepage + subscription page.
// @match       https://*.youtube.com/
// @match       https://*.youtube.com/feed/*
// @match       https://youtube.com/
// @match       https://youtube.com/feed/*
// @icon        https://i.imgur.com/rvNc0hN.png
// @grant       none
// ==/UserScript==
const observer = new MutationObserver(function(mutationList, observer) {
    // Remove any reel shelves
    const shelves = document.getElementsByTagName('ytd-reel-shelf-renderer')
    for (let i = 0; i < shelves.length; i++) {
        console.log("Removing shorts shelf", shelves[i]);
        shelves[i].remove()
    }

    // Remove any rich reel shelves
    const richShelves = document.getElementsByTagName('ytd-rich-shelf-renderer')
    for (let i = 0; i < richShelves.length; i++) {
        console.log("Removing shorts shelf", richShelves[i]);
        richShelves[i].remove()
    }

    // Remove anything with the shorts URL
    const urls = document.querySelectorAll('[href*="/shorts/"]');
    for (let i = 0; i < urls.length; i++) {
        const element = urls[i].closest('ytd-item-section-renderer');
        if (element) { console.log("Removing short url", urls[i]); element.remove(); }
    }

    // Remove anything with the shorts overlay
    const overlays = document.querySelectorAll('ytd-thumbnail-overlay-time-status-renderer[overlay-style="SHORTS"]');
    for (let i = 0; i < overlays.length; i++) {
        const element = overlays[i];

        const grid = element.closest('.ytd-grid-renderer');
        if (grid) { console.log("Removing short overlay", element); grid.remove(); return; }

        const section = element.closest('ytd-item-section-renderer');
        if (section) { console.log("Removing short overlay", element); grid.remove(); return; }

        var list = element.closest('.ytd-section-list-renderer');
        if (list) { console.log("Removing short overlay", element); list.remove(); }
    }
});


const observeShorts = () => {
    observer.observe(document.querySelector('ytd-page-manager'), { childList: true, subtree: true });
}

// Ensure the observer is update when we navigate away
navigation.addEventListener("navigate", (event) => {
    const url = new URL(event.destination.url);
    observer.disconnect();
    observeShorts()
});

// Ensure we observe on initial load as well
observeShorts();