Traduttore MyMemory con Scelta Lingua

Traduci testo selezionato usando MyMemory con scelta della lingua. Lingue supportate: "Italiano": "it", "Inglese": "en", "Francese": "fr", "Spagnolo": "es", "Tedesco": "de","Portoghese": "pt", "Russo": "ru","Cinese": "zh", "Giapponese": "ja", "Coreano": "ko"

当前为 2024-09-17 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Traduttore MyMemory con Scelta Lingua
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Traduci testo selezionato usando MyMemory con scelta della lingua. Lingue supportate: "Italiano": "it", "Inglese": "en", "Francese": "fr", "Spagnolo": "es", "Tedesco": "de","Portoghese": "pt", "Russo": "ru","Cinese": "zh", "Giapponese": "ja", "Coreano": "ko"
// @author       Magneto1
// @match        *://*/*
// @license      MIT
// @grant        GM_xmlhttpRequest
// @grant        GM_notification
// @grant        GM_registerMenuCommand
// ==/UserScript==

(function() {
    'use strict';

    let selectedText = '';

    // Funzione per tradurre il testo usando MyMemory
    function translateWithMyMemory(text, sourceLang, targetLang) {
        const url = `https://api.mymemory.translated.net/get?q=${encodeURIComponent(text)}&langpair=${sourceLang}|${targetLang}`;

        GM_xmlhttpRequest({
            method: "GET",
            url: url,
            onload: function(response) {
                try {
                    const data = JSON.parse(response.responseText);
                    if (data && data.responseData && data.responseData.translatedText) {
                        GM_notification({ title: "Traduzione MyMemory", text: data.responseData.translatedText });
                    } else {
                        GM_notification({ title: "Errore", text: "Nessuna traduzione trovata." });
                    }
                } catch (error) {
                    GM_notification({ title: "Errore", text: "Si è verificato un errore nella risposta." });
                }
            },
            onerror: function() {
                GM_notification({ title: "Errore", text: "Impossibile contattare il servizio di traduzione." });
            }
        });
    }

    // Funzione per registrare il comando di traduzione
    function registerTranslateCommand() {
        GM_registerMenuCommand("Traduci", () => {
            if (selectedText) {
                const sourceLang = prompt("Inserisci la lingua di origine (es. 'en' per inglese, 'it' per italiano):");
                const targetLang = prompt("Inserisci la lingua di destinazione (es. 'en' per inglese, 'it' per italiano):");

                if (sourceLang && targetLang) {
                    translateWithMyMemory(selectedText, sourceLang, targetLang);
                } else {
                    GM_notification({ title: "Errore", text: "Lingua di origine o destinazione non valida." });
                }
            } else {
                GM_notification({ title: "Errore", text: "Nessun testo selezionato." });
            }
        });
    }

    // Aggiungi un listener per la selezione del testo
    document.addEventListener('mouseup', function() {
        selectedText = window.getSelection().toString();
        registerTranslateCommand(); // Registra il comando ogni volta che si seleziona del testo
    });
})();