Auto Translate to English

Automatically translate web pages to English

当前为 2024-02-17 提交的版本,查看 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Auto Translate to English
// @namespace    https://discord.gg/VnxvMFbKbu
// @version      0.1
// @description  Automatically translate web pages to English
// @author       Pixel.Pilot
// @match        http://*/*
// @match        https://*/*
// @grant        GM_xmlhttpRequest
// @license You are not allowed to reuse this script for any purpose.
// ==/UserScript==

(function() {
    'use strict';

    // Function to translate text using Google Translate API
    function translateText(text, onSuccess, onError) {
        const url = `https://translate.googleapis.com/translate_a/single?client=gtx&sl=auto&tl=en&dt=t&q=${encodeURIComponent(text)}`;
        GM_xmlhttpRequest({
            method: 'GET',
            url: url,
            onload: function(response) {
                const jsonResponse = JSON.parse(response.responseText);
                const translatedText = jsonResponse[0][0][0];
                if (translatedText) {
                    onSuccess(translatedText);
                } else {
                    onError();
                }
            },
            onerror: function(error) {
                onError(error);
            }
        });
    }

    // Function to traverse and translate all visible elements
    function translateAllVisibleElements() {
        const allElements = document.querySelectorAll('*');
        allElements.forEach(element => {
            if (element.nodeType === Node.TEXT_NODE) {
                translateText(element.textContent.trim(), 
                              translatedText => {
                                  element.textContent = translatedText;
                              },
                              () => {
                                  console.log(`Translation failed for: ${element.textContent}`);
                              });
            } else if (element.hasChildNodes()) {
                translateAllVisibleElementsRecursive(element);
            }
        });
    }

    // Recursive function to traverse and translate all visible child elements
    function translateAllVisibleElementsRecursive(element) {
        element.childNodes.forEach(childNode => {
            if (childNode.nodeType === Node.TEXT_NODE) {
                translateText(childNode.textContent.trim(), 
                              translatedText => {
                                  childNode.textContent = translatedText;
                              },
                              () => {
                                  console.log(`Translation failed for: ${childNode.textContent}`);
                              });
            } else if (childNode.hasChildNodes()) {
                translateAllVisibleElementsRecursive(childNode);
            }
        });
    }

    // Call the translation function on all visible elements
    translateAllVisibleElements();
})();