// ==UserScript== // @name 🚀 极致网页体验增强器 // @namespace https://github.com/pluto // @version 1.0.0 // @description 全网通用:去广告、解除复制限制、自动展开全文、智能阅读模式、暗黑护眼、快捷操作面板 — 一站式网页体验提升 // @author Pluto // @match *://*/* // @grant GM_addStyle // @grant GM_registerMenuCommand // @grant GM_setValue // @grant GM_getValue // @grant GM_notification // @run-at document-idle // @license MIT // ==/UserScript== (function () { 'use strict'; // ======================== 配置中心 ======================== const CONFIG = { adBlock: true, // 去广告 unblockCopy: true, // 解除复制限制 autoExpand: true, // 自动展开全文 cleanMode: false, // 阅读模式(默认关闭,手动开启) darkMode: false, // 暗黑模式(默认关闭) removePopups: true, // 去弹窗 cleanURL: true, // 清理跟踪参数 showPanel: true, // 显示快捷面板 fontSize: 18, // 阅读模式字体大小 }; // ======================== 工具函数 ======================== const $ = (sel, ctx = document) => ctx.querySelector(sel); const $$ = (sel, ctx = document) => [...ctx.querySelectorAll(sel)]; const log = (msg) => console.log('%c[极致增强]', 'color:#00d4aa;font-weight:bold', msg); // ======================== 1. 广告屏蔽 ======================== const AD_SELECTORS = [ // 通用广告 '[class*="ad-"]', '[class*="ad_"]', '[id*="ad-"]', '[id*="ad_"]', '[class*="ads-"]', '[class*="ads_"]', '[id*="ads-"]', '[id*="ads_"]', '[class*="advert"]', '[id*="advert"]', '[class*="banner"]', '[id*="banner"]', '[class*="sponsor"]', '[id*="sponsor"]', 'iframe[src*="ad"]', 'iframe[src*="doubleclick"]', 'iframe[src*="googlead"]', 'iframe[src*="adservice"]', // 弹窗广告 '[class*="popup"]', '[class*="modal-ad"]', '[class*="float-ad"]', '[class*="fixed-ad"]', '[class*="corner-ad"]', '[class*="overlay"]', // 常见广告容器 '.ad', '.ads', '.advert', '.advertisement', '#ad', '#ads', '#advert', // 百度广告 '[data-tuiguang]', '[class*="ec-tuiguang"]', // CSDN广告 '.recommend-ad', '.blog-footer-bottom', // 知乎广告 '.Pc-card', '.Pc-Business-card', // 简书广告 '.download-app', ]; function blockAds() { if (!CONFIG.adBlock) return; let count = 0; AD_SELECTORS.forEach(sel => { $$(sel).forEach(el => { if (el.offsetHeight > 0) { el.style.display = 'none'; count++; } }); }); if (count > 0) log(`已屏蔽 ${count} 个广告元素`); } // ======================== 2. 解除复制限制 ======================== function unblockCopy() { if (!CONFIG.unblockCopy) return; // 移除事件监听限制 const events = ['copy', 'cut', 'paste', 'selectstart', 'contextmenu', 'mousedown', 'mouseup']; events.forEach(evt => { document.addEventListener(evt, (e) => e.stopPropagation(), true); }); // 移除 CSS 限制 GM_addStyle(` *, *::before, *::after { -webkit-user-select: text !important; -moz-user-select: text !important; -ms-user-select: text !important; user-select: text !important; -webkit-touch-callout: default !important; } `); // 移除 JS 层面限制 try { Object.defineProperty(document, 'oncopy', { get: () => null, set: () => {} }); Object.defineProperty(document, 'oncut', { get: () => null, set: () => {} }); Object.defineProperty(document, 'onselectstart', { get: () => null, set: () => {} }); Object.defineProperty(document, 'oncontextmenu', { get: () => null, set: () => {} }); } catch (e) { } log('已解除复制限制'); } // ======================== 3. 自动展开全文 ======================== function autoExpand() { if (!CONFIG.autoExpand) return; const EXPAND_SELECTORS = [ // 通用 '[class*="expand"]', '[class*="show-more"]', '[class*="read-more"]', '[class*="unfold"]', '[class*="展开"]', '[class*="更多"]', 'a[href*="javascript"]', // CSDN '.hide-article-box', '#readMore', '.readall_box', '.article-show-more', // 知乎 '.RichContent-inner .ContentItem-expandButton', '.QuestionRichText-more', // 百度 '.c-content-display', // 微信公众号 '#js_pc_qr_code', // 通用"展开全文"按钮 'button', 'a', ]; let expanded = 0; EXPAND_SELECTORS.forEach(sel => { $$(sel).forEach(el => { const text = el.textContent || ''; if (/展开|更多|read\s*more|show\s*more|展开全文|查看全部|查看更多/i.test(text)) { el.click(); expanded++; } }); }); // 移除遮罩层 $$('[class*="gradient"]', '[class*="mask"]').forEach(el => { if (el.style.background && el.style.background.includes('gradient')) { el.style.display = 'none'; } }); if (expanded > 0) log(`已自动展开 ${expanded} 处内容`); } // ======================== 4. 去弹窗 ======================== function removePopups() { if (!CONFIG.removePopups) return; const POPUP_SELECTORS = [ '[class*="modal"]', '[class*="dialog"]', '[class*="popup"]', '[class*="overlay"]', '[class*="mask"]', '[class*="toast"]', '[class*="notice"]', '[class*="alert"]', '[class*="tip"]', '[role="dialog"]', '[role="alertdialog"]', '.ant-modal-mask', '.ant-modal-wrap', '.el-dialog__wrapper', '.el-overlay', '.modal-backdrop', '.modal-overlay', ]; POPUP_SELECTORS.forEach(sel => { $$(sel).forEach(el => { const style = window.getComputedStyle(el); if (style.position === 'fixed' && style.zIndex > 999) { // 检查是否是登录弹窗、广告弹窗等 const text = el.textContent || ''; if (/登录|注册|关注|下载app|开会员|VIP|广告|推广|微信/i.test(text)) { el.style.display = 'none'; log('已移除弹窗'); } } }); }); // 移除 body 的 overflow: hidden if (document.body.style.overflow === 'hidden') { document.body.style.overflow = 'auto'; } } // ======================== 5. 阅读模式 ======================== function toggleCleanMode() { CONFIG.cleanMode = !CONFIG.cleanMode; if (CONFIG.cleanMode) { // 提取正文 const article = extractArticle(); if (!article) { GM_notification({ title: '提示', text: '未找到可提取的正文内容', timeout: 2000 }); CONFIG.cleanMode = false; return; } // 创建阅读层 const overlay = document.createElement('div'); overlay.id = 'reading-overlay'; overlay.innerHTML = `

