Guest User

Untitled

a guest
Jul 26th, 2025
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name         v2ex 关键词屏蔽(强制隐藏或移除)
  3. // @namespace    http://tampermonkey.net/
  4. // @version      0.4
  5. // @description  强制屏蔽标题中包含 “币” 或 “sol” 的帖子 —— 可选移除或 !important 隐藏。带日志辅助调试。忽略大小写。 。
  6. // @author       YourName
  7. // @match        https://www.v2ex.com/*
  8. // @grant        none
  9. // ==/UserScript==
  10.  
  11. (function() {
  12.     'use strict';
  13.  
  14.     const keywords = ['币', 'sol'];
  15.     const re = new RegExp(keywords.map(k => k.replace(/[-\/\\^$*+?.()|[\]{}]/g,'\\$&')).join('|'), 'i');
  16.  
  17.     function filterPosts() {
  18.         document.querySelectorAll('div[class="cell item"]').forEach((div, idx) => {
  19.             const link = div.querySelector('.item_title a.topic-link');
  20.             if (!link) {
  21.                 console.log(`[过滤][${idx}] 无标题链接`);
  22.                 return;
  23.             }
  24.             const text = link.textContent.trim();
  25.             const matched = re.test(text);
  26.             console.log(`[过滤][${idx}] "${text}" => 匹配: ${matched}`);
  27.             if (matched) {
  28.                 // —— 方案 A: 直接从 DOM 中移除 ——
  29. //                 div.remove();
  30. //                 console.log(`[过滤][${idx}] 已移除`);
  31.  
  32.                 // —— 方案 B: 强制添加 !important 隐藏 ——
  33.                 div.style.setProperty('display', 'none', 'important');
  34.                 console.log(`[过滤][${idx}] 已隐藏(!important)`);
  35.             }
  36.         });
  37.     }
  38.  
  39.     // 首次过滤
  40.     filterPosts();
  41.  
  42.     // 监听异步加载
  43.     new MutationObserver(muts => {
  44.         muts.forEach(m => {
  45.             if (m.addedNodes.length) {
  46.                 filterPosts();
  47.             }
  48.         });
  49.     }).observe(document.body, { childList: true, subtree: true });
  50. })();
  51.  
Add Comment
Please, Sign In to add comment