// ==UserScript== // @name 网页工具箱 - 多功能网页增强工具 // @namespace http://tampermonkey.net/ // @version 1.0 // @description 集合多种实用网页工具的增强插件,包括广告屏蔽、阅读模式、网页截图、链接提取等功能 // @author shenfangda // @match *://*/* // @grant GM_addStyle // @grant GM_setClipboard // @grant GM_download // @grant GM_xmlhttpRequest // @grant GM_info // @connect * // @license MIT // @downloadURL https://update.greasyfork.icu/scripts/549897/%E7%BD%91%E9%A1%B5%E5%B7%A5%E5%85%B7%E7%AE%B1%20-%20%E5%A4%9A%E5%8A%9F%E8%83%BD%E7%BD%91%E9%A1%B5%E5%A2%9E%E5%BC%BA%E5%B7%A5%E5%85%B7.user.js // @updateURL https://update.greasyfork.icu/scripts/549897/%E7%BD%91%E9%A1%B5%E5%B7%A5%E5%85%B7%E7%AE%B1%20-%20%E5%A4%9A%E5%8A%9F%E8%83%BD%E7%BD%91%E9%A1%B5%E5%A2%9E%E5%BC%BA%E5%B7%A5%E5%85%B7.meta.js // ==/UserScript== (function() { 'use strict'; // 配置 const config = { // 工具列表 tools: [ { id: 'adblock', name: '广告屏蔽', icon: '🚫' }, { id: 'reader', name: '阅读模式', icon: '📖' }, { id: 'screenshot', name: '网页截图', icon: '📸' }, { id: 'links', name: '链接提取', icon: '🔗' }, { id: 'text', name: '文本提取', icon: '📝' }, { id: 'password', name: '密码生成', icon: '🔑' }, { id: 'qrcode', name: '二维码生成', icon: '🔲' }, { id: 'translate', name: '快速翻译', icon: '🌐' } ], // 默认设置 defaultSettings: { adblockSelectors: [ '广告', '.ad', '.ads', '.advertisement', '[class*="ad-"]', '[id*="ad-"]', '.google-ads', '.banner-ad', '.sidebar-ad' ], readerMode: { maxWidth: 800, lineHeight: 1.6, fontSize: 16, fontFamily: 'Arial, sans-serif' } } }; // 主要功能类 class WebToolkit { constructor() { this.settings = {...config.defaultSettings}; this.init(); } init() { console.log('网页工具箱已启动'); this.createUI(); this.bindEvents(); } // 创建用户界面 createUI() { GM_addStyle(` #web-toolkit-panel { position: fixed; top: 20px; right: 20px; width: 300px; max-height: 80vh; background: #fff; border: 1px solid #ccc; border-radius: 8px; box-shadow: 0 4px 12px rgba(0,0,0,0.15); z-index: 10000; font-family: Arial, sans-serif; font-size: 14px; overflow: hidden; display: none; } #web-toolkit-panel-header { background: #2c3e50; color: white; padding: 12px 15px; cursor: move; display: flex; justify-content: space-between; align-items: center; } #web-toolkit-panel-title { font-weight: bold; font-size: 16px; } #web-toolkit-panel-close { background: none; border: none; color: white; font-size: 20px; cursor: pointer; width: 24px; height: 24px; display: flex; align-items: center; justify-content: center; border-radius: 50%; } #web-toolkit-panel-close:hover { background: rgba(255,255,255,0.2); } #web-toolkit-panel-content { padding: 15px; overflow-y: auto; max-height: calc(80vh - 50px); } .web-toolkit-section { margin-bottom: 20px; } .web-toolkit-section-title { font-weight: bold; margin-bottom: 12px; color: #2c3e50; border-bottom: 1px solid #eee; padding-bottom: 6px; display: flex; align-items: center; } .web-toolkit-section-title i { margin-right: 8px; font-size: 16px; } .web-toolkit-tools-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; } .web-toolkit-tool-item { padding: 12px; background: #f8f9fa; border: 1px solid #e9ecef; border-radius: 6px; cursor: pointer; transition: all 0.2s; display: flex; flex-direction: column; align-items: center; text-align: center; } .web-toolkit-tool-item:hover { background: #e9ecef; transform: translateY(-2px); box-shadow: 0 2px 5px rgba(0,0,0,0.1); } .web-toolkit-tool-icon { font-size: 24px; margin-bottom: 8px; } .web-toolkit-tool-name { font-size: 13px; color: #495057; } .web-toolkit-btn { padding: 10px 15px; background: #3498db; color: white; border: none; border-radius: 4px; cursor: pointer; font-size: 14px; width: 100%; margin-top: 5px; transition: background 0.2s; } .web-toolkit-btn:hover { background: #2980b9; } .web-toolkit-btn.secondary { background: #95a5a6; } .web-toolkit-btn.secondary:hover { background: #7f8c8d; } .web-toolkit-btn.success { background: #27ae60; } .web-toolkit-btn.success:hover { background: #229954; } .web-toolkit-btn.danger { background: #e74c3c; } .web-toolkit-btn.danger:hover { background: #c0392b; } .web-toolkit-input-group { margin-bottom: 15px; } .web-toolkit-input-group label { display: block; margin-bottom: 5px; font-weight: 500; color: #495057; } .web-toolkit-input { width: 100%; padding: 8px 12px; border: 1px solid #ddd; border-radius: 4px; font-size: 14px; box-sizing: border-box; } .web-toolkit-textarea { width: 100%; padding: 8px 12px; border: 1px solid #ddd; border-radius: 4px; font-size: 14px; box-sizing: border-box; min-height: 100px; resize: vertical; } .web-toolkit-result { background: #f8f9fa; border: 1px solid #e9ecef; border-radius: 4px; padding: 12px; margin-top: 10px; max-height: 200px; overflow-y: auto; } .web-toolkit-notification { position: fixed; top: 20px; right: 20px; background: #27ae60; color: white; padding: 12px 20px; border-radius: 4px; box-shadow: 0 2px 10px rgba(0,0,0,0.2); z-index: 10001; display: none; } #web-toolkit-toggle-btn { position: fixed; top: 20px; right: 20px; width: 45px; height: 45px; background: #2c3e50; color: white; border: none; border-radius: 50%; box-shadow: 0 2px 10px rgba(0,0,0,0.2); cursor: pointer; z-index: 9999; font-size: 20px; display: flex; align-items: center; justify-content: center; } #web-toolkit-toggle-btn:hover { background: #34495e; transform: scale(1.05); } .web-toolkit-password-container { display: flex; gap: 10px; } .web-toolkit-password-container .web-toolkit-input { flex: 1; } .web-toolkit-qrcode-container { text-align: center; } .web-toolkit-qrcode-container canvas { max-width: 100%; border: 1px solid #ddd; border-radius: 4px; } `); // 创建主面板 const panel = document.createElement('div'); panel.id = 'web-toolkit-panel'; panel.innerHTML = `
网页工具箱
🔧 常用工具
`; document.body.appendChild(panel); // 创建切换按钮 const toggleBtn = document.createElement('button'); toggleBtn.id = 'web-toolkit-toggle-btn'; toggleBtn.innerHTML = '🛠️'; document.body.appendChild(toggleBtn); // 创建通知元素 const notification = document.createElement('div'); notification.className = 'web-toolkit-notification'; notification.id = 'web-toolkit-notification'; document.body.appendChild(notification); // 初始化工具网格 this.initToolsGrid(); } // 初始化工具网格 initToolsGrid() { const toolsGrid = document.getElementById('web-toolkit-tools-grid'); toolsGrid.innerHTML = ''; config.tools.forEach(tool => { const toolItem = document.createElement('div'); toolItem.className = 'web-toolkit-tool-item'; toolItem.dataset.toolId = tool.id; toolItem.innerHTML = `
${tool.icon}
${tool.name}
`; toolsGrid.appendChild(toolItem); }); } // 绑定事件 bindEvents() { // 切换面板显示 document.getElementById('web-toolkit-toggle-btn').addEventListener('click', () => { const panel = document.getElementById('web-toolkit-panel'); panel.style.display = panel.style.display === 'none' ? 'block' : 'none'; }); // 关闭面板 document.getElementById('web-toolkit-panel-close').addEventListener('click', () => { document.getElementById('web-toolkit-panel').style.display = 'none'; }); // 拖拽面板 this.makeDraggable(document.getElementById('web-toolkit-panel-header'), document.getElementById('web-toolkit-panel')); // 工具点击事件 document.getElementById('web-toolkit-tools-grid').addEventListener('click', (e) => { const toolItem = e.target.closest('.web-toolkit-tool-item'); if (toolItem) { const toolId = toolItem.dataset.toolId; this.openToolDetail(toolId); } }); } // 使面板可拖拽 makeDraggable(header, panel) { let pos1 = 0, pos2 = 0, pos3 = 0, pos4 = 0; header.onmousedown = dragMouseDown; function dragMouseDown(e) { e = e || window.event; e.preventDefault(); // 获取鼠标位置 pos3 = e.clientX; pos4 = e.clientY; document.onmouseup = closeDragElement; document.onmousemove = elementDrag; } function elementDrag(e) { e = e || window.event; e.preventDefault(); // 计算新位置 pos1 = pos3 - e.clientX; pos2 = pos4 - e.clientY; pos3 = e.clientX; pos4 = e.clientY; // 设置元素新位置 panel.style.top = (panel.offsetTop - pos2) + "px"; panel.style.left = (panel.offsetLeft - pos1) + "px"; } function closeDragElement() { // 停止移动 document.onmouseup = null; document.onmousemove = null; } } // 打开工具详情 openToolDetail(toolId) { const toolDetails = document.getElementById('web-toolkit-tool-details'); toolDetails.style.display = 'block'; const tool = config.tools.find(t => t.id === toolId); if (!tool) return; switch(toolId) { case 'adblock': this.renderAdblockTool(toolDetails); break; case 'reader': this.renderReaderTool(toolDetails); break; case 'screenshot': this.renderScreenshotTool(toolDetails); break; case 'links': this.renderLinksTool(toolDetails); break; case 'text': this.renderTextTool(toolDetails); break; case 'password': this.renderPasswordTool(toolDetails); break; case 'qrcode': this.renderQRCodeTool(toolDetails); break; case 'translate': this.renderTranslateTool(toolDetails); break; default: toolDetails.innerHTML = `