${article.title}

${article.content}
`; document.body.appendChild(overlay); // 样式 GM_addStyle(` #reading-overlay { position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: ${CONFIG.darkMode ? '#1a1a2e' : '#faf9f6'}; z-index: 999999; overflow-y: auto; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif; } #reading-header { max-width: 800px; margin: 0 auto; padding: 20px 20px 0; display: flex; justify-content: space-between; align-items: flex-start; } #reading-title { font-size: 28px; font-weight: 700; color: ${CONFIG.darkMode ? '#e0e0e0' : '#1a1a1a'}; margin: 0; line-height: 1.4; flex: 1; } #reading-close { background: ${CONFIG.darkMode ? '#333' : '#f0f0f0'}; border: none; color: ${CONFIG.darkMode ? '#ccc' : '#666'}; padding: 8px 16px; border-radius: 6px; cursor: pointer; font-size: 14px; margin-left: 20px; transition: all 0.2s; } #reading-close:hover { background: #ff4757; color: #fff; } #reading-body { max-width: 800px; margin: 30px auto; padding: 0 20px 100px; font-size: ${CONFIG.fontSize}px; line-height: 1.9; color: ${CONFIG.darkMode ? '#c8c8c8' : '#333'}; } #reading-body p { margin-bottom: 1.2em; text-indent: 2em; } #reading-body img { max-width: 100%; border-radius: 8px; margin: 20px 0; } #reading-body h2, #reading-body h3 { color: ${CONFIG.darkMode ? '#e0e0e0' : '#1a1a1a'}; margin: 1.5em 0 0.8em; } #reading-body a { color: #00d4aa; text-decoration: none; } `); // 关闭事件 $('#reading-close').onclick = () => toggleCleanMode(); document.addEventListener('keydown', (e) => { if (e.key === 'Escape' && CONFIG.cleanMode) toggleCleanMode(); }); log('已开启阅读模式'); } else { const overlay = $('#reading-overlay'); if (overlay) overlay.remove(); log('已关闭阅读模式'); } } function extractArticle() { // 策略1:找 article 标签 let el = $('article'); // 策略2:找常见内容容器 if (!el) el = $('[class*="article"]') || $('[class*="content"]') || $('[class*="post"]'); if (!el) el = $('[id*="article"]') || $('[id*="content"]') || $('[id*="post"]'); // 策略3:找最大的文本块 if (!el) { let maxLen = 0; $$('div, section').forEach(d => { const text = d.textContent || ''; if (text.length > maxLen && d.children.length < 50) { maxLen = text.length; el = d; } }); } if (!el) return null; const title = $('h1')?.textContent || document.title; // 清理内容 let content = el.innerHTML; // 移除脚本和样式 content = content.replace(//gi, ''); content = content.replace(//gi, ''); // 移除广告和无关元素 content = content.replace(/<[^>]+(ad|banner|sponsor|share|comment)[^>]*>[\s\S]*?<\/[^>]+>/gi, ''); return { title, content }; } // ======================== 6. 暗黑模式 ======================== function toggleDarkMode() { CONFIG.darkMode = !CONFIG.darkMode; if (CONFIG.darkMode) { GM_addStyle(` html { filter: invert(1) hue-rotate(180deg) !important; } img, video, iframe, canvas, svg, [style*="background-image"] { filter: invert(1) hue-rotate(180deg) !important; } `); log('已开启暗黑模式'); } else { // 移除暗黑样式 $$('style').forEach(s => { if (s.textContent.includes('invert(1)')) s.remove(); }); log('已关闭暗黑模式'); } } // ======================== 7. URL 清理 ======================== function cleanURL() { if (!CONFIG.cleanURL) return; const url = new URL(window.location.href); const trackingParams = [ 'utm_source', 'utm_medium', 'utm_campaign', 'utm_content', 'utm_term', 'fbclid', 'gclid', 'msclkid', 'dclid', 'twclid', 'ref', 'spm', 'from', 's_cid', '_ga', 'mc_cid', 'mc_eid', 'share_source', 'share_medium', 'plat_id', 'share_from', ]; let cleaned = false; trackingParams.forEach(p => { if (url.searchParams.has(p)) { url.searchParams.delete(p); cleaned = true; } }); if (cleaned && url.href !== window.location.href) { window.history.replaceState({}, '', url.href); log('已清理跟踪参数'); } } // ======================== 8. 快捷面板 ======================== function createPanel() { if (!CONFIG.showPanel) return; const panel = document.createElement('div'); panel.id = 'enhancer-panel'; panel.innerHTML = `
🚀
`; document.body.appendChild(panel); // 样式 GM_addStyle(` #enhancer-panel { position: fixed; bottom: 20px; right: 20px; z-index: 999998; font-family: -apple-system, BlinkMacSystemFont, sans-serif; } #enhancer-toggle { width: 48px; height: 48px; border-radius: 50%; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); display: flex; align-items: center; justify-content: center; cursor: pointer; box-shadow: 0 4px 15px rgba(102,126,234,0.4); transition: all 0.3s; font-size: 22px; } #enhancer-toggle:hover { transform: scale(1.1); box-shadow: 0 6px 20px rgba(102,126,234,0.6); } #enhancer-menu { position: absolute; bottom: 60px; right: 0; width: 240px; background: #1e1e2e; border-radius: 12px; padding: 12px; box-shadow: 0 8px 30px rgba(0,0,0,0.3); border: 1px solid rgba(255,255,255,0.1); animation: slideUp 0.2s ease; } @keyframes slideUp { from { opacity: 0; transform: translateY(10px); } to { opacity: 1; transform: translateY(0); } } .enhancer-title { color: #e0e0e0; font-size: 14px; font-weight: 600; padding: 4px 8px 8px; border-bottom: 1px solid rgba(255,255,255,0.1); margin-bottom: 8px; } .enhancer-item { display: flex; justify-content: space-between; align-items: center; padding: 8px; border-radius: 6px; cursor: pointer; transition: background 0.2s; } .enhancer-item:hover { background: rgba(255,255,255,0.08); } .enhancer-item span { color: #ccc; font-size: 13px; } .switch { position: relative; display: inline-block; width: 36px; height: 20px; } .switch input { opacity: 0; width: 0; height: 0; } .slider { position: absolute; top: 0; left: 0; right: 0; bottom: 0; background: #444; border-radius: 20px; transition: 0.3s; cursor: pointer; } .slider::before { content: ''; position: absolute; width: 16px; height: 16px; left: 2px; bottom: 2px; background: #fff; border-radius: 50%; transition: 0.3s; } input:checked + .slider { background: #00d4aa; } input:checked + .slider::before { transform: translateX(16px); } .enhancer-divider { height: 1px; background: rgba(255,255,255,0.1); margin: 8px 0; } .enhancer-info { color: #888; font-size: 11px; padding: 4px 8px; line-height: 1.5; } .enhancer-info kbd { background: #333; border: 1px solid #555; border-radius: 3px; padding: 1px 5px; font-size: 10px; color: #ccc; } `); // 事件绑定 $('#enhancer-toggle').onclick = () => { const menu = $('#enhancer-menu'); menu.style.display = menu.style.display === 'none' ? 'block' : 'none'; }; // 开关事件 $$('.enhancer-item').forEach(item => { item.onclick = (e) => { if (e.target.tagName === 'INPUT') return; // 让 checkbox 自己处理 const input = item.querySelector('input'); input.checked = !input.checked; handleToggle(item.dataset.action, input.checked); }; const input = item.querySelector('input'); input.onchange = () => handleToggle(item.dataset.action, input.checked); }); // 点击外部关闭 document.addEventListener('click', (e) => { if (!e.target.closest('#enhancer-panel')) { const menu = $('#enhancer-menu'); if (menu) menu.style.display = 'none'; } }); } function handleToggle(action, state) { switch (action) { case 'ad': CONFIG.adBlock = state; blockAds(); break; case 'copy': CONFIG.unblockCopy = state; unblockCopy(); break; case 'expand': CONFIG.autoExpand = state; autoExpand(); break; case 'popup': CONFIG.removePopups = state; removePopups(); break; case 'clean': toggleCleanMode(); break; case 'dark': toggleDarkMode(); break; case 'url': CONFIG.cleanURL = state; cleanURL(); break; } GM_setValue('enhancer_config', CONFIG); GM_notification({ title: '极致增强器', text: `${state ? '已开启' : '已关闭'}`, timeout: 1500 }); } // ======================== 9. 快捷键 ======================== function initHotkeys() { document.addEventListener('keydown', (e) => { // 忽略输入框 if (['INPUT', 'TEXTAREA', 'SELECT'].includes(e.target.tagName)) return; if (e.target.isContentEditable) return; switch (e.key.toLowerCase()) { case 'h': // 显示/隐藏面板 if (!e.ctrlKey && !e.altKey && !e.metaKey) { e.preventDefault(); const menu = $('#enhancer-menu'); if (menu) menu.style.display = menu.style.display === 'none' ? 'block' : 'none'; } break; case 'r': // 阅读模式 if (!e.ctrlKey && !e.altKey && !e.metaKey) { e.preventDefault(); toggleCleanMode(); } break; case 'd': // 暗黑模式 if (!e.ctrlKey && !e.altKey && !e.metaKey) { e.preventDefault(); toggleDarkMode(); } break; } }); } // ======================== 10. 菜单注册 ======================== function registerMenus() { GM_registerMenuCommand('📖 阅读模式 (R)', () => toggleCleanMode()); GM_registerMenuCommand('🌙 暗黑模式 (D)', () => toggleDarkMode()); GM_registerMenuCommand('🛡️ 重新去广告', () => blockAds()); GM_registerMenuCommand('📖 重新展开全文', () => autoExpand()); GM_registerMenuCommand('🚫 重新去弹窗', () => removePopups()); } // ======================== 初始化 ======================== function init() { log('🚀 极致网页体验增强器已启动'); // 加载保存的配置 const saved = GM_getValue('enhancer_config'); if (saved) Object.assign(CONFIG, saved); // 执行功能 cleanURL(); unblockCopy(); blockAds(); removePopups(); autoExpand(); createPanel(); initHotkeys(); registerMenus(); // 动态内容监控(延迟加载的广告等) const observer = new MutationObserver(() => { blockAds(); removePopups(); }); observer.observe(document.body, { childList: true, subtree: true }); // 定期清理(应对动态加载) setInterval(() => { blockAds(); removePopups(); }, 5000); log('所有功能初始化完成 ✓'); } // 启动 if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', init); } else { init(); } })();