Auto Translate to English (LibreTranslate)

Automatically translate selected text to English using LibreTranslate API.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Auto Translate to English (LibreTranslate)
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Automatically translate selected text to English using LibreTranslate API.
// @author       Da cat
// @match        *://*/*
// @grant        GM_setClipboard
// ==/UserScript==

(function() {
    'use strict';

    const LIBRETRANSLATE_API_URL = 'https://libretranslate.com/translate'; // LibreTranslate API endpoint

    // Function to translate the selected text
    function translateText(text) {
        const data = {
            q: text,
            source: 'auto', // Auto-detect language
            target: 'en', // Translate to English
            format: 'text'
        };

        const xhr = new XMLHttpRequest();
        xhr.withCredentials = true;

        xhr.addEventListener('readystatechange', function () {
            if (this.readyState === this.DONE) {
                const response = JSON.parse(this.responseText);
                console.log('Translation Response:', response);

                // Check if the response has a translation
                if (response && response.translatedText) {
                    const translatedText = response.translatedText;
                    alert(`Translated Text: ${translatedText}`);

                    // Optionally, copy the translated text to the clipboard
                    GM_setClipboard(translatedText);
                    console.log('Translated text copied to clipboard');
                } else {
                    alert('Translation failed. Please check the console for details.');
                }
            }
        });

        xhr.open('POST', LIBRETRANSLATE_API_URL);
        xhr.setRequestHeader('Content-Type', 'application/json');

        xhr.send(JSON.stringify(data));
    }

    // Function to handle context menu click
    function handleContextMenu(event) {
        const selectedText = window.getSelection().toString().trim();
        if (selectedText) {
            translateText(selectedText);
        } else {
            alert('Please select some text to translate.');
        }
    }

    // Add context menu command
    document.addEventListener('contextmenu', function(event) {
        const selectedText = window.getSelection().toString().trim();
        if (selectedText) {
            event.preventDefault(); // Prevent default context menu
            handleContextMenu(event); // Call the translate function
        }
    });
})();