translatorTest

translate from japanise to chinese use translate

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         translatorTest
// @namespace    http://tampermonkey.net/
// @version      2024-10-06
// @description  translate from japanise to chinese use translate
// @author       Rexkny
// @license      MIT
// @match        */1.RpgxViewer/index.html
// @icon         data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
// @grant        none
// @require      https://cdn.jsdelivr.net/npm/translate@1/translate.min.js
// ==/UserScript==

(function() {
    'use strict';
    const translatedTextBox = document.createElement("div")
    translatedTextBox.innerText = "翻译内容将在下方显示:"
    translatedTextBox.style.color = 'white'
    const translateSpan = document.createElement("span")
    translateSpan.style.display = 'block'
    const contentWrap = document.querySelector('#body-wrapper')
    contentWrap.appendChild(translatedTextBox)
    translatedTextBox.appendChild(translateSpan)
    const doTranslate = (strTobeTranslated, targetDom, sourceLanguage, targetLanguage) => {
        const options = {}
        options.engine = 'google'; // 使用Google翻译
        options.from = sourceLanguage || 'ja'; // 源语言为日语
        options.to = targetLanguage || 'zh'; // 目标语言为中文
        options.cache = 2000; // 请求时长2000ms

        translate(strTobeTranslated, options).then(res => {
            translateSpan.innerText = res
        })
    }
    // 获取场景父元素
    var scenceView = document.querySelector('#scene-viewer')
    const config = { childList: true, subtree: true };
    const callback = (mutationsList) => { // 父元素更新时获取对话框文本
        const textBox = document.querySelector('.text-box-text')
        if (textBox && textBox.innerText) {
            const formattedText = textBox.innerText.replace(/[\r\n]+/g, '')
            doTranslate(textBox.innerText, textBox)
        }
    }
    const textBoxObserver = new MutationObserver(callback);
    textBoxObserver.observe(scenceView, config);
    document.addEventListener('beforeunload', () => {
        textBoxObserver.disconnect()
    })
})();