您需要先安装一个扩展,例如 篡改猴、Greasemonkey 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 Userscripts ,之后才能安装此脚本。
您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。
您需要先安装用户脚本管理器扩展后才能安装此脚本。
Automatically translate web pages to English
当前为
// ==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(); })();