Reddit Post Filter

Automatically hide posts containing specific keywords or posted by specific users

// ==UserScript==
// @name         Reddit Post Filter
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Automatically hide posts containing specific keywords or posted by specific users
// @author       brfuk
// @license      MIT
// @match        *://www.reddit.com/*
// @grant        GM_setValue
// @grant        GM_getValue
// ==/UserScript==

(function() {
    'use strict';

    // Retrieve stored keywords and usernames, or use default values if not set
    let blockedKeywords = GM_getValue('blockedKeywords', ["test"]);
    let blockedUsers = GM_getValue('blockedUsers', ["test"]);

    // Add styles
    const style = `
        #popup-container {
            display: none;
            position: fixed;
            top: 20%;
            left: 50%;
            transform: translateX(-50%);
            background-color: white;
            border: 1px solid #ccc;
            padding: 20px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            z-index: 9999;
            width: 300px;
            height: 400px;
            overflow: auto;
        }
        #popup-container h2 {
            text-align: center;
        }
        #popup-container label {
            display: block;
            margin-top: 10px;
        }
        #popup-container textarea {
            width: 100%;
            height: 100px;
            margin-top: 5px;
        }
        #popup-container button {
            display: block;
            width: 100%;
            margin-top: 20px;
            padding: 0px;
            background-color: #4CAF50;
            color: white;
            border: none;
            cursor: pointer;
        }
        #popup-container button:hover {
            background-color: #45a049;
        }
        #open-popup-button {
            position: fixed;
            bottom: 10px;
            right: 10px;
            background-color: #4CAF50;
            color: white;
            border: none;
            padding: 0px 5px;
            cursor: pointer;
            font-size: 14px;
            border-radius: 5px;
            box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2);
            transition: background-color 0.3s;
        }
        #open-popup-button:hover {
            background-color: #45a049;
        }
    `;

    // Create the top-right button
    const openButton = document.createElement('button');
    openButton.id = 'open-popup-button';
    openButton.innerText = '⚙️';
    document.body.appendChild(openButton);

    // Button click event to show the popup menu
    openButton.addEventListener('click', () => {
        popupContainer.style.display = 'block';
    });

    const styleTag = document.createElement('style');
    styleTag.innerHTML = style;
    document.head.appendChild(styleTag);

    // Create the popup menu
    const popupContainer = document.createElement('div');
    popupContainer.id = 'popup-container';
    popupContainer.style.display = 'none';
    popupContainer.innerHTML = `
        <h2>Modify Keywords and Users</h2>
        <label for="keywords">Keywords:</label>
        <textarea id="keywords">${blockedKeywords.join("\n")}</textarea>

        <label for="users">Usernames:</label>
        <textarea id="users">${blockedUsers.join("\n")}</textarea>

        <button id="save-button">Save</button>
    `;
    document.body.appendChild(popupContainer);

    // Save button functionality
    document.getElementById('save-button').addEventListener('click', () => {
        const updatedKeywords = document.getElementById('keywords').value.split("\n").filter(k => k.trim() !== "");
        const updatedUsers = document.getElementById('users').value.split("\n").filter(u => u.trim() !== "");

        GM_setValue('blockedKeywords', updatedKeywords);
        GM_setValue('blockedUsers', updatedUsers);

        //alert("Changes saved successfully!");
        popupContainer.style.display = 'none';

        hideBlockedPosts(); // Immediately update filtering
    });

    // Listen for Ctrl + Z shortcut to open the popup menu
    window.addEventListener('keydown', (event) => {
        if (event.ctrlKey && event.key === 'z') {
            popupContainer.style.display = 'block';
        }
    });

    // Listen for Esc key to close the popup menu
    window.addEventListener('keydown', (event) => {
        if (event.key === 'Escape') { // Pressing Esc closes the menu
            popupContainer.style.display = 'none';
        }
    });

    function hideBlockedPosts() {
        let posts = document.querySelectorAll('shreddit-post');
        posts.forEach(post => {
            let titleText = post.querySelector('[id^="post-title"]').innerText.toLowerCase();
            let authorName = post.getAttribute('author');

            if (blockedKeywords.length > 0 && blockedKeywords.some(keyword => titleText.includes(keyword.toLowerCase()))) {
                post.style.display = 'none';
            }
            if (blockedUsers.length > 0 && blockedUsers.includes(authorName)) {
                post.style.display = 'none';
            }
        });
    }

    // Observe page changes to handle dynamically loaded posts
    const observer = new MutationObserver(hideBlockedPosts);
    observer.observe(document.body, { childList: true, subtree: true });

    // Run immediately to hide posts on initial page load
    hideBlockedPosts();
})();

QingJ © 2025

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