// ==UserScript== // @name Reddit Lite去ad // @namespace https://reddit.com/ // @version 4.0 // @description 去AD,极简,不影响页面加载 // @author Glyn 极简优化 // @match https://www.reddit.com/* // @grant none // @license MIT // @run-at document-start // ==/UserScript== (function() { 'use strict'; const AD_SEL = ` shreddit-comments-page-ad, shreddit-ad-post, div[data-testid="adpost"], div[data-testid*="promoted"], div[data-adclicklocation], div[id^="ad_"], faceplate-tracker[thing-type="ad_post"], shreddit-async-loader[bundlename*="ad"]:not([bundlename*="comment"]):not([bundlename*="reply"]):not([bundlename*="body_header"]) `; let timer, observer; document.documentElement.appendChild(document.createElement('style')).textContent = `${AD_SEL}{display:none!important}`; const cleanAds = (mutations) => { (window.requestIdleCallback || setTimeout)(() => { document.querySelectorAll(AD_SEL).forEach(el => el.remove()); mutations?.forEach(m => m.addedNodes.forEach(n => { if (n.nodeType !== 1) return; n.querySelectorAll('span,a').forEach(el => { el.textContent.trim().toLowerCase() === 'promoted' && el.closest('shreddit-post, div[data-testid="post-container"]')?.remove(); }); })); }, { timeout: 200 }); }; // 3. 优化监听:防抖+仅监听必要变化,无高频触发 const initObserver = () => observer = new MutationObserver((m) => { clearTimeout(timer); timer = setTimeout(() => cleanAds(m), 80); }).observe(document.body, { childList: true, subtree: true }); // 4. 生命周期优化:不抢占首屏加载,后台自动降载 document.addEventListener('DOMContentLoaded', () => { cleanAds(); initObserver(); }); document.addEventListener('visibilitychange', () => document.hidden ? observer?.disconnect() : initObserver()); })();