Reddit Advanced Content Filter

Automatically hides posts in your Reddit feed based on keywords or subreddits you specify. Supports whitelist entries that override filtering.

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

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name Reddit Advanced Content Filter
// @namespace https://greasyfork.org/en/users/567951-stuart-saddler
// @version 2.7
// @description Automatically hides posts in your Reddit feed based on keywords or subreddits you specify. Supports whitelist entries that override filtering.
// @author Stuart Saddler
// @license MIT
// @icon https://clipart-library.com/images_k/smoke-clipart-transparent/smoke-clipart-transparent-6.png
// @supportURL https://greasyfork.org/en/users/567951-stuart-saddler
// @match *://www.reddit.com/*
// @match *://old.reddit.com/*
// @run-at document-end
// @grant GM.getValue
// @grant GM.setValue
// @grant GM_addStyle
// @grant GM_registerMenuCommand
// @grant GM_unregisterMenuCommand
// ==/UserScript==

(async function () {
    'use strict';

    console.log('[DEBUG] Script started. Reddit Advanced Content Filter (Debug Version).');

    // Debounce function to prevent excessive calls
    function debounce(func, wait) {
        console.log('[DEBUG] Defining debounce function.');
        let timeout;
        return (...args) => {
            clearTimeout(timeout);
            timeout = setTimeout(() => func.apply(this, args), wait);
        };
    }

    console.log('[DEBUG] debounce function successfully defined.');

    const postSelector = 'article, div[data-testid="post-container"], shreddit-post';
    let filteredCount = 0;
    let menuCommand = null;
    let processedPosts = new WeakSet();
    let blocklistSet = new Set();
    let keywordPattern = null;
    // NEW: whitelist support
    let whitelistSet = new Set();
    let whitelistPattern = null;
    let pendingUpdates = 0;

    const batchUpdateCounter = debounce(() => {
        // Always attempt to register/update the menu command
        if (typeof GM_registerMenuCommand !== 'undefined') {
            console.log('[DEBUG] GM_registerMenuCommand is available. Registering menu command.');
            if (menuCommand !== null) {
                GM_unregisterMenuCommand(menuCommand);
                console.log('[DEBUG] Unregistered existing menu command.');
            }
            menuCommand = GM_registerMenuCommand(
                `Configure Filter (${filteredCount} blocked)`,
                showConfig
            );
            console.log('[DEBUG] Menu command registered/updated.');
        } else {
            console.error('[DEBUG] GM_registerMenuCommand is not available. Falling back to createFallbackButton.');
            createFallbackButton();
        }
    }, 16);

    const CSS = `
        .content-filtered { display: none !important; height: 0 !important; overflow: hidden !important; }
        .reddit-filter-dialog {
            position: fixed;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            background: white;
            padding: 20px;
            border-radius: 8px;
            z-index: 1000000;
            box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
            min-width: 300px;
            max-width: 350px;
            font-family: Arial, sans-serif;
            color: #333;
        }
        .reddit-filter-dialog h2 {
            margin-top: 0;
            color: #0079d3;
            font-size: 1.5em;
            font-weight: bold;
        }
        .reddit-filter-dialog p {
            font-size: 0.9em;
            margin-bottom: 10px;
            color: #555;
        }
        .reddit-filter-dialog textarea {
            width: calc(100% - 16px);
            height: 100px;
            padding: 8px;
            margin: 10px 0;
            border: 1px solid #ccc;
            border-radius: 4px;
            font-family: monospace;
            background: #f9f9f9;
            color: #000;
            resize: vertical;
        }
        .reddit-filter-dialog .button-container {
            display: flex;
            justify-content: flex-end;
            gap: 10px;
            margin-top: 10px;
        }
        .reddit-filter-dialog button {
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 8px 16px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 1em;
            text-align: center;
        }
        .reddit-filter-dialog .save-btn {
            background-color: #0079d3;
            color: white;
        }
        .reddit-filter-dialog .cancel-btn {
            background-color: #f2f2f2;
            color: #333;
        }
        .reddit-filter-dialog button:hover {
            opacity: 0.9;
        }
        .reddit-filter-overlay {
            position: fixed;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background: rgba(0, 0, 0, 0.5);
            z-index: 999999;
        }
    `;

    if (!document.querySelector('style[data-reddit-filter]')) {
        const style = document.createElement('style');
        style.textContent = CSS;
        style.setAttribute('data-reddit-filter', 'true');
        document.head.appendChild(style);
        console.log('[DEBUG] Injected custom CSS.');
    }

    /**
     * Constructs a regular expression pattern from a list of keywords.
     * @param {string[]} keywords - Array of keywords/subreddit names.
     * @returns {RegExp|null} - Compiled regular expression or null if no keywords provided.
     */
    const getKeywordPattern = (keywords) => {
        if (keywords.length === 0) {
            console.warn('[DEBUG] List is empty. No keyword pattern will be created.');
            return null;
        }
        const escapedKeywords = keywords.map(k => k.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'));
        // Note: the plural matching (s|es|ies) is applied uniformly.
        const patternString = `\\b(${escapedKeywords.join('|')})(s|es|ies)?\\b`;
        const pattern = new RegExp(patternString, 'i');
        console.log('[DEBUG] Constructed pattern:', pattern);
        return pattern;
    };

    /**
     * Displays the configuration dialog for managing the blocklist and whitelist.
     */
    async function showConfig() {
        console.log('[DEBUG] Opening configuration dialog.');

        const overlay = document.createElement('div');
        overlay.className = 'reddit-filter-overlay';
        const dialog = document.createElement('div');
        dialog.className = 'reddit-filter-dialog';
        dialog.innerHTML = `
            <h2>Reddit Filter: Settings</h2>
            <p><strong>Blocklist:</strong> Enter keywords or subreddit names one per line. Posts matching these will be hidden.</p>
            <textarea spellcheck="false" id="blocklist">${Array.from(blocklistSet).join('\n')}</textarea>
            <p><strong>Whitelist:</strong> Enter words or phrases one per line. If any are present in a post, that post will <em>not</em> be filtered even if it contains a blocked word.</p>
            <textarea spellcheck="false" id="whitelist">${Array.from(whitelistSet).join('\n')}</textarea>
            <div class="button-container">
                <button class="cancel-btn">Cancel</button>
                <button class="save-btn">Save</button>
            </div>
        `;

        document.body.appendChild(overlay);
        document.body.appendChild(dialog);

        const closeDialog = () => {
            dialog.remove();
            overlay.remove();
            console.log('[DEBUG] Configuration dialog closed.');
        };

        dialog.querySelector('.save-btn').addEventListener('click', async () => {
            // Update blocklist
            const blocklistInput = dialog.querySelector('#blocklist').value;
            blocklistSet = new Set(
                blocklistInput
                    .split('\n')
                    .map(item => item.trim().toLowerCase())
                    .filter(item => item.length > 0)
            );
            keywordPattern = getKeywordPattern(Array.from(blocklistSet));
            await GM.setValue('blocklist', Array.from(blocklistSet));
            console.log('[DEBUG] Blocklist saved:', Array.from(blocklistSet));

            // Update whitelist
            const whitelistInput = dialog.querySelector('#whitelist').value;
            whitelistSet = new Set(
                whitelistInput
                    .split('\n')
                    .map(item => item.trim().toLowerCase())
                    .filter(item => item.length > 0)
            );
            whitelistPattern = getKeywordPattern(Array.from(whitelistSet));
            await GM.setValue('whitelist', Array.from(whitelistSet));
            console.log('[DEBUG] Whitelist saved:', Array.from(whitelistSet));

            closeDialog();
            location.reload();
        });

        dialog.querySelector('.cancel-btn').addEventListener('click', closeDialog);
        overlay.addEventListener('click', closeDialog);
    }

    /**
     * Creates a fallback button for configuring the filter if GM_registerMenuCommand is unavailable.
     */
    function createFallbackButton() {
        console.log('[DEBUG] Creating fallback button.');
        const button = document.createElement('button');
        button.innerHTML = `Configure Filter (${filteredCount} blocked)`;
        button.style.cssText = 'position:fixed;top:10px;right:10px;z-index:999999;padding:8px;';
        button.addEventListener('click', showConfig);
        document.body.appendChild(button);
        console.log('[DEBUG] Fallback button created.');
    }

    /**
     * Processes a batch of posts to determine if they should be hidden.
     * @param {HTMLElement[]} posts - Array of post elements.
     */
    async function processPostsBatch(posts) {
        const batchSize = 5;
        for (let i = 0; i < posts.length; i += batchSize) {
            const batch = posts.slice(i, i + batchSize);
            await new Promise(resolve => requestIdleCallback(resolve, { timeout: 1000 }));
            batch.forEach(post => processPost(post));
        }
    }

    /**
     * Processes an individual post to decide whether to hide it.
     * Whitelist check is performed first (overriding any blocklist matches).
     * @param {HTMLElement} post - The post element to process.
     */
    function processPost(post) {
        if (!post || processedPosts.has(post)) return;
        processedPosts.add(post);

        const postContent = post.textContent.toLowerCase();

        // NEW: Check if the post matches any whitelist term. If so, do not filter.
        if (whitelistPattern && whitelistPattern.test(postContent)) {
            console.log('[DEBUG] Post contains whitelisted content. Skipping filtering:', post);
            return;
        }

        let shouldHide = false;
        const subredditElement = post.querySelector('a[data-click-id="subreddit"], a.subreddit');

        if (subredditElement) {
            const subredditName = subredditElement.textContent.trim().replace(/^r\//i, '').toLowerCase();
            console.log(`[DEBUG] Found subreddit: r/${subredditName}`);
            if (blocklistSet.has(subredditName)) {
                shouldHide = true;
                console.log(`[DEBUG] Hiding post from blocked subreddit: r/${subredditName}`);
            }
        }

        if (!shouldHide && blocklistSet.size > 0 && keywordPattern) {
            const matches = keywordPattern.test(postContent);
            console.log(`[DEBUG] Processing post. Content includes blocked keyword: ${matches}`);
            shouldHide = matches;
        }

        if (shouldHide) {
            hidePost(post);
            console.log('[DEBUG] Post hidden:', post);
        } else {
            console.log('[DEBUG] Post not hidden:', post);
        }
    }

    /**
     * Hides a post by adding the 'content-filtered' class.
     * @param {HTMLElement} post - The post element to hide.
     */
    function hidePost(post) {
        post.classList.add('content-filtered');
        const parentArticle = post.closest(postSelector);
        if (parentArticle) {
            parentArticle.classList.add('content-filtered');
        }
        filteredCount++;
        pendingUpdates++;
        batchUpdateCounter();
    }

    /**
     * Debounced function to handle updates to the posts.
     */
    const debouncedUpdate = debounce((posts) => {
        processPostsBatch(Array.from(posts));
    }, 100);

    /**
     * Initializes the userscript by loading the filter settings and setting up the MutationObserver.
     */
    async function init() {
        try {
            // Load blocklist
            const blocklist = await GM.getValue('blocklist', []);
            blocklistSet = new Set(blocklist.map(item => item.toLowerCase()));
            keywordPattern = getKeywordPattern(Array.from(blocklistSet));
            console.log('[DEBUG] Loaded blocklist:', blocklist);

            // NEW: Load whitelist
            const whitelist = await GM.getValue('whitelist', []);
            whitelistSet = new Set(whitelist.map(item => item.toLowerCase()));
            whitelistPattern = getKeywordPattern(Array.from(whitelistSet));
            console.log('[DEBUG] Loaded whitelist:', whitelist);

            batchUpdateCounter(); // Register/Update menu command on init
        } catch (error) {
            console.error('[DEBUG] Failed to load filter settings:', error);
        }

        const observerTarget = document.querySelector('.main-content') || document.body;
        const observer = new MutationObserver(mutations => {
            const newPosts = new Set();
            mutations.forEach(mutation => {
                mutation.addedNodes.forEach(node => {
                    if (node.nodeType === Node.ELEMENT_NODE) {
                        if (node.matches?.(postSelector)) {
                            newPosts.add(node);
                        }
                        node.querySelectorAll?.(postSelector).forEach(post => newPosts.add(post));
                    }
                });
            });
            if (newPosts.size > 0) {
                debouncedUpdate(newPosts);
            }
        });

        observer.observe(observerTarget, { childList: true, subtree: true });

        const initialPosts = document.querySelectorAll(postSelector);
        if (initialPosts.length > 0) {
            debouncedUpdate(initialPosts);
        }

        console.log('[DEBUG] Initialization complete. Observing posts.');
    }

    await init();
})();