English to Portuguese Live Botghost Translator

Translates English text to Portuguese on Botghost

当前为 2023-04-23 提交的版本,查看 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         English to Portuguese Live Botghost Translator 
// @namespace    https://thelostmoon.net
// @license      MIT
// @version      2.6.9
// @description  Translates English text to Portuguese on Botghost
// @match        http://https://dashboard.botghost.com/*
// @match        https://dashboard.botghost.com/*
// @grant        GM_xmlhttpRequest
// ==/UserScript==

(function() {
    'use strict';

    // Google Translate API URL
    const API_URL = "https://translate.googleapis.com/translate_a/single?client=gtx&sl=en&tl=pt&dt=t&q=";

    // Translate function
    function translate(text, callback) {
        const url = API_URL + encodeURIComponent(text);
        GM_xmlhttpRequest({
            method: "GET",
            url: url,
            onload: function(response) {
                const translation = JSON.parse(response.responseText)[0][0][0];
                callback(translation);
            }
        });
    }

    // Translate all text nodes in the given DOM node
    function translateNode(node) {
        if (node.nodeType === Node.TEXT_NODE) {
            const text = node.textContent.trim();
            if (text) {
                translate(text, function(translation) {
                    node.textContent = translation;
                });
            }
        } else if (node.nodeType === Node.ELEMENT_NODE) {
            const children = node.childNodes;
            for (let i = 0; i < children.length; i++) {
                translateNode(children[i]);
            }
        }
    }

    // Translate all text nodes in the given subtree
    function translateSubtree(subtree) {
        const nodes = subtree.querySelectorAll('*');
        for (let i = 0; i < nodes.length; i++) {
            translateNode(nodes[i]);
        }
    }

    // Observe the DOM for changes and translate any new content that gets added
    const observer = new MutationObserver(function(mutations) {
        for (let i = 0; i < mutations.length; i++) {
            const mutation = mutations[i];
            if (mutation.type === "childList" && mutation.addedNodes.length > 0) {
                for (let j = 0; j < mutation.addedNodes.length; j++) {
                    const addedNode = mutation.addedNodes[j];
                    translateSubtree(addedNode);
                }
            }
        }
    });
    observer.observe(document.body, { childList: true, subtree: true });

    // Translate all existing content when the script is first loaded
    translateSubtree(document.body);
})();