Reddit Advanced Content Filter

Automatically hides posts and comments on Reddit based on keywords or subreddits you specify. Case-insensitive filtering supports plural forms. Perfect for curating your feed.

当前为 2024-12-10 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Reddit Advanced Content Filter
  3. // @namespace https://greasyfork.org/en/users/567951-stuart-saddler
  4. // @version 2.2
  5. // @description Automatically hides posts and comments on Reddit based on keywords or subreddits you specify. Case-insensitive filtering supports plural forms. Perfect for curating your feed.
  6. // @author Stuart Saddler
  7. // @license MY
  8. // @icon https://clipart-library.com/images_k/smoke-clipart-transparent/smoke-clipart-transparent-6.png
  9. // @supportURL https://greasyfork.org/en/users/567951-stuart-saddler
  10. // @match *://www.reddit.com/*
  11. // @match *://old.reddit.com/*
  12. // @run-at document-end
  13. // @grant GM.getValue
  14. // @grant GM.setValue
  15. // @grant GM_addStyle
  16. // @grant GM_registerMenuCommand
  17. // @grant GM_unregisterMenuCommand
  18. // ==/UserScript==
  19.  
  20. (async function() {
  21. 'use strict';
  22.  
  23. const postSelector = 'article, div[data-testid="post-container"], shreddit-post';
  24. let filteredCount = 0;
  25. let menuCommand = null;
  26. let processedPosts = new Set();
  27. let blocklistArray = [];
  28. let keywordPattern = null;
  29. let shortKeywords = new Set();
  30. let longKeywords = [];
  31.  
  32. const CSS = `
  33. .content-filtered { display: none !important; height: 0 !important; overflow: hidden !important; }
  34. .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; }
  35. .reddit-filter-dialog h2 { margin-top: 0; color: #0079d3; font-size: 1.5em; font-weight: bold; }
  36. .reddit-filter-dialog p { font-size: 0.9em; margin-bottom: 10px; color: #555; }
  37. .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; background: #f9f9f9; color: #000; resize: vertical; }
  38. .reddit-filter-dialog .button-container { display: flex; justify-content: flex-end; gap: 10px; margin-top: 10px; }
  39. .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; }
  40. .reddit-filter-dialog .save-btn { background-color: #0079d3; color: white; }
  41. .reddit-filter-dialog .cancel-btn { background-color: #f2f2f2; color: #333; }
  42. .reddit-filter-dialog button:hover { opacity: 0.9; }
  43. .reddit-filter-overlay { position: fixed; top: 0; left: 0; right: 0; bottom: 0; background: rgba(0, 0, 0, 0.5); z-index: 999999; }
  44. `;
  45.  
  46. if (typeof GM_addStyle !== 'undefined') {
  47. GM_addStyle(CSS);
  48. } else {
  49. const style = document.createElement('style');
  50. style.textContent = CSS;
  51. document.head.appendChild(style);
  52. }
  53.  
  54. const getKeywordPattern = (keywords) => {
  55. const escapedKeywords = keywords.map(k => k.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')).join('|');
  56. return new RegExp(`\\b(${escapedKeywords})(s|es|ies)?\\b`, 'i');
  57. };
  58.  
  59. const cleanupProcessedPosts = () => {
  60. if (processedPosts.size > 10000) {
  61. processedPosts.clear();
  62. }
  63. };
  64.  
  65. setInterval(cleanupProcessedPosts, 300000);
  66.  
  67. async function showConfig() {
  68. const overlay = document.createElement('div');
  69. overlay.className = 'reddit-filter-overlay';
  70. const dialog = document.createElement('div');
  71. dialog.className = 'reddit-filter-dialog';
  72. dialog.innerHTML = `
  73. <h2>Reddit Filter: Blocklist</h2>
  74. <p>Enter keywords or subreddit names one per line. Filtering is case-insensitive.</p>
  75. <p><em>Keywords can match common plural forms (e.g., "apple" blocks "apples"). Irregular plurals (e.g., "mouse" and "mice") must be added separately. Subreddit names should be entered without the "r/" prefix (e.g., "subredditname").</em></p>
  76. <textarea spellcheck="false" id="blocklist">${blocklistArray.join('\n')}</textarea>
  77. <div class="button-container">
  78. <button class="cancel-btn">Cancel</button>
  79. <button class="save-btn">Save</button>
  80. </div>
  81. `;
  82.  
  83. document.body.appendChild(overlay);
  84. document.body.appendChild(dialog);
  85.  
  86. const closeDialog = () => {
  87. dialog.remove();
  88. overlay.remove();
  89. };
  90.  
  91. dialog.querySelector('.save-btn').addEventListener('click', async () => {
  92. const blocklistInput = dialog.querySelector('#blocklist').value;
  93. blocklistArray = blocklistInput
  94. .split('\n')
  95. .map(item => item.trim().toLowerCase())
  96. .filter(item => item.length > 0);
  97.  
  98. shortKeywords = new Set(blocklistArray.filter(k => k.length < 4));
  99. longKeywords = blocklistArray.filter(k => k.length >= 4);
  100. keywordPattern = getKeywordPattern(longKeywords);
  101.  
  102. await GM.setValue('blocklist', blocklistArray);
  103. closeDialog();
  104. location.reload();
  105. });
  106.  
  107. dialog.querySelector('.cancel-btn').addEventListener('click', closeDialog);
  108. overlay.addEventListener('click', closeDialog);
  109. }
  110.  
  111. function createFallbackButton() {
  112. const button = document.createElement('button');
  113. button.innerHTML = `Configure Blocklist (${filteredCount} blocked)`;
  114. button.style.cssText = 'position:fixed;top:10px;right:10px;z-index:999999;padding:8px;';
  115. button.addEventListener('click', showConfig);
  116. document.body.appendChild(button);
  117. }
  118.  
  119. function updateCounter() {
  120. if (typeof GM_registerMenuCommand !== 'undefined') {
  121. if (menuCommand !== null) {
  122. GM_unregisterMenuCommand(menuCommand);
  123. }
  124. menuCommand = GM_registerMenuCommand(
  125. `Configure Blocklist (${filteredCount} blocked)`,
  126. showConfig
  127. );
  128. } else {
  129. createFallbackButton();
  130. }
  131. }
  132.  
  133. async function processPostsBatch(posts) {
  134. const batchSize = 10;
  135. for (let i = 0; i < posts.length; i += batchSize) {
  136. const batch = posts.slice(i, i + batchSize);
  137. await new Promise(resolve => requestIdleCallback(resolve, { timeout: 1000 }));
  138. batch.forEach(post => processPost(post));
  139. }
  140. }
  141.  
  142. function processPost(post) {
  143. if (!post || processedPosts.has(post)) return;
  144. processedPosts.add(post);
  145.  
  146. let shouldHide = false;
  147. const subredditElement = post.getElementsByClassName('subreddit')[0] ||
  148. post.querySelector('a[data-click-id="subreddit"]');
  149.  
  150. if (subredditElement) {
  151. const subredditName = subredditElement.textContent.trim().replace(/^r\//i, '').toLowerCase();
  152. if (blocklistArray.includes(subredditName)) {
  153. shouldHide = true;
  154. }
  155. }
  156.  
  157. if (!shouldHide && (shortKeywords.size > 0 || longKeywords.length > 0)) {
  158. const postContent = post.textContent.toLowerCase();
  159. shouldHide = Array.from(shortKeywords).some(keyword => postContent.includes(keyword)) ||
  160. (longKeywords.length > 0 && keywordPattern.test(postContent));
  161. }
  162.  
  163. if (shouldHide) {
  164. requestIdleCallback(() => {
  165. post.classList.add('content-filtered');
  166. const parentArticle = post.closest(postSelector);
  167. if (parentArticle) {
  168. parentArticle.classList.add('content-filtered');
  169. }
  170. filteredCount++;
  171. updateCounter();
  172. }, { timeout: 1000 });
  173. }
  174. }
  175.  
  176. const debouncedUpdate = debounce((posts) => {
  177. processPostsBatch(Array.from(posts));
  178. }, 100);
  179.  
  180. function debounce(func, wait) {
  181. let timeout;
  182. return (...args) => {
  183. clearTimeout(timeout);
  184. timeout = setTimeout(() => {
  185. timeout = null;
  186. func.apply(this, args);
  187. }, wait);
  188. };
  189. }
  190.  
  191. async function init() {
  192. blocklistArray = (await GM.getValue('blocklist', [])).map(item => item.toLowerCase());
  193. shortKeywords = new Set(blocklistArray.filter(k => k.length < 4));
  194. longKeywords = blocklistArray.filter(k => k.length >= 4);
  195. keywordPattern = getKeywordPattern(longKeywords);
  196.  
  197. updateCounter();
  198.  
  199. const observerTarget = document.getElementById('main-content') || document.body;
  200. const observer = new MutationObserver(mutations => {
  201. const newPosts = new Set();
  202. mutations.forEach(mutation => {
  203. mutation.addedNodes.forEach(node => {
  204. if (node.nodeType === Node.ELEMENT_NODE) {
  205. if (node.matches?.(postSelector)) {
  206. newPosts.add(node);
  207. }
  208. node.querySelectorAll?.(postSelector).forEach(post => newPosts.add(post));
  209. }
  210. });
  211. });
  212. if (newPosts.size > 0) {
  213. debouncedUpdate(newPosts);
  214. }
  215. });
  216.  
  217. observer.observe(observerTarget, { childList: true, subtree: true });
  218.  
  219. const initialPosts = document.getElementsByClassName('thing') ||
  220. document.querySelectorAll(postSelector);
  221. if (initialPosts.length > 0) {
  222. debouncedUpdate(initialPosts);
  223. }
  224. }
  225.  
  226. await init();
  227. })();