|
| 1 | +import { showToast } from '@xlxz/components'; |
| 2 | + |
| 3 | +export {}; |
| 4 | + |
| 5 | +// ─── 配置 ───────────────────────────────────────────────────── |
| 6 | + |
| 7 | +const DEFAULTS: Record<string, boolean> = { |
| 8 | + adClean: true, |
| 9 | + clipboardClean: true, |
| 10 | + commentClean: true, |
| 11 | + articleClean: true, |
| 12 | +}; |
| 13 | + |
| 14 | +function getCfg(key: string): boolean { |
| 15 | + const v = GM_getValue('csdn_' + key, undefined); |
| 16 | + return v === undefined ? DEFAULTS[key] ?? true : v; |
| 17 | +} |
| 18 | + |
| 19 | +function setCfg(key: string, value: boolean): void { |
| 20 | + GM_setValue('csdn_' + key, value); |
| 21 | +} |
| 22 | + |
| 23 | +// ─── 广告清理 ───────────────────────────────────────────────── |
| 24 | + |
| 25 | +const AD_SELECTORS = [ |
| 26 | + '#footerRightAds', |
| 27 | + '.side-question-box', |
| 28 | + "div[id^='dmp_ad']", |
| 29 | + "div[class^='ad_']", |
| 30 | + "div[id^='floor-ad_']", |
| 31 | + '.adsbygoogle', |
| 32 | + '#recommendAdBox', |
| 33 | + '#asideNewNps', |
| 34 | + '.box-shadow', |
| 35 | + '.toolbar-advert', |
| 36 | +]; |
| 37 | + |
| 38 | +function removeAds(): void { |
| 39 | + for (const selector of AD_SELECTORS) { |
| 40 | + document.querySelectorAll(selector).forEach((el) => el.remove()); |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +// ─── 剪切板净化 ─────────────────────────────────────────────── |
| 45 | + |
| 46 | +function cleanClipboard(): void { |
| 47 | + // 移除 CSDN 的复制劫持 |
| 48 | + try { |
| 49 | + const w = window as any; |
| 50 | + if (w.csdn?.copyright) { |
| 51 | + w.csdn.copyright.textData = ''; |
| 52 | + } |
| 53 | + } catch { /* ignore */ } |
| 54 | + |
| 55 | + // 修复代码块复制按钮 |
| 56 | + waitFor('.hljs-button', (copyBtn) => { |
| 57 | + copyBtn.classList.remove('signin'); |
| 58 | + copyBtn.setAttribute('data-title', '复制'); |
| 59 | + copyBtn.setAttribute( |
| 60 | + 'onclick', |
| 61 | + "hljs.copyCode(event);setTimeout(function(){$('.hljs-button').attr('data-title', '复制');},3500);" |
| 62 | + ); |
| 63 | + }); |
| 64 | + |
| 65 | + // 修复内联代码复制 |
| 66 | + waitFor('code', (codeEl) => { |
| 67 | + codeEl.setAttribute('onclick', 'mdcp.copyCode(event)'); |
| 68 | + codeEl.addEventListener('copy', (e) => { |
| 69 | + const selection = window.getSelection()?.toString() ?? ''; |
| 70 | + if (selection) { |
| 71 | + e.preventDefault(); |
| 72 | + navigator.clipboard.writeText(selection).then( |
| 73 | + () => showToast('复制成功', { type: 'success', duration: 2000 }), |
| 74 | + () => showToast('复制失败,请重试', { type: 'error', duration: 2000 }) |
| 75 | + ); |
| 76 | + } |
| 77 | + }); |
| 78 | + }); |
| 79 | + |
| 80 | + // 解除 jQuery 的 copy 事件绑定 |
| 81 | + try { |
| 82 | + (window as any).jQuery?.('#content_views').unbind('copy'); |
| 83 | + } catch { /* ignore */ } |
| 84 | +} |
| 85 | + |
| 86 | +// ─── 评论区优化 ─────────────────────────────────────────────── |
| 87 | + |
| 88 | +function cleanComment(): void { |
| 89 | + const commentList = document.querySelector('.comment-list-box') as HTMLElement | null; |
| 90 | + if (commentList) { |
| 91 | + commentList.style.overflow = ''; |
| 92 | + commentList.style.maxHeight = ''; |
| 93 | + } |
| 94 | + document.getElementById('commentPage')?.classList.remove('d-none'); |
| 95 | + document.getElementById('btnMoreComment')?.remove(); |
| 96 | +} |
| 97 | + |
| 98 | +// ─── 文章展开 ───────────────────────────────────────────────── |
| 99 | + |
| 100 | +function expandArticle(): void { |
| 101 | + const articleContent = document.getElementById('article_content'); |
| 102 | + if (articleContent) { |
| 103 | + articleContent.removeAttribute('style'); |
| 104 | + } |
| 105 | + document.querySelector('.hide-article-box')?.remove(); |
| 106 | +} |
| 107 | + |
| 108 | +// ─── 工具函数 ───────────────────────────────────────────────── |
| 109 | + |
| 110 | +function waitFor(selector: string, callback: (el: HTMLElement) => void, maxRetries = 10): void { |
| 111 | + let retries = 0; |
| 112 | + const check = () => { |
| 113 | + const el = document.querySelector(selector) as HTMLElement | null; |
| 114 | + if (el) { |
| 115 | + callback(el); |
| 116 | + } else if (retries < maxRetries) { |
| 117 | + retries++; |
| 118 | + setTimeout(check, 500); |
| 119 | + } |
| 120 | + }; |
| 121 | + setTimeout(check, 800); |
| 122 | +} |
| 123 | + |
| 124 | +// ─── 菜单 ───────────────────────────────────────────────────── |
| 125 | + |
| 126 | +GM_registerMenuCommand('⚙️ CSDN 净化 — 设置', () => { |
| 127 | + const keys = Object.keys(DEFAULTS); |
| 128 | + for (const key of keys) { |
| 129 | + const current = getCfg(key); |
| 130 | + const labels: Record<string, string> = { |
| 131 | + adClean: '广告清理', |
| 132 | + clipboardClean: '剪切板净化', |
| 133 | + commentClean: '评论区优化', |
| 134 | + articleClean: '文章展开', |
| 135 | + }; |
| 136 | + const label = labels[key] ?? key; |
| 137 | + const input = prompt(`「${label}」当前为【${current ? '开启' : '关闭'}】。\n输入 0 关闭,1 开启,留空保持不变:`, current ? '1' : '0'); |
| 138 | + if (input === '0') setCfg(key, false); |
| 139 | + else if (input === '1') setCfg(key, true); |
| 140 | + } |
| 141 | + alert('设置已保存,刷新后生效。'); |
| 142 | +}); |
| 143 | + |
| 144 | +// ─── 主流程 ─────────────────────────────────────────────────── |
| 145 | + |
| 146 | +// 广告清理:定时执行(应对动态加载) |
| 147 | +if (getCfg('adClean')) { |
| 148 | + setInterval(removeAds, 3000); |
| 149 | +} |
| 150 | + |
| 151 | +// 剪切板净化 |
| 152 | +if (getCfg('clipboardClean')) { |
| 153 | + cleanClipboard(); |
| 154 | +} |
| 155 | + |
| 156 | +// 评论区优化 |
| 157 | +if (getCfg('commentClean')) { |
| 158 | + setTimeout(cleanComment, 3000); |
| 159 | +} |
| 160 | + |
| 161 | +// 文章展开 |
| 162 | +if (getCfg('articleClean')) { |
| 163 | + setTimeout(expandArticle, 1000); |
| 164 | +} |
0 commit comments