Automatically hides posts and comments on Reddit based on keywords or subreddits you specify
目前為
// ==UserScript==
// @name Reddit Advanced Content Filter
// @namespace https://greasyfork.org/en/users/567951-stuart-saddler
// @version 2.1
// @description Automatically hides posts and comments on Reddit based on keywords or subreddits you specify
// @author Stuart Saddler
// @license MY
// @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';
const postSelector = 'article, div[data-testid="post-container"], shreddit-post';
let filteredCount = 0;
let menuCommand = null;
let processedPosts = new WeakSet();
let blocklistArray = [];
let keywordPattern = null;
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;
}
.reddit-filter-overlay {
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: rgba(0, 0, 0, 0.5);
z-index: 999999;
}
.reddit-filter-dialog textarea {
width: calc(100% - 16px);
height: 150px;
padding: 8px;
margin: 10px 0;
border: 1px solid #ccc;
border-radius: 4px;
font-family: monospace;
resize: vertical;
}
.reddit-filter-dialog .button-container {
display: flex;
justify-content: flex-end;
gap: 10px;
margin-top: 10px;
}
.reddit-filter-dialog button {
padding: 8px 16px;
border: none;
border-radius: 4px;
cursor: pointer;
}
.reddit-filter-dialog .save-btn {
background-color: #0079d3;
color: white;
}
.reddit-filter-dialog .cancel-btn {
background-color: #f2f2f2;
color: #333;
}
`;
if (typeof GM_addStyle !== 'undefined') {
GM_addStyle(CSS);
} else {
const style = document.createElement('style');
style.textContent = CSS;
document.head.appendChild(style);
}
const getKeywordPattern = (keywords) => {
const escapedKeywords = keywords.map(k =>
k.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
).join('|');
return new RegExp(`\\b(${escapedKeywords})(s|es|ies)?\\b`, 'i');
};
async function showConfig() {
const overlay = document.createElement('div');
overlay.className = 'reddit-filter-overlay';
const dialog = document.createElement('div');
dialog.className = 'reddit-filter-dialog';
const fragment = document.createDocumentFragment();
dialog.innerHTML = `
<h2>Reddit Filter: Blocklist</h2>
<p>Enter keywords or subreddit names one per line. Filtering is case-insensitive.</p>
<textarea spellcheck="false" id="blocklist">${blocklistArray.join('\n')}</textarea>
<div class="button-container">
<button class="cancel-btn">Cancel</button>
<button class="save-btn">Save</button>
</div>
`;
fragment.appendChild(dialog);
document.body.appendChild(overlay);
document.body.appendChild(fragment);
const closeDialog = () => {
dialog.remove();
overlay.remove();
};
dialog.querySelector('.save-btn').addEventListener('click', async () => {
const blocklistInput = dialog.querySelector('#blocklist').value;
blocklistArray = blocklistInput
.split('\n')
.map(item => item.trim().toLowerCase())
.filter(item => item.length > 0);
keywordPattern = getKeywordPattern(blocklistArray);
await GM.setValue('blocklist', blocklistArray);
closeDialog();
location.reload();
});
dialog.querySelector('.cancel-btn').addEventListener('click', closeDialog);
overlay.addEventListener('click', closeDialog);
}
function createFallbackButton() {
const button = document.createElement('button');
button.innerHTML = `Configure Blocklist (${filteredCount} blocked)`;
button.style.cssText = 'position:fixed;top:10px;right:10px;z-index:999999;padding:8px;';
button.addEventListener('click', showConfig);
document.body.appendChild(button);
}
function updateCounter() {
if (typeof GM_registerMenuCommand !== 'undefined') {
if (menuCommand !== null) {
GM_unregisterMenuCommand(menuCommand);
}
menuCommand = GM_registerMenuCommand(
`Configure Blocklist (${filteredCount} blocked)`,
showConfig
);
} else {
createFallbackButton();
}
}
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 => requestAnimationFrame(resolve));
batch.forEach(post => processPost(post));
}
}
function processPost(post) {
if (!post || processedPosts.has(post)) return;
processedPosts.add(post);
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();
if (blocklistArray.includes(subredditName)) {
shouldHide = true;
}
}
if (!shouldHide && blocklistArray.length > 0) {
const postContent = post.textContent.toLowerCase();
shouldHide = keywordPattern.test(postContent);
}
if (shouldHide) {
requestAnimationFrame(() => {
post.classList.add('content-filtered');
const parentArticle = post.closest(postSelector);
if (parentArticle) {
parentArticle.classList.add('content-filtered');
}
filteredCount++;
updateCounter();
});
}
}
const debouncedUpdate = debounce((posts) => {
processPostsBatch(Array.from(posts));
}, 100);
function debounce(func, wait) {
let timeout;
return (...args) => {
clearTimeout(timeout);
timeout = setTimeout(() => {
timeout = null;
func.apply(this, args);
}, wait);
};
}
async function init() {
blocklistArray = (await GM.getValue('blocklist', [])).map(item => item.toLowerCase());
keywordPattern = getKeywordPattern(blocklistArray);
updateCounter();
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);
}
}
await init();
})();