工具 "${tool.name}" 尚未实现

`; } // 添加返回按钮 const backButton = document.createElement('button'); backButton.className = 'web-toolkit-btn secondary'; backButton.textContent = '← 返回工具列表'; backButton.onclick = () => { toolDetails.style.display = 'none'; this.initToolsGrid(); }; toolDetails.appendChild(backButton); } // 渲染广告屏蔽工具 renderAdblockTool(container) { container.innerHTML = `
🚫 广告屏蔽

点击下方按钮屏蔽页面中的广告元素

`; document.getElementById('web-toolkit-adblock-btn').addEventListener('click', () => { this.blockAds(); }); } // 渲染阅读模式工具 renderReaderTool(container) { container.innerHTML = `
📖 阅读模式

点击下方按钮进入阅读模式,专注于文章内容

`; document.getElementById('web-toolkit-reader-btn').addEventListener('click', () => { this.enterReaderMode(); }); } // 渲染网页截图工具 renderScreenshotTool(container) { container.innerHTML = `
📸 网页截图

点击下方按钮截取当前网页

`; document.getElementById('web-toolkit-screenshot-btn').addEventListener('click', () => { this.takeScreenshot(); }); } // 渲染链接提取工具 renderLinksTool(container) { container.innerHTML = `
🔗 链接提取

