YouTube 关键词屏蔽

屏蔽包含指定关键词的视频、评论和相关视频。YouTube Keyword Blocker.

当前为 2025-03-05 提交的版本,查看 最新版本

// ==UserScript==
// @name         YouTube 关键词屏蔽
// @namespace    https://gf.qytechs.cn/users/1171320
// @version      0.11
// @description  屏蔽包含指定关键词的视频、评论和相关视频。YouTube Keyword Blocker.
// @author       yzcjd
// @author2     Lama AI 辅助
// @match        *://www.youtube.com/*
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    // 默认关键词列表
    let keywords = [];

    // 从本地存储加载关键词
    function loadKeywords() {
        const storedKeywords = localStorage.getItem('youtubeBlockKeywords');
        if (storedKeywords) {
            keywords = JSON.parse(storedKeywords);
        }
    }

    // 保存关键词到本地存储
    function saveKeywords() {
        localStorage.setItem('youtubeBlockKeywords', JSON.stringify(keywords));
    }

    // 检查元素是否包含关键词
    function containsKeywords(text) {
        return keywords.some(keyword => text.toLowerCase().includes(keyword.toLowerCase()));
    }

    // 移除包含关键词的元素
    function removeElements() {
        // 移除视频
        document.querySelectorAll('ytd-video-renderer, ytd-grid-video-renderer, ytd-compact-video-renderer, ytd-rich-item-renderer').forEach(video => {
            const titleElement = video.querySelector('#video-title') || video.querySelector('.title');
            const title = titleElement?.textContent || '';
            if (containsKeywords(title)) {
                video.remove();
            }
        });

        // 移除评论
        document.querySelectorAll('ytd-comment-thread-renderer').forEach(comment => {
            const commentTextElement = comment.querySelector('#content-text');
            const commentText = commentTextElement?.textContent || '';
            if (containsKeywords(commentText)) {
                comment.remove();
            }
        });

        // 移除相关视频
        document.querySelectorAll('ytd-compact-video-renderer').forEach(relatedVideo => {
            const relatedTitleElement = relatedVideo.querySelector('#video-title') || relatedVideo.querySelector('.title');
            const relatedTitle = relatedTitleElement?.textContent || '';
            if (containsKeywords(relatedTitle)) {
                relatedVideo.remove();
            }
        });

        // 移除首页后的视频,保留空白区域
        document.querySelectorAll('ytd-rich-item-renderer').forEach(homeVideo => {
            const homeTitleElement = homeVideo.querySelector('#video-title') || homeVideo.querySelector('.title');
            const homeTitle = homeTitleElement?.textContent || '';
            if (containsKeywords(homeTitle)) {
                homeVideo.style.display = 'none'; // 不完全移除,留空白
            }
        });
    }

    // 创建设置关键词的按钮
    function createSettingsButton() {
        const button = document.createElement('button');
        button.textContent = '关键词设置';
        button.style.position = 'fixed';
        button.style.bottom = '20px';
        button.style.right = '20px';
        button.style.zIndex = '1000';
        button.style.padding = '10px 20px';
        button.style.backgroundColor = '#ff0000';
        button.style.color = '#ffffff';
        button.style.border = 'none';
        button.style.borderRadius = '5px';
        button.style.cursor = 'pointer';

        button.addEventListener('click', () => {
            const input = prompt('请输入要屏蔽的关键词(用逗号分隔):', keywords.join(', '));
            if (input !== null) {
                keywords = input.split(',').map(word => word.trim()).filter(word => word);
                saveKeywords();
                removeElements();
            }
        });

        document.body.appendChild(button);
    }

    // 页面加载后执行
    window.addEventListener('load', () => {
        loadKeywords();
        createSettingsButton();
        removeElements();

        // 使用 MutationObserver 监听动态加载的内容
        const observer = new MutationObserver(removeElements);
        observer.observe(document.body, { childList: true, subtree: true });
    });

})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址