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-06 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Reddit Advanced Content Filter
  3. // @namespace https://greasyfork.org/en/users/567951-stuart-saddler
  4. // @version 2.0
  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. let filteredCount = 0;
  24. let menuCommand = null;
  25. let processedPosts = new WeakSet();
  26. let blocklistArray = [];
  27.  
  28. const CSS = `
  29. .content-filtered {
  30. display: none !important;
  31. height: 0 !important;
  32. overflow: hidden !important;
  33. }
  34. /* Updated CSS to match Bluesky's configuration dialog styles */
  35. .reddit-filter-dialog {
  36. position: fixed;
  37. top: 50%;
  38. left: 50%;
  39. transform: translate(-50%, -50%);
  40. background: white;
  41. padding: 20px;
  42. border-radius: 8px;
  43. z-index: 1000000;
  44. box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  45. min-width: 300px;
  46. max-width: 350px;
  47. font-family: Arial, sans-serif;
  48. color: #333;
  49. }
  50. .reddit-filter-dialog h2 {
  51. margin-top: 0;
  52. color: #0079d3;
  53. font-size: 1.5em;
  54. font-weight: bold;
  55. }
  56. .reddit-filter-dialog p {
  57. font-size: 0.9em;
  58. margin-bottom: 10px;
  59. color: #555;
  60. }
  61. .reddit-filter-dialog textarea {
  62. width: calc(100% - 16px); /* Ensures consistent padding */
  63. height: 150px; /* Adjusted height to match Bluesky's */
  64. padding: 8px;
  65. margin: 10px 0;
  66. border: 1px solid #ccc;
  67. border-radius: 4px;
  68. font-family: monospace;
  69. background: #f9f9f9;
  70. color: #000;
  71. resize: vertical;
  72. }
  73. .reddit-filter-dialog .button-container {
  74. display: flex;
  75. justify-content: flex-end;
  76. gap: 10px;
  77. margin-top: 10px;
  78. }
  79. .reddit-filter-dialog button {
  80. display: flex;
  81. align-items: center;
  82. justify-content: center;
  83. padding: 8px 16px;
  84. border: none;
  85. border-radius: 4px;
  86. cursor: pointer;
  87. font-size: 1em;
  88. text-align: center;
  89. }
  90. .reddit-filter-dialog .save-btn {
  91. background-color: #0079d3;
  92. color: white;
  93. }
  94. .reddit-filter-dialog .cancel-btn {
  95. background-color: #f2f2f2;
  96. color: #333;
  97. }
  98. .reddit-filter-dialog button:hover {
  99. opacity: 0.9;
  100. }
  101. .reddit-filter-overlay {
  102. position: fixed;
  103. top: 0;
  104. left: 0;
  105. right: 0;
  106. bottom: 0;
  107. background: rgba(0, 0, 0, 0.5);
  108. z-index: 999999;
  109. }
  110. `;
  111.  
  112. // Add CSS
  113. if (typeof GM_addStyle !== 'undefined') {
  114. GM_addStyle(CSS);
  115. } else {
  116. const style = document.createElement('style');
  117. style.textContent = CSS;
  118. document.head.appendChild(style);
  119. }
  120.  
  121. // Function to create and display a modal dialog for configuration
  122. async function showConfig() {
  123. const overlay = document.createElement('div');
  124. overlay.className = 'reddit-filter-overlay';
  125.  
  126. const dialog = document.createElement('div');
  127. dialog.className = 'reddit-filter-dialog';
  128. dialog.innerHTML = `
  129. <h2>Reddit Filter: Blocklist</h2>
  130. <p>Enter keywords or subreddit names one per line. Filtering is case-insensitive.</p>
  131. <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>
  132. <textarea spellcheck="false" id="blocklist">${blocklistArray.join('\n')}</textarea>
  133. <div class="button-container">
  134. <button class="cancel-btn">Cancel</button>
  135. <button class="save-btn">Save</button>
  136. </div>
  137. `;
  138.  
  139. document.body.appendChild(overlay);
  140. document.body.appendChild(dialog);
  141.  
  142. const closeDialog = () => {
  143. dialog.remove();
  144. overlay.remove();
  145. };
  146.  
  147. dialog.querySelector('.save-btn').addEventListener('click', async () => {
  148. const blocklistInput = dialog.querySelector('#blocklist').value;
  149.  
  150. blocklistArray = blocklistInput
  151. .split('\n')
  152. .map(item => item.trim().toLowerCase())
  153. .filter(item => item.length > 0);
  154.  
  155. await GM.setValue('blocklist', blocklistArray);
  156.  
  157. closeDialog();
  158. location.reload();
  159. });
  160.  
  161. dialog.querySelector('.cancel-btn').addEventListener('click', closeDialog);
  162. overlay.addEventListener('click', closeDialog);
  163. }
  164.  
  165. // Function to update menu commands with the current count of blocked items
  166. function updateCounter() {
  167. if (menuCommand !== null) {
  168. GM_unregisterMenuCommand(menuCommand);
  169. }
  170.  
  171. menuCommand = GM_registerMenuCommand(
  172. `Configure Blocklist (${filteredCount} blocked)`, // Updated to show blocked count
  173. showConfig
  174. );
  175. }
  176.  
  177. // Function to process and filter individual posts
  178. function processPost(post) {
  179. if (!post || processedPosts.has(post)) return;
  180. processedPosts.add(post);
  181.  
  182. let shouldHide = false;
  183.  
  184. // Check for blocked subreddits
  185. const subredditElement = post.querySelector('a[data-click-id="subreddit"], a.subreddit');
  186. if (subredditElement) {
  187. const subredditName = subredditElement.textContent.trim().replace(/^r\//i, '').toLowerCase();
  188. if (blocklistArray.includes(subredditName)) {
  189. shouldHide = true;
  190. }
  191. }
  192.  
  193. // Check for blocked keywords if not already hidden
  194. if (!shouldHide && blocklistArray.length > 0) {
  195. const postContent = post.textContent.toLowerCase();
  196. for (const keyword of blocklistArray) {
  197. const escapedKeyword = keyword.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
  198. const pattern = new RegExp(`\\b${escapedKeyword}(s|es|ies)?\\b`, 'i');
  199. if (pattern.test(postContent)) {
  200. shouldHide = true;
  201. break;
  202. }
  203. }
  204. }
  205.  
  206. if (shouldHide) {
  207. post.classList.add('content-filtered');
  208. const parentArticle = post.closest('article, div[data-testid="post-container"], shreddit-post');
  209. if (parentArticle) {
  210. parentArticle.classList.add('content-filtered');
  211. }
  212. filteredCount++;
  213. updateCounter();
  214. }
  215. }
  216.  
  217. // Initialization function
  218. async function init() {
  219. blocklistArray = (await GM.getValue('blocklist', [])).map(item => item.toLowerCase());
  220.  
  221. // Initialize menu commands
  222. updateCounter();
  223.  
  224. // Set up MutationObserver to handle dynamically loaded content
  225. const observer = new MutationObserver(mutations => {
  226. for (const mutation of mutations) {
  227. for (const node of mutation.addedNodes) {
  228. if (node.nodeType === Node.ELEMENT_NODE) {
  229. if (node.matches('article, div[data-testid="post-container"], shreddit-post')) {
  230. processPost(node);
  231. }
  232. node.querySelectorAll('article, div[data-testid="post-container"], shreddit-post')
  233. .forEach(processPost);
  234. }
  235. }
  236. }
  237. });
  238.  
  239. observer.observe(document.body, {
  240. childList: true,
  241. subtree: true
  242. });
  243.  
  244. // Initial processing of existing posts
  245. document.querySelectorAll('article, div[data-testid="post-container"], shreddit-post')
  246. .forEach(processPost);
  247. }
  248.  
  249. // Run initialization
  250. await init();
  251.  
  252. })();