Live Botghost Translator

Translates English text to the selected language on Botghost (Overlay)

当前为 2023-05-20 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Live Botghost Translator
// @namespace    https://thelostmoon.net
// @license      MIT
// @version      3.9.1
// @description  Translates English text to the selected language on Botghost (Overlay)
// @match        http://https://dashboard.botghost.com/*
// @match        https://dashboard.botghost.com/*
// @grant        none
// ==/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" },
  ];

  // Create overlay and center the dropdown
  const overlay = document.createElement("div");
  overlay.style.position = "fixed";
  overlay.style.top = "0";
  overlay.style.left = "0";
  overlay.style.width = "100%";
  overlay.style.height = "100%";
  overlay.style.background = "rgba(0, 0, 0, 0.5)";
  overlay.style.display = "flex";
  overlay.style.alignItems = "center";
  overlay.style.justifyContent = "center";
  overlay.style.zIndex = "9999";

  // Language selection dropdown
  function createLanguageDropdown() {
    const dropdownContainer = document.createElement("div");
    dropdownContainer.style.display = "flex";
    dropdownContainer.style.flexDirection = "column";
    dropdownContainer.style.alignItems = "center";
    dropdownContainer.style.gap = "5px";

    // Close button (X)
    const closeButton = document.createElement("div");
    closeButton.innerHTML = "X";
    closeButton.style.cursor = "pointer";
    closeButton.style.fontSize = "16px";
    closeButton.style.fontWeight = "bold";
    closeButton.style.color = "#fff";
    closeButton.addEventListener("click", function() {
      document.body.removeChild(overlay);
    });

    // Language dropdown
    const dropdown = document.createElement("select");
    dropdown.style.width = "200px";

    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);
    }

    // OK button
    const okButton = document.createElement("button");
    okButton.innerHTML = "OK";
    okButton.style.padding = "5px 10px";
    okButton.style.borderRadius = "4px";
    okButton.style.backgroundColor = "#4CAF50";
    okButton.style.border = "none";
    okButton.style.color = "#fff";
    okButton.addEventListener("click", function() {
      selectedLanguage = dropdown.value;
      translateSubtree(document.body, selectedLanguage);
      document.body.removeChild(overlay);
    });

    dropdownContainer.appendChild(closeButton);
    dropdownContainer.appendChild(dropdown);
    dropdownContainer.appendChild(okButton);

    return dropdownContainer;
  }

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

  // Translate function
  async function translate(text, language, callback) {
    const url = API_BASE_URL + encodeURIComponent(text) + "&tl=" + language;

    try {
      const response = await fetch(url);
      const data = await response.json();
      const translation = data[0][0][0];
      callback(translation);
    } catch (error) {
      console.error("Translation error:", error);
    }
  }

  // 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);
    }
  }

  // Add overlay and language dropdown when the script is first loaded
  let selectedLanguage = "";
  const languageDropdown = createLanguageDropdown();
  overlay.appendChild(languageDropdown);
  document.body.appendChild(overlay);
})();