Live Botghost Translator

Translates English text to Portuguese on Botghost

目前為 2023-05-20 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Live Botghost Translator 
// @namespace    https://thelostmoon.net
// @license      MIT
// @version      3.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';

  // Language options
  const languages = [
  { code: "af", name: "Afrikaans" },
  { code: "ar", name: "Arabic" },
  { code: "bn", name: "Bengali" },
  { code: "zh-CN", name: "Chinese (Simplified)" },
  { code: "zh-TW", name: "Chinese (Traditional)" },
  { code: "en", name: "English" },
  { code: "fr", name: "French" },
  { code: "de", name: "German" },
  { code: "hi", name: "Hindi" },
  { code: "it", name: "Italian" },
  { code: "ja", name: "Japanese" },
  { code: "ko", name: "Korean" },
  { code: "pt", name: "Portuguese" },
  { code: "ru", name: "Russian" },
  { code: "es", name: "Spanish" },
  { code: "th", name: "Thai" },
];


  // Language selection dropdown
  function createLanguageDropdown() {
    const dropdown = document.createElement("select");
    dropdown.style.marginRight = "10px";

    for (let i = 0; i < languages.length; i++) {
      const option = document.createElement("option");
      option.value = languages[i].code;
      option.text = languages[i].name;
      dropdown.appendChild(option);
    }

    return dropdown;
  }

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

  // Translate function
  function translate(text, language, callback) {
    const url = API_BASE_URL + encodeURIComponent(text) + "&tl=" + language;
    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, language) {
    if (node.nodeType === Node.TEXT_NODE) {
      const text = node.textContent.trim();
      if (text) {
        translate(text, language, 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], language);
      }
    }
  }

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

  // 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, selectedLanguage);
        }
      }
    }
  });
  observer.observe(document.body, { childList: true, subtree: true });

  // Translate all existing content when the script is first loaded
  let selectedLanguage = "";
  const languageDropdown = createLanguageDropdown();
  languageDropdown.addEventListener("change", function() {
    selectedLanguage = languageDropdown.value;
    translateSubtree(document.body, selectedLanguage);
  });

  // Add the language dropdown to the page
  const header = document.querySelector(".header");
  if (header) {
    header.appendChild(languageDropdown);
  }
})();