// ==UserScript== // @name NoAd去广告(纯净版 · 装好即用) // @namespace 鉴紫 // @version 2.1 // @description NoAd去广告:无设置、装好即用的去广告脚本,国内外全站(所有 http/https 页面)均显示 NoAd 按钮并自动屏蔽广告。内置分站广告位隐藏 + 通用兜底广告选择器 + 主流广告联盟请求拦截(页面世界 fetch/XHR/WebSocket/src 尽力拦截)+ 保守的侵入式弹窗/浮层清理 + 搜索结果广告强力屏蔽 + 悬浮窗框选屏蔽。仅屏蔽广告,不动正常阅读内容。无远程依赖、不自动下载、不全局误杀。 // @match *://*/* // @exclude-match *://localhost/* // @exclude-match *://127.0.0.1/* // 说明:*://*/* 已覆盖所有 http/https 页面(含国内外各类站点),脚本在任意网站均显示 NoAd 按钮并自动屏蔽广告。 // @run-at document-start // @grant GM_addStyle // @grant GM_setValue // @grant GM_getValue // @grant GM_xmlhttpRequest // @license MIT // ==/UserScript== (function () { 'use strict'; /* ========================================================= * 云端规则库(社区共享) * 后端:WorkBuddy Cloud Service(鉴紫去广告应用)。仅用 publishableKey 公开调用, * 表 ad_rules 配置为「匿名公开读 + 匿名插入」(无更新/删除,防滥用)。 * 用户脚本通过 Tampermonkey 的 GM_xmlhttpRequest 调用(绕过浏览器 CORS)。 * ========================================================= */ const CLOUD_ENDPOINT = 'https://jianzi-noad.app.workbuddy.host'; const CLOUD_KEY = 'wbpk_c9lDzSeyRu77eIH7mwbvmL_SaUHQDB7HRqrbpptGMamcfQW60YO1Amx'; let syncStatusEl = null; // 注册域名(站点级):去掉子域名,使同站不同子域/不同页面共享规则 const MULTI_TLD = new Set(['com.cn','net.cn','org.cn','co.cn','com.hk','com.tw','com.my','co.uk','org.uk','com.au','co.jp','com.br','com.sg','ac.uk']); function regDomainOf(hostname) { const h = (hostname || '').toLowerCase(); if (!h || h === 'localhost' || /^[\d.]+$/.test(h)) return h; const p = h.split('.'); if (p.length <= 2) return h; if (MULTI_TLD.has(p.slice(-2).join('.'))) return p.slice(-3).join('.'); return p.slice(-2).join('.'); } function currentSite() { return regDomainOf(location.hostname); } function cloudRequest(method, path, body, cb) { GM_xmlhttpRequest({ method: method, url: CLOUD_ENDPOINT + '/.cloud/database/rest/' + path, headers: { 'Content-Type': 'application/json', 'x-wb-webapp-access-key': CLOUD_KEY, 'Prefer': 'return=representation' }, data: body ? JSON.stringify(body) : undefined, onload: function (res) { let data = null; try { data = res.responseText ? JSON.parse(res.responseText) : null; } catch (e) {} cb(res.status, data); }, onerror: function () { cb(0, null); } }); } function syncStatus(msg) { if (syncStatusEl) syncStatusEl.textContent = msg; } // 上传:把本域名下你手动框选的所有选择器推到云端(每条贡献独立成行,合并去重由拉取方处理) function uploadRules() { if (!customSels.length) { syncStatus('你还没有框选任何广告,无规则可上传'); return; } const payload = { site: currentSite(), selectors: customSels, author: null, note: null }; cloudRequest('POST', 'ad_rules', payload, function (status) { if (status >= 200 && status < 300) syncStatus('已上传 ' + customSels.length + ' 条规则到云端 ✔'); else syncStatus('上传失败(HTTP ' + status + '),请检查网络或稍后重试'); }); } // 拉取:取该域名所有社区贡献,合并去重后应用到本页并存入本地(之后访问自动生效) function pullRules() { const site = currentSite(); cloudRequest('GET', 'ad_rules?site=eq.' + encodeURIComponent(site) + '&select=selectors', null, function (status, rows) { if (status === 0) { syncStatus('同步失败:网络或云端不可用'); return; } if (!Array.isArray(rows)) { syncStatus('同步失败(HTTP ' + status + ')'); return; } const merged = new Set(); rows.forEach(r => { (r.selectors || []).forEach(s => merged.add(s)); }); const arr = Array.from(merged); let applied = 0; arr.forEach(sel => { if (customSels.indexOf(sel) < 0) { customSels.push(sel); for (const el of $all(sel)) { if (hide(el)) applied++; } } }); try { GM_setValue(CUSTOM_KEY(), JSON.stringify(customSels)); } catch (e) {} syncStatus('已同步 ' + arr.length + ' 条规则,本页生效 ' + applied + ' 处 ✔'); updateStatus(); }); } // 自动拉取:安装后首次访问某站点时静默同步一次(1 小时内不重复),让社区规则开箱即用 function autoPull() { const site = currentSite(); const lastKey = 'agb_lastpull_' + site; let last = 0; try { last = parseInt(GM_getValue(lastKey, '0'), 10) || 0; } catch (e) {} const now = Date.now(); if (now - last < 3600 * 1000) return; try { GM_setValue(lastKey, String(now)); } catch (e) {} pullRules(); } /* ========================================================= * 设计说明 * 1. 装好即用:无设置面板、无菜单、无远程依赖,启用即工作。 * 2. 三层防护: * a) 分站元素隐藏(CSS 注入 + 动态兜底)—— 各站已知广告位。 * b) 广告联盟请求拦截(页面世界补丁)—— 拦掉动态注入的广告请求。 * c) 保守的侵入式弹窗/浮层清理——只对有强广告信号的浮层动手, * 绝不误伤正常弹窗(登录框、图片灯箱、播放器全屏等)。 * 3. 安全护栏:凡内部含有真实结果链接(h3 a)或 video 的容器,任何规则 * 都不得隐藏,从根上杜绝“整列结果被清空”这类误杀。 * ========================================================= */ /* ---------------- 轻量 DOM 工具(替代 jQuery) ---------------- */ const $all = (sel, root) => { try { return Array.from((root || document).querySelectorAll(sel)); } catch (e) { return []; } }; // 安全护栏:含真实内容链接或视频的容器不得被隐藏 function isProtected(el) { if (!el) return false; if (el.querySelector && el.querySelector('h3 a, .c-title a, a[href^="http"]')) { // 仅当该容器本身就是一个结果块(有标题链接)时保护 if (el.matches && (el.matches('#content_left > div') || el.querySelector('h3'))) return true; } return false; } // 累计屏蔽计数(用于悬浮窗展示“本页累计屏蔽 N 处”) let blockedCount = 0, lastGlobal = 0; function hide(el, force) { if (!el || !el.style || el.dataset.removed) return false; if (el.id && el.id.indexOf('agb-') === 0) return false; // 硬护栏:永不隐藏自己的 UI 控件(FAB/面板/高亮框) // 护栏:所有隐藏操作(自动规则/弹窗清理/框选)都不得隐藏“正常结果块”或“页面主体内容”, // 从根上杜绝“整列结果被清空 / 整页变白”。force 仅在用户框选且已通过主体校验时使用。 if (!force && (isProtected(el) || isMainContent(el))) return false; el.dataset.removed = '1'; el.style.setProperty('display', 'none', 'important'); blockedCount++; // 每实际隐藏一个元素计一次(dataset.removed 去重,不重复计数) if (statusEl) updateStatus(); return true; } function hideAll(sel, root) { for (const el of $all(sel, root)) hide(el); } function containsAdText(root, words) { const leaves = root.querySelectorAll('span, div, a, p'); for (const n of leaves) { if (n.children.length > 0) continue; // 只取纯文字叶子 const t = (n.textContent || '').trim(); if (words.includes(t)) return true; } return false; } // 判断元素是否为“页面主体内容”,防止误伤导致整页空白 let bodyTextLen = -1; // 缓存页面文字总量,cleanup 开头刷新,避免在每次 hide 时重复遍历整页文本 function isMainContent(el) { if (!el) return false; if (bodyTextLen < 0) bodyTextLen = (document.body && document.body.textContent || '').length; const bt = bodyTextLen; const et = (el.textContent || '').length; if (bt > 200 && et / bt > 0.5) return true; // 含过半页面文字 if (el.parentElement === document.body) { const id = (el.id || '').toLowerCase(); const cls = (el.className || '').toLowerCase(); const generic = ['content', 'main', 'article', 'app', 'root', 'wrapper', 'container', 'page', 'layout', 'doc', 'post', 'read', 'body']; if (generic.some(g => id === g || (' ' + cls + ' ').indexOf(' ' + g + ' ') >= 0) && !/(ad|advert|promo|sponsor|banner|popup|float)/.test(id + cls)) return true; } return false; } // 轻量提示 function agbToast(msg) { let t = document.getElementById('agb-toast'); if (!t) { t = document.createElement('div'); t.id = 'agb-toast'; t.style.cssText = 'position:fixed;left:50%;top:18px;transform:translateX(-50%);z-index:2147483647;background:rgba(0,0,0,.82);color:#fff;padding:8px 14px;border-radius:8px;font:13px system-ui,"Microsoft YaHei",sans-serif;box-shadow:0 4px 14px rgba(0,0,0,.3);pointer-events:none;transition:opacity .3s;'; document.body.appendChild(t); } t.textContent = msg; t.style.opacity = '1'; clearTimeout(t.__t); t.__t = setTimeout(() => { t.style.opacity = '0'; }, 2400); } /* ---------------- 主流广告联盟域名(请求层拦截) ---------------- */ const BLOCK_DOMAINS = [ // 百度联盟 'pos.baidu.com', 'cpro.baidu.com', 'eclick.baidu.com', 'c.baidu.com', 'hm.baidu.com', // Google AdSense / DoubleClick 'pagead2.googlesyndication.com', 'googlesyndication.com', 'doubleclick.net', 'adservice.google.com', 'googleadservices.com', 'ad.doubleclick.net', // 腾讯广点通 'gdt.qq.com', 'ad.qq.com', 'a.gdt.qq.com', 'v.gdt.qq.com', 't.gdt.qq.com', // 阿里妈妈 / 淘联盟 'alimama.com', 'tanx.com', 'ad.alimama.com', 'm.alimama.com', // 字节穿山甲 / 巨量引擎 'ad.toutiao.com', 'ad.oceanengine.com', 'is.snssdk.com', 'csjplatform.com', // 快手联盟 'ad.kuaishou.com', 'neptune.kuaishou.com', // 360 'union.360.cn', 'ad.360.cn', // 新浪 'sax.sina.com.cn', 'sax.sina.com', 'dmp.sina.com.cn', 'fyd.sina.com.cn', 'vipr.sina.com.cn', 'mark.sina.com.cn', 'us.sina.com.cn', // 搜狐 / 其它 'ad.sohu.com', 'bat.bing.com' ]; /* ---------------- 程序化广告的通用元素(全站生效) ---------------- */ const GLOBAL_HIDE = [ 'ins.adsbygoogle', // Google AdSense 广告单元 'iframe[src*="googlesyndication.com"]', 'iframe[src*="doubleclick.net"]', 'iframe[src*="pos.baidu.com"]', 'iframe[src*="cpro.baidu.com"]', 'iframe[src*="eclick.baidu.com"]', 'iframe[src*="gdt.qq.com"]', 'iframe[src*="alimama.com"]', 'iframe[src*="tanx.com"]', 'iframe[src*="oceanengine.com"]', 'iframe[src*="snssdk.com"]', 'iframe[src*="dmp.sina.com.cn"]', 'iframe[src*="fyd.sina.com.cn"]', 'iframe[src*="vipr.sina.com.cn"]', 'iframe[src*="mark.sina.com.cn"]', 'iframe[src*="sax.sina.com"]' ]; const GLOBAL_HIDE_CSS = 'html:not(.agb-disabled) ' + GLOBAL_HIDE.join(',') + '{display:none!important;}'; // 按站“不屏蔽本站”开关:禁用时在 上加 .agb-disabled,使上面的全局广告 CSS 失效,并跳过所有屏蔽逻辑。 // 站点级:作用于整个网站(含子域名),与规则共享范围一致。 const DISABLE_KEY = 'agb_disabled'; function getDisabledSet() { try { const arr = JSON.parse(GM_getValue(DISABLE_KEY, '[]')) || []; return new Set(arr.map(h => regDomainOf(h))); // 兼容旧版以完整 host 存储的条目 } catch (e) { return new Set(); } } function isSiteDisabled() { return getDisabledSet().has(currentSite()); } function setSiteDisabled(on) { const s = getDisabledSet(); if (on) s.add(currentSite()); else s.delete(currentSite()); try { GM_setValue(DISABLE_KEY, JSON.stringify([...s])); } catch (e) {} } function applyDisableState(on) { const de = document.documentElement; if (on) { de.classList.add('agb-disabled'); for (const el of document.querySelectorAll('[data-removed]')) { delete el.dataset.removed; el.style.setProperty('display', ''); } if (customStyle) customStyle.textContent = ''; blockedCount = 0; } else { de.classList.remove('agb-disabled'); try { cleanup(); } catch (e) {} try { recountGlobal(); } catch (e) {} } updateStatus(); } /* ---------------- 分站规则 ---------------- * remove : 静态广告位选择器(CSS 注入 + 动态兜底) * adText : 结果块内出现这些“纯文字叶子”就整块隐藏(推广标注) * feeds : 用于 adText 扫描的容器 * close : 要点击关闭的按钮(如强制登录弹窗的关闭键) * kill : 直接隐藏的弹窗/遮罩容器 * tweak : 站点专属微调(外链直跳等) */ const RULES = { baidu: { match: h => h.includes('baidu.com'), remove: [ '#top-ad', '[cmatchid]', '#con-ar', '.ec-pc_mat_c_banner', '.ec-pc_mat_c_banner__cc_banner_background_b', '.san-card[tpl="feed-ad"]', '.san-card[data-tpl="feed-ad"]', '.c-ad', '.result-op[data-isad]', '#content_left > div[data-isad]' ], adText: ['广告'], feeds: '#content_left > div', search: { items: '#content_left > div', adText: ['广告'], blocks: [ '#content_left > div[data-isad]', '#content_left > div[data-isad="1"]', '.ec-tuiguang', '[cmatchid]', '.c-ad', '#content_left > div .c-ad', '.result-op[data-isad]', '.ec-pc_mat_c_banner' ] }, // 百度知道 / 贴吧 / 文库 / 百科 的广告位 extra: [ '.wgt-ads', '#qb-ad', '.answer-ad', // 知道 '#pagelet_ad', '.ad-banner', '.icon-hot-ad', // 贴吧 '.reader-ad', '.ad-wrap', // 文库 '#side-ad', '.ad-img', '#ad-pic' // 百科 ] }, google: { match: h => h.includes('google.'), remove: [ '.ads-ad', '#tads', '#tads + div', '.commercial-unit', '[aria-label="Ads"]', '.pla-unit', '#rhs .commercial-unit', '.cu-container', '.ad-slot', 'ins.adsbygoogle' ], adText: ['广告', 'Ad', 'Sponsored'], feeds: 'div.g, div.MjjYud, div.tF2Cxc', search: { items: 'div.g, div.MjjYud, div.tF2Cxc', adText: ['Ad', 'Sponsored', '广告'], blocks: [ '#tads', '#tadsb', '[aria-label="Ads"]', 'div[aria-label="Sponsored"]', 'div[data-text-ad]', '.ads-ad', '.commercial-unit', '.cu-container', '.pla-unit' ] } }, bing: { match: h => h.includes('bing.com'), remove: ['.b_ad', '#b_results .b_ad', '.b_rcrumbs', '#b_pole', '.b_nation', 'div[data-uniq="ads"]'], adText: ['广告', 'Ad'], feeds: '#b_results > li.b_algo, #b_results > div.b_algo', search: { items: '#b_results > li, #b_results > div', adText: ['Ad', '广告'], blocks: ['.b_ad', '#b_results .b_ad', '.b_ad_last', 'div[data-uniq="ads"]'] } }, so360: { match: h => h.includes('so.com'), remove: ['.ec-ad', '#e_ads', '.result-ad', '.ad-result', '[data-isad="1"]', '.news-ad', '.ad-news'], adText: ['广告'], feeds: '#container > .result, #container > li, #main > .res-list > li', search: { items: '#container > .result, #container > li, #main > .res-list > li, .res-list li', adText: ['广告'], blocks: ['.ec-ad', '#e_ads', '[data-isad="1"]', '.result-ad', '.ad-result', '.news-ad', '.ad-news'] } }, sogou: { match: h => h.includes('sogou.com'), remove: ['.ad-link', '#ad_block', '.vrwrap-ad', '[data-ad="1"]', '.spread', '.spread-ad', '.ad-res'], adText: ['广告'], feeds: '#wrapper > #main > .results > .vrwrap, #content > .rb', search: { items: '#wrapper > #main > .results > .vrwrap, #content > .rb, .results .vrwrap', adText: ['广告'], blocks: ['.ad-link', '#ad_block', '[data-ad="1"]', '.vrwrap-ad', '.ad-res', '.spread', '.spread-ad'] } }, zhihu: { match: h => h.includes('zhihu.com'), remove: ['.Pc-feedAd', '.Pc-card', '.Pc-word', '.Banner-adsense', '.GlobalSideBar .Commercial', '.TopstorySideBar .Pc-card', '.Recommendation-MainColumn .Pc-feedAd'], adText: ['广告'], feeds: '.TopstoryItem, .ContentItem, .FeedCard', close: ['.signFlowModal .Modal-closeButton', '.Modal-wrapper .Modal-closeButton'], kill: ['.signFlowModal-container', '.Modal-mask', '.login-mask'], tweak: () => { const p = document.querySelector('p.link'); if (location.href.includes('link.zhihu.com/?target=') && p && p.textContent) { location.href = p.textContent; } } }, douban: { match: h => h.includes('douban.com'), remove: ['div[ad-status="appended"]', '.ad-banner', '.promo', '.sponsor', '[id^="dale_"]', '.article-ad'], adText: ['广告'], feeds: '.article, .mod, .topic-list li, .subject .related-info' }, bilibili: { match: h => h.includes('bilibili.com'), remove: ['.bili-video-card[data-ad="1"]', '.ad-card', '.home-top-banner', '.bili-ad', 'a[href*="cm.bilibili.com/cm/api/fees"]'], feeds: '.bili-video-card, .bili-live-card' }, weibo: { match: h => h.includes('weibo.com'), remove: ['.WB_feed_vipcover', '.send-interaction .ad', '.card-ad', '[data-ad="1"]', '.WB_feed_ads'], adText: ['广告', '推广'], feeds: '.WB_feed_expand, .card-wrap, .Feed_card' }, csdn: { match: h => h.includes('csdn.net'), remove: ['.recommend-ad', '.ad-box', '.blog-content-ad', '.template-box-ad', '#asideFooter', '.csdn-ad', '.ad-top'], adText: ['广告'], feeds: '.article-list, .blog-content, .recommend-item', tweak: csdnAutoReadmore }, toutiao: { match: h => h.includes('toutiao.com'), remove: ['.ad-label', '.article-ad', '.feed-ad', '[data-ad-article]', '.ad-float', '.tt-ad'], adText: ['广告', '推广'], feeds: '.article-content, .feed-card, .news-feed' }, qq: { match: h => h.includes('qq.com'), remove: ['.ad-box', '#ad-box', '.mod-ad', '.text-ad', '.tuijian-ad', '.article-ad', '#adwrap', '.ad-pic', '.qqcom-ad'], adText: ['广告'], feeds: '.mod, .article, .content-article' }, sina: { match: h => h.includes('sina.com.cn'), remove: ['.sina-ad', '#ad_article', '.blk_ad', '.ad_01', '.ad-none', '#ad_content', '.sina_float', '[class*="sina_float"]', '.sinaad', '#sinaad', '[class*="float_ad"]', '#float_ad', '.float-ad', '[class*="suspend"]', '[class*="float"]', '[id*="float_ad"]'], adText: ['广告'], feeds: '.article, .blk, #artibody', kill: ['#float_ad', '.sina_float', '[class*="sina_float"]', '[class*="float_ad"]', '#suspend_box', '.suspend-box', '[class*="suspend_ad"]'] }, netease: { match: h => h.includes('163.com'), remove: ['.ad-article', '.post_ad', '#endAd', '.ntes-live-ad', '.ns-ad', '.ad-sponsorship'], adText: ['广告'], feeds: '.post_content, .post_body, #endText' }, juejin: { match: h => h.includes('juejin.cn'), remove: ['.article-ad', '.sidebar-block-ad', '[data-ad]', '.ad-block'], adText: ['广告'], feeds: '.article-area, .entry-content' }, oschina: { match: h => h.includes('oschina.net'), remove: ['.ad-wrap', '.blog-ad', '.article-ad', '.recommend-ad'], adText: ['广告'], feeds: '.blog-content, .article-detail' }, hao123: { match: h => h.includes('hao123.com'), remove: ['.ad-box', '#ad-box', '.ads', '.float-ad', '.ad-area', '.ad-wrap', '.gg', '#gg', '.top-ad', '.bottom-ad'] }, sohu: { match: h => h.includes('sohu.com'), remove: ['.ad-banner', '.ad-wrap', '.sohu-ad', '#sohu_ad', '.article-ad', '.sidebar-ad', '.right-ad', '.left-ad', '.focus-ad', '.text-ad', '.gg', '#gg', '.float-ad', '.ad-float', '[class*="sohu_ad"]', '[class*="s_ad"]', '[id*="ad_"]'], adText: ['广告'], feeds: '.article, .article-content, .main-news, .news-list' }, ifeng: { match: h => h.includes('ifeng.com'), remove: ['.ad-banner', '#ad-banner', '.ad-box', '#ad-box', '.side-ad', '.right-ad', '.left-ad', '.focus-ad', '.news-ad', '.article-ad', '.text-ad', '.float-ad', '.gg', '#gg', '[class*="ifeng_ad"]', '[id*="ifeng_ad"]', '[class*="ifengAd"]'], adText: ['广告'], feeds: '.article, .article_content, .news_list, .main_content, .news-list' } }; /* 通用兜底:对所有已加载的站点(含未单独配置的新闻/资讯/报纸/头条站)套用“高把握”的通用广告选择器。 * 只选用几乎不可能是正文内容的类名/结构,避免误杀;浮层/iframe/弹窗类已由 autoKillOverlays/killCornerFloaters 兜底。 * 配合上面“全站通配”的 @match,未单独列名的各类新闻网页也能自动挡掉横幅/侧栏/程序化广告。 */ const GENERIC_REMOVE = [ '.ad-banner', '.ad-wrap', '.ad-box', '.ad-article', '.sidebar-ad', '.content-ad', '.article-ad', '.text-ad', '.float-ad', '.banner-ad', '.top-ad', '.bottom-ad', '.gg', '#gg', '.adsbox', '.advert', '.advertisement', '[class*="ad-box"]', '[class*="ad-wrap"]', '[class*="banner-ad"]', '[class*="article-ad"]', '[class*="sidebar-ad"]', '[class*="float-ad"]', 'iframe[id*="ad_"]', 'iframe[src*="/ads/"]', 'iframe[src*="adsystem"]' ]; /* ---------------- 用户框选屏蔽(按域名记忆) ---------------- * 悬浮窗“框选广告”后,把选中的元素选择器存在本域名下, * 之后每次访问自动隐藏,且不影响页面主要内容。 */ const CUSTOM_KEY = () => 'agb_block_' + currentSite(); let customSels = []; try { customSels = JSON.parse(GM_getValue(CUSTOM_KEY(), '[]')) || []; } catch (e) { customSels = []; } // 迁移:旧版按完整 host 存的规则,首次运行并入站点级键,避免历史手动屏蔽丢失 if (!customSels.length) { try { const old = JSON.parse(GM_getValue('agb_block_' + location.host, '[]')) || []; if (old.length) { customSels = old; try { GM_setValue(CUSTOM_KEY(), JSON.stringify(customSels)); } catch (e) {} } } catch (e) {} } let customStyle = null; // 最近屏蔽记录(最多 2 条)。每条为 { sels:[选择器...], label },一次框选/点击可能产生多个选择器。 // 持久化按站点分开(同站不同页面/子域共享),刷新后仍可恢复;仅记录“用户手动屏蔽”的动作,不含自动/程序化清理。 const UNDO_KEY = () => 'agb_undo_' + currentSite(); let undoStack = []; try { undoStack = (JSON.parse(GM_getValue(UNDO_KEY(), '[]')) || []).map(x => typeof x === 'string' ? { sels: [x], label: x } : x); } catch (e) { undoStack = []; } function escapeHtml(s) { return String(s).replace(/[&<>"']/g, c => ({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' }[c])); } // 记录一次用户屏蔽动作(一次框选/点击可能含多个选择器),仅保留最近两条 function pushUndo(sels) { sels = (Array.isArray(sels) ? sels : [sels]).filter(Boolean); if (!sels.length) return; undoStack.push({ sels: sels, label: sels[0] }); undoStack = undoStack.filter((g, i) => undoStack.findIndex(x => x.label === g.label) === i); // 同首项去重 if (undoStack.length > 2) undoStack = undoStack.slice(-2); try { GM_setValue(UNDO_KEY(), JSON.stringify(undoStack)); } catch (e) {} renderUndoList(); } // 恢复一次误屏蔽:从自定义规则与本域名记录中移除对应选择器,并重新显示被隐藏的元素 function restoreGroup(sels) { if (!Array.isArray(sels) || !sels.length) return; for (const sel of sels) customSels = customSels.filter(s => s !== sel); undoStack = undoStack.filter(g => !(g.sels && g.sels.some(s => sels.indexOf(s) >= 0))); try { GM_setValue(CUSTOM_KEY(), JSON.stringify(customSels)); } catch (e) {} try { GM_setValue(UNDO_KEY(), JSON.stringify(undoStack)); } catch (e) {} let restored = 0; for (const sel of sels) { for (const el of $all(sel)) { if (el.dataset && el.dataset.removed) { delete el.dataset.removed; el.style.setProperty('display', ''); restored++; } } } blockedCount = Math.max(0, blockedCount - restored); if (customStyle) { customStyle.textContent = customSels.filter(s => { try { const m = document.querySelector(s); return !!m && !isMainContent(m) && !(m.id && m.id.indexOf('agb-') === 0); } catch (e) { return false; } }).map(s => s + '{display:none!important;}').join('\n'); } updateStatus(); renderUndoList(); agbToast('已恢复误屏蔽区域'); } // 渲染“最近屏蔽”列表(最多 2 条,每条可一键恢复) function renderUndoList() { const box = document.getElementById('agb-undo'); if (!box) return; if (!undoStack.length) { box.style.display = 'none'; box.innerHTML = ''; return; } box.style.display = 'block'; const items = undoStack.slice().reverse().map(g => { const sel = g.sels[0] || ''; const label = sel.length > 40 ? sel.slice(0, 40) + '…' : sel; const extra = g.sels.length > 1 ? '(+' + (g.sels.length - 1) + ')' : ''; return '