Translate Buttons for beta.character.ai

Adds a translate buttons for character messages in the chat for beta.character.ai

目前為 2022-12-12 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name        Translate Buttons for beta.character.ai
// @namespace   TranslateButtonsForBetaCharacterAI
// @version     1.2
// @description Adds a translate buttons for character messages in the chat for beta.character.ai
// @icon        https://characterai.io/static/favicon.ico
// @match       https://beta.character.ai/chat?char=*
// @grant       GM_xmlhttpRequest
// @run-at      document-end
// @license     MIT
// ==/UserScript==

function addButtons() {
    var elements = document.getElementsByClassName("msg char-msg");
    for (var i = 0; i < elements.length; i++) {
        const charMsg = elements[i];

        if (charMsg.querySelector(".translate-button") === null) {
            var btn = document.createElement("button");
            btn.innerHTML = "Tr";
            btn.classList.add("translate-button");
            btn.style.cssText = "width: 30px; height: 30px;";

            // Add event listener for click event on translate button
            btn.addEventListener("click", function() {
                const msgElements = charMsg.querySelectorAll('p, li');
                for (var j = 0; j < msgElements.length; j++) {
                    const msgElement = msgElements[j];
                    const text = msgElement.textContent;

                    if (msgElement.originalText != null) {
                        console.log("Return original message text: " + msgElement.originalText);
                        msgElement.textContent = msgElement.originalText;
                        msgElement.originalText = null;
                        continue;
                    }

                    console.log("Message text: " + text);
                    msgElement.originalText = text;

                    // Use the user's language as the target language in the API request
                    var url = "https://translate.googleapis.com/translate_a/single?client=gtx&sl=auto&tl=" + navigator.language + "&dt=t&q=" + encodeURIComponent(text);

                    GM_xmlhttpRequest({
                        method: "GET",
                        url: url,
                        onload: function(response) {
                            const translation = JSON.parse(response.responseText)[0][0][0];

                            // Log the translation
                            console.log("Translated text: " + translation);

                            // Replace the original text with the translated text
                            msgElement.textContent = translation;
                        }
                    });
                }
            });

            // Log the button being added
            console.log("Added translate button for element: " + charMsg);
            charMsg.insertBefore(btn, charMsg.firstChild);
        }
    }
}

setInterval(addButtons, 1000); // Check every second