YouTube Channel Blocker (URL input)

Blocks YouTube videos by entering channel URLs dynamically

As of 2025-04-26. See the latest version.

// ==UserScript==
// @name         YouTube Channel Blocker (URL input)
// @description  Blocks YouTube videos by entering channel URLs dynamically
// @match        https://www.youtube.com/*
// @run-at       document-start
// @grant        GM_registerMenuCommand
// @grant        GM_setValue
// @grant        GM_getValue
// @version 0.0.1.20250426081948
// @namespace https://greasyfork.org/users/1435046
// ==/UserScript==

(function() {
    'use strict';

    // Load blocked channels list from storage, or start with empty array
    let blockedChannelUrls = GM_getValue('blockedChannelUrls', []);

    // Extract lowercase handles from URLs
    function extractHandles(urls) {
        return urls
            .map(url => {
                const m = url.match(/\/@([^\/?#]+)/);
                return m ? m[1].toLowerCase() : null;
            })
            .filter(Boolean);
    }

    let blockedHandles = extractHandles(blockedChannelUrls);

    // Function to block videos
    function blockVideos() {
        document.querySelectorAll('ytd-rich-item-renderer, ytd-video-renderer').forEach(el => {
            const link = el.querySelector('a[href*="/@"]');
            if (!link) return;
            const handle = link.href.split('/@')[1].split(/[\/?#]/)[0].toLowerCase();
            if (blockedHandles.includes(handle)) {
                el.remove();
            }
        });
    }

    // Mutation observer to catch dynamic page changes
    const observer = new MutationObserver(blockVideos);
    observer.observe(document, { childList: true, subtree: true });
    document.addEventListener('yt-navigate-finish', blockVideos);

    blockVideos();

    // Add menu command to let user add new URLs
    GM_registerMenuCommand('Add Blocked Channel', async () => {
        const inputUrl = prompt('Enter full YouTube channel URL (like https://www.youtube.com/@SomeHandle):');
        if (inputUrl) {
            blockedChannelUrls.push(inputUrl.trim());
            GM_setValue('blockedChannelUrls', blockedChannelUrls);
            blockedHandles = extractHandles(blockedChannelUrls);
            alert('Channel added. Reload the page to update.');
        }
    });
})();