// ==UserScript== // @name 智能翻译助手 // @namespace http://tampermonkey.net/ // @version 1.0.0 // @description 功能强大的网页翻译工具,支持多语言,可自定义配置,界面精美,支持移动端 // @author Eray // @run-at document-start // @match *://*/* // @grant GM_setValue // @grant GM_getValue // @grant GM_addStyle // @grant GM_registerMenuCommand // @license Apache-2.0 // @require https://unpkg.com/i18n-jsautotranslate@3.18.0/index.js // @downloadURL none // ==/UserScript== (function() { 'use strict'; // 尽早注入样式和创建悬浮球 const earlyInit = () => { // 检测是否为移动设备 const isMobile = () => { return /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) || window.innerWidth <= 768; }; // 获取保存的配置 const getConfig = () => { const defaultConfig = { enabled: true, localLanguage: 'chinese_simplified', targetLanguage: 'chinese_simplified', floatBallSize: isMobile() ? 45 : 50, floatBallPosition: { x: 20, y: 100 }, floatBallOpacity: 0.8, autoTranslate: false, showFloatBall: true, translateService: 'client.edge', allowHalfBall: true }; const saved = GM_getValue('translateConfig', null); return saved ? { ...defaultConfig, ...saved } : defaultConfig; }; const config = getConfig(); const mobile = isMobile(); // 注入基础样式 const baseStyles = ` #translate-float-ball { position: fixed; width: ${config.floatBallSize}px; height: ${config.floatBallSize}px; border-radius: 50%; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); box-shadow: 0 4px 15px rgba(102, 126, 234, 0.4); cursor: ${mobile ? 'pointer' : 'move'}; z-index: 2147483647; display: ${config.showFloatBall ? 'flex' : 'none'}; align-items: center; justify-content: center; opacity: ${config.floatBallOpacity}; left: ${config.floatBallPosition.x}px; top: ${config.floatBallPosition.y}px; transition: transform 0.3s ease, box-shadow 0.3s ease; user-select: none; -webkit-user-select: none; touch-action: none; } #translate-float-ball svg { width: ${mobile ? '24px' : '28px'}; height: ${mobile ? '24px' : '28px'}; fill: white; pointer-events: none; } `; // 创建style标签 const style = document.createElement('style'); style.textContent = baseStyles; // 创建悬浮球 const ball = document.createElement('div'); ball.id = 'translate-float-ball'; ball.innerHTML = ` `; // 等待DOM准备好 if (document.head) { document.head.appendChild(style); } else { document.addEventListener('DOMContentLoaded', () => { document.head.appendChild(style); }); } if (document.body) { document.body.appendChild(ball); } else { document.addEventListener('DOMContentLoaded', () => { document.body.appendChild(ball); }); } }; // 立即执行早期初始化 earlyInit(); // 主要功能代码 (() => { // 动态加载脚本 function loadScript(src) { return new Promise((resolve, reject) => { // 检查是否已经加载 if (window.translate) { resolve(); return; } const script = document.createElement('script'); script.src = src; script.onload = resolve; script.onerror = reject; document.head.appendChild(script); }); } // 检测是否为移动设备 function isMobile() { return /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) || window.innerWidth <= 768; } // 配置管理 class ConfigManager { constructor() { this.defaultConfig = { enabled: true, localLanguage: 'chinese_simplified', targetLanguage: 'chinese_simplified', floatBallSize: isMobile() ? 45 : 50, floatBallPosition: { x: 20, y: 100 }, floatBallOpacity: 0.8, autoTranslate: false, ignoredClasses: [], ignoredIds: [], customTerms: {}, showFloatBall: true, translateService: 'client.edge', allowHalfBall: true }; this.config = this.loadConfig(); } loadConfig() { const saved = GM_getValue('translateConfig', null); return saved ? { ...this.defaultConfig, ...saved } : this.defaultConfig; } saveConfig() { GM_setValue('translateConfig', this.config); } get(key) { return this.config[key]; } set(key, value) { this.config[key] = value; this.saveConfig(); } } // 翻译管理器 class TranslateManager { constructor(configManager) { this.configManager = configManager; this.initialized = false; this.listenerStarted = false; this.currentLanguage = null; } init() { if (this.initialized || typeof translate === 'undefined') return; try { // 配置translate.js translate.language.setLocal(this.configManager.get('localLanguage')); translate.service.use(this.configManager.get('translateService')); translate.selectLanguageTag.show = false; // 设置忽略的类和ID const ignoredClasses = this.configManager.get('ignoredClasses'); const ignoredIds = this.configManager.get('ignoredIds'); if (ignoredClasses.length > 0) { translate.ignore.class = ignoredClasses; } if (ignoredIds.length > 0) { translate.ignore.id = ignoredIds; } // 设置自定义术语 const customTerms = this.configManager.get('customTerms'); if (Object.keys(customTerms).length > 0) { translate.nomenclature.append(customTerms); } // 只启动一次监听 if (!this.listenerStarted) { translate.listener.start(); this.listenerStarted = true; } this.initialized = true; } catch (error) { console.error('翻译初始化失败:', error); } } changeLanguage(targetLang) { if (!this.initialized) this.init(); if (typeof translate === 'undefined') return; try { this.currentLanguage = targetLang; translate.changeLanguage(targetLang); } catch (error) { console.error('切换语言失败:', error); } } toggle(enabled) { if (enabled && !this.initialized) { this.init(); if (this.configManager.get('autoTranslate')) { setTimeout(() => { this.changeLanguage(this.configManager.get('targetLanguage')); }, 100); } } else if (!enabled && this.initialized) { this.changeLanguage(this.configManager.get('localLanguage')); } } execute() { if (!this.initialized) this.init(); if (typeof translate !== 'undefined') { try { translate.execute(); } catch (error) { console.error('执行翻译失败:', error); } } } } // UI管理器 class UIManager { constructor(configManager, translateManager) { this.configManager = configManager; this.translateManager = translateManager; this.floatBall = document.getElementById('translate-float-ball'); this.panel = null; this.isDragging = false; this.dragOffset = { x: 0, y: 0 }; this.touchStartPos = { x: 0, y: 0 }; this.touchStartTime = 0; this.init(); } init() { this.injectStyles(); this.setupFloatBall(); this.createPanel(); this.bindEvents(); // 初始化翻译 if (this.configManager.get('enabled')) { setTimeout(() => { this.translateManager.toggle(true); }, 1000); } } injectStyles() { const mobile = isMobile(); GM_addStyle(` /* 悬浮球动画样式 */ #translate-float-ball:active { transform: scale(0.95); } #translate-float-ball.dragging { transition: none !important; transform: scale(1.1); box-shadow: 0 6px 20px rgba(102, 126, 234, 0.6); } #translate-float-ball:hover { transform: scale(1.05); box-shadow: 0 6px 20px rgba(102, 126, 234, 0.5); } /* 控制面板样式 */ #translate-panel { position: fixed; width: ${mobile ? '90%' : 'min(400px, 90vw)'}; max-width: 400px; max-height: ${mobile ? '85vh' : '600px'}; background: white; border-radius: 12px; box-shadow: 0 10px 40px rgba(0, 0, 0, 0.15); z-index: 2147483646; display: none; font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif; overflow: hidden; ${mobile ? ` left: 50% !important; top: 50% !important; transform: translate(-50%, -50%); ` : ''} } #translate-panel.show { display: block; animation: slideIn 0.3s ease; } @keyframes slideIn { from { opacity: 0; transform: ${mobile ? 'translate(-50%, -45%)' : 'translateY(-20px)'}; } to { opacity: 1; transform: ${mobile ? 'translate(-50%, -50%)' : 'translateY(0)'}; } } .translate-panel-header { background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white; padding: ${mobile ? '15px' : '20px'}; display: flex; justify-content: space-between; align-items: center; } .translate-panel-title { font-size: ${mobile ? '16px' : '18px'}; font-weight: 600; } .translate-panel-close { width: 30px; height: 30px; border-radius: 50%; background: rgba(255, 255, 255, 0.2); border: none; cursor: pointer; display: flex; align-items: center; justify-content: center; transition: background 0.3s; font-size: 18px; color: white; } .translate-panel-close:hover { background: rgba(255, 255, 255, 0.3); } .translate-panel-close:active { transform: scale(0.95); } .translate-panel-body { padding: ${mobile ? '15px' : '20px'}; max-height: ${mobile ? 'calc(85vh - 60px)' : '500px'}; overflow-y: auto; -webkit-overflow-scrolling: touch; } .translate-panel-body::-webkit-scrollbar { width: 6px; } .translate-panel-body::-webkit-scrollbar-thumb { background: rgba(0,0,0,0.2); border-radius: 3px; } .translate-panel-body::-webkit-scrollbar-track { background: transparent; } .translate-control-group { margin-bottom: ${mobile ? '15px' : '20px'}; } .translate-control-label { display: block; margin-bottom: 8px; font-size: 14px; font-weight: 500; color: #333; } .translate-switch { position: relative; display: inline-block; width: 50px; height: 24px; } .translate-switch input { opacity: 0; width: 0; height: 0; } .translate-switch-slider { position: absolute; cursor: pointer; top: 0; left: 0; right: 0; bottom: 0; background-color: #ccc; transition: .4s; border-radius: 24px; } .translate-switch-slider:before { position: absolute; content: ""; height: 18px; width: 18px; left: 3px; bottom: 3px; background-color: white; transition: .4s; border-radius: 50%; } .translate-switch input:checked + .translate-switch-slider { background-color: #667eea; } .translate-switch input:checked + .translate-switch-slider:before { transform: translateX(26px); } .translate-select { width: 100%; padding: ${mobile ? '12px' : '10px'}; border: 1px solid #ddd; border-radius: 8px; font-size: ${mobile ? '16px' : '14px'}; background: white; cursor: pointer; transition: border-color 0.3s; } .translate-select:focus { outline: none; border-color: #667eea; } .translate-slider-container { display: flex; align-items: center; gap: 10px; } .translate-slider { flex: 1; -webkit-appearance: none; height: 6px; border-radius: 3px; background: #ddd; outline: none; } .translate-slider::-webkit-slider-thumb { -webkit-appearance: none; appearance: none; width: 18px; height: 18px; border-radius: 50%; background: #667eea; cursor: pointer; } .translate-slider::-moz-range-thumb { width: 18px; height: 18px; border-radius: 50%; background: #667eea; cursor: pointer; border: none; } .translate-slider-value { min-width: 45px; text-align: center; font-size: 14px; color: #666; } .translate-button { width: 100%; padding: ${mobile ? '14px' : '12px'}; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white; border: none; border-radius: 8px; font-size: ${mobile ? '16px' : '14px'}; font-weight: 500; cursor: pointer; transition: transform 0.3s, box-shadow 0.3s; -webkit-tap-highlight-color: transparent; } .translate-button:hover { transform: translateY(-1px); box-shadow: 0 4px 12px rgba(102, 126, 234, 0.4); } .translate-button:active { transform: scale(0.98); } .translate-section-title { font-size: ${mobile ? '15px' : '16px'}; font-weight: 600; color: #333; margin-bottom: 15px; padding-bottom: 10px; border-bottom: 2px solid #f0f0f0; } .translate-info { background: #f8f9fa; padding: 12px; border-radius: 8px; font-size: ${mobile ? '12px' : '13px'}; color: #666; margin-top: 10px; line-height: 1.5; } /* 响应式优化 */ @media (max-width: 768px) { .translate-control-group { margin-bottom: 15px; } } @media (min-width: 769px) and (max-width: 1024px) { #translate-panel { width: min(400px, 80vw); } } `); } setupFloatBall() { if (!this.floatBall) return; const size = this.configManager.get('floatBallSize'); const position = this.configManager.get('floatBallPosition'); const opacity = this.configManager.get('floatBallOpacity'); this.floatBall.style.width = `${size}px`; this.floatBall.style.height = `${size}px`; this.floatBall.style.opacity = opacity; // 确保在可视区域内 this.ensureInViewport(); } createPanel() { const panel = document.createElement('div'); panel.id = 'translate-panel'; // 获取支持的语言列表 const languages = [ { value: 'chinese_simplified', name: '简体中文' }, { value: 'chinese_traditional', name: '繁體中文' }, { value: 'english', name: 'English' }, { value: 'spanish', name: 'Español' }, { value: 'french', name: 'Français' }, { value: 'german', name: 'Deutsch' }, { value: 'japanese', name: '日本語' }, { value: 'korean', name: '한국어' }, { value: 'russian', name: 'Русский' }, { value: 'arabic', name: 'العربية' }, { value: 'portuguese', name: 'Português' }, { value: 'italian', name: 'Italiano' }, { value: 'dutch', name: 'Nederlands' }, { value: 'polish', name: 'Polski' }, { value: 'turkish', name: 'Türkçe' }, { value: 'thai', name: 'ไทย' }, { value: 'vietnamese', name: 'Tiếng Việt' }, { value: 'indonesian', name: 'Bahasa Indonesia' }, { value: 'hindi', name: 'हिन्दी' }, { value: 'hebrew', name: 'עברית' } ]; const mobile = isMobile(); panel.innerHTML = `