YouTube Username Blocker

Blocks videos by YouTube username/handle

ของเมื่อวันที่ 18-04-2025 ดู เวอร์ชันล่าสุด

// ==UserScript==
// @name         YouTube Username Blocker
// @description  Blocks videos by YouTube username/handle
// @match        https://www.youtube.com/*
// @run-at       document-start
// @version 0.0.1.20250418115438
// @namespace https://greasyfork.org/users/1435046
// ==/UserScript==

(function() {
    'use strict';

    // Add usernames to block (without @, case insensitive)
    const blockedUsernames = [
        'MikeThurston'.toLowerCase(),
        // Add more usernames here
    ];

    function blockVideos() {
        document.querySelectorAll('ytd-rich-item-renderer').forEach(video => {
            // New selector that works with different YouTube layouts
            const channelLink = video.querySelector('a[href^="/@"]');
            if (channelLink) {
                const username = channelLink.href.split('/@')[1].split('/')[0].toLowerCase();
                if (blockedUsernames.includes(username)) {
                    video.remove(); // Use remove() instead of hide
                }
            }
        });
    }

    // Improved observation
    const observer = new MutationObserver(() => {
        blockVideos();
        // Also check for shadow DOM components
        document.querySelectorAll('ytd-rich-item-renderer').forEach(video => {
            if (video.shadowRoot) {
                video.shadowRoot.querySelectorAll('a[href^="/@"]').forEach(link => {
                    const username = link.href.split('/@')[1].split('/')[0].toLowerCase();
                    if (blockedUsernames.includes(username)) {
                        video.remove();
                    }
                });
            }
        });
    });

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

    // Run on page navigation too
    document.addEventListener('yt-navigate-finish', blockVideos);
    blockVideos();
})();