点击下方按钮提取页面中的所有链接

`; document.getElementById('web-toolkit-links-btn').addEventListener('click', () => { this.extractLinks(); }); } // 渲染文本提取工具 renderTextTool(container) { container.innerHTML = `
📝 文本提取

点击下方按钮提取页面中的所有文本内容

`; document.getElementById('web-toolkit-text-btn').addEventListener('click', () => { this.extractText(); }); } // 渲染密码生成工具 renderPasswordTool(container) { container.innerHTML = `
🔑 密码生成
`; document.getElementById('web-toolkit-password-generate').addEventListener('click', () => { this.generatePassword(); }); document.getElementById('web-toolkit-password-copy').addEventListener('click', () => { this.copyPassword(); }); } // 渲染二维码生成工具 renderQRCodeTool(container) { container.innerHTML = `
🔲 二维码生成
`; document.getElementById('web-toolkit-qrcode-generate').addEventListener('click', () => { this.generateQRCode(); }); } // 渲染翻译工具 renderTranslateTool(container) { container.innerHTML = `
🌐 快速翻译
`; document.getElementById('web-toolkit-translate-btn').addEventListener('click', () => { this.translateText(); }); } // 广告屏蔽功能 blockAds() { const result = document.getElementById('web-toolkit-adblock-result'); let blockedCount = 0; // 遍历所有选择器并隐藏匹配的元素 this.settings.adblockSelectors.forEach(selector => { try { const elements = document.querySelectorAll(selector); elements.forEach(element => { if (element.style.display !== 'none') { element.style.display = 'none'; blockedCount++; } }); } catch (e) { console.warn('选择器无效:', selector); } }); // 显示结果 result.style.display = 'block'; result.innerHTML = `已屏蔽 ${blockedCount} 个广告元素`; this.showNotification(`已屏蔽 ${blockedCount} 个广告元素`); } // 阅读模式 enterReaderMode() { // 保存原始内容 const originalContent = document.body.innerHTML; // 创建阅读模式容器 const readerContainer = document.createElement('div'); readerContainer.id = 'web-toolkit-reader-mode'; readerContainer.style.cssText = ` position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: white; z-index: 99999; overflow-y: auto; padding: 20px; box-sizing: border-box; `; // 提取文章内容(简化版) const articleContent = this.extractArticleContent(); readerContainer.innerHTML = `

