Auto Translate to English (LibreTranslate)

Automatically translate selected text to English using LibreTranslate API.

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

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

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

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

您需要先安装一款用户脚本管理器扩展,例如 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
        }
    });
})();