Greasy Fork

Greasy Fork is available in English.

网页文本翻译工具

选中文本后右键翻译成中文

当前为 2025-06-02 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         网页文本翻译工具
// @namespace    http://tampermonkey.net/
// @version      1.0 bate
// @description  选中文本后右键翻译成中文
// @author       Your name
// @match        *://*/*
// @grant        GM_xmlhttpRequest
// @grant        GM_addStyle
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    // 添加样式
    GM_addStyle(`
        .translation-popup {
            position: fixed;
            top: 10px;
            right: 10px;
            padding: 10px;
            background: white;
            border: 1px solid #ccc;
            border-radius: 4px;
            z-index: 10000;
            max-width: 300px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.2);
        }
        .translation-close {
            position: absolute;
            right: 5px;
            top: 5px;
            border: none;
            background: none;
            cursor: pointer;
        }
    `);

    // 创建右键菜单
    document.addEventListener('contextmenu', function(event) {
        const selectedText = window.getSelection().toString().trim();
        if (selectedText) {
            event.preventDefault();
            const contextMenu = document.createElement('div');
            contextMenu.className = 'translation-popup';
            contextMenu.style.top = event.pageY + 'px';
            contextMenu.style.left = event.pageX + 'px';
            contextMenu.innerHTML = '<button class="translation-menu-item">翻译选中文本</button>';
            
            document.body.appendChild(contextMenu);
            
            const menuItem = contextMenu.querySelector('.translation-menu-item');
            menuItem.addEventListener('click', () => {
                translateText(selectedText);
                contextMenu.remove();
            });
            
            // 点击其他地方关闭菜单
            document.addEventListener('click', function closeMenu() {
                contextMenu.remove();
                document.removeEventListener('click', closeMenu);
            });
        }
    });

    function showTranslation(translatedText) {
        const div = document.createElement('div');
        div.className = 'translation-popup';
        div.textContent = translatedText;

        const closeButton = document.createElement('button');
        closeButton.className = 'translation-close';
        closeButton.textContent = '×';
        closeButton.onclick = () => div.remove();
        div.appendChild(closeButton);

        document.body.appendChild(div);
        
        setTimeout(() => div.remove(), 5000);
    }

    function translateText(text) {
        GM_xmlhttpRequest({
            method: 'GET',
            url: `https://api.mymemory.translated.net/get?q=${encodeURIComponent(text)}&langpair=auto|zh`,
            onload: function(response) {
                try {
                    const data = JSON.parse(response.responseText);
                    if (data.responseStatus === 200) {
                        showTranslation(data.responseData.translatedText);
                    }
                } catch (error) {
                    console.error('Translation error:', error);
                }
            },
            onerror: function(error) {
                console.error('Request error:', error);
            }
        });
    }
})();