${document.title}

${articleContent}
`; document.body.appendChild(readerContainer); // 绑定退出事件 document.getElementById('web-toolkit-reader-exit').addEventListener('click', () => { document.body.removeChild(readerContainer); }); this.showNotification('已进入阅读模式'); } // 提取文章内容(简化版) extractArticleContent() { // 尝试找到文章内容 const contentSelectors = [ 'article', '.content', '.post-content', '.article-content', '.entry-content', 'main', '#content' ]; for (const selector of contentSelectors) { const element = document.querySelector(selector); if (element) { return element.innerHTML; } } // 如果没找到,返回body内容(去除script和style) const tempDiv = document.createElement('div'); tempDiv.innerHTML = document.body.innerHTML; // 移除script和style标签 const scripts = tempDiv.querySelectorAll('script'); scripts.forEach(script => script.remove()); const styles = tempDiv.querySelectorAll('style'); styles.forEach(style => style.remove()); return tempDiv.innerHTML; } // 网页截图 takeScreenshot() { this.showNotification('截图功能需要特殊权限,此版本为简化实现'); const result = document.getElementById('web-toolkit-screenshot-result'); result.style.display = 'block'; result.innerHTML = `

截图功能说明:

完整截图功能需要使用 html2canvas 或类似库,此版本为演示。

您可以手动使用浏览器的截图功能:

`; } // 提取链接 extractLinks() { const links = Array.from(document.querySelectorAll('a[href]')) .map(a => ({ text: a.textContent.trim(), url: a.href })) .filter(link => link.text && link.url); const result = document.getElementById('web-toolkit-links-result'); result.style.display = 'block'; if (links.length === 0) { result.innerHTML = '

未找到链接

'; return; } let html = `

找到 ${links.length} 个链接:

'; result.innerHTML = html; this.showNotification(`提取到 ${links.length} 个链接`); } // 提取文本 extractText() { const text = document.body.innerText || document.body.textContent; const result = document.getElementById('web-toolkit-text-result'); result.style.display = 'block'; if (!text) { result.innerHTML = '

未找到文本内容

'; return; } // 截取前1000个字符作为预览 const preview = text.substring(0, 1000); const fullText = text; result.innerHTML = `

文本长度: ${fullText.length} 字符

`; document.getElementById('web-toolkit-text-copy').addEventListener('click', () => { GM_setClipboard(fullText); this.showNotification('文本已复制到剪贴板'); }); this.showNotification(`提取到 ${fullText.length} 字符的文本`); } // 生成密码 generatePassword() { const length = parseInt(document.getElementById('web-toolkit-password-length').value) || 12; const includeUppercase = document.getElementById('web-toolkit-password-uppercase').checked; const includeNumbers = document.getElementById('web-toolkit-password-numbers').checked; const includeSymbols = document.getElementById('web-toolkit-password-symbols').checked; let charset = 'abcdefghijklmnopqrstuvwxyz'; if (includeUppercase) charset += 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; if (includeNumbers) charset += '0123456789'; if (includeSymbols) charset += '!@#$%^&*()_+-=[]{}|;:,.<>?'; let password = ''; for (let i = 0; i < length; i++) { const randomIndex = Math.floor(Math.random() * charset.length); password += charset[randomIndex]; } document.getElementById('web-toolkit-password-result').value = password; this.showNotification('密码已生成'); } // 复制密码 copyPassword() { const password = document.getElementById('web-toolkit-password-result').value; if (password) { GM_setClipboard(password); this.showNotification('密码已复制到剪贴板'); } } // 生成二维码 generateQRCode() { const text = document.getElementById('web-toolkit-qrcode-text').value; if (!text) { this.showNotification('请输入文本内容'); return; } const result = document.getElementById('web-toolkit-qrcode-result'); result.innerHTML = `

二维码内容: ${text}

由于版权原因,完整二维码生成功能需要引入第三方库。

您可以使用在线二维码生成器或安装专门的二维码插件。

`; this.showNotification('二维码生成功能需要第三方库支持'); } // 翻译文本 translateText() { const text = document.getElementById('web-toolkit-translate-input').value; const targetLang = document.getElementById('web-toolkit-translate-target').value; if (!text) { this.showNotification('请输入要翻译的文本'); return; } const result = document.getElementById('web-toolkit-translate-result'); result.style.display = 'block'; // 模拟翻译结果 const translations = { 'zh': '这是翻译后的中文文本', 'en': 'This is the translated English text', 'ja': 'これは翻訳された日本語テキストです', 'ko': '이것은 번역된 한국어 텍스트입니다', 'fr': 'Ceci est le texte traduit en français', 'de': 'Dies ist der übersetzte deutsche Text', 'es': 'Este es el texto traducido al español' }; result.innerHTML = `

翻译结果 (${targetLang}):

${translations[targetLang] || translations['en']}

注意:实际翻译功能需要调用翻译API

`; this.showNotification('翻译功能为演示版本'); } // 显示通知 showNotification(message) { const notification = document.getElementById('web-toolkit-notification'); notification.textContent = message; notification.style.display = 'block'; setTimeout(() => { notification.style.display = 'none'; }, 3000); } } // 初始化插件 new WebToolkit(); })();