Smart Translate with DeepL API

Translate chosen text to another language with DeepL API.

Versione datata 27/12/2024. Vedi la nuova versione l'ultima versione.

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

// ==UserScript==
// @license MIT
// @name         Smart Translate with DeepL API
// @namespace    http://tampermonkey.net/
// @version      1.1.0
// @description  Translate chosen text to another language with DeepL API.
// @author       Twil3akine
// @match        *://*/*
// @match        file:///*
// @grant        GM_xmlhttpRequest
// ==/UserScript==

(function() {
    'use strict';

    const apiKey = 'YOUR_DEEPL_API_KEY';

    console.log('Smart-Translate is started!');
    // set keyboard-shortcut (Alt+T)
    document.addEventListener('keydown', function(event) {
        if (event.altKey && event.key === "t") {
            const selectedText = window.getSelection().toString().trim();
            if (selectedText) {
                // send request to DeepL API
                translateText(selectedText);
            } else {
                alert('Please select text!');
            }
        }
    });

    const translateText = (text) => {
        const url = 'https://api-free.deepl.com/v2/translate';

        const params = {
            auth_key: apiKey,
            text: text,
            target_lang: 'JA',
        };

        GM_xmlhttpRequest({
            method: 'POST',
            url: url,
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded',
            },
            data: new URLSearchParams(params).toString(),
            onload: (response) => {
                try {
                    const result = JSON.parse(response.responseText);
                    const translatedText = result.translations[0].text;
                    alert(`${text}\n\n->\n\n${translatedText}`);
                    console.log(`${text}\n\n->\n\n${translatedText}`);
                } catch (e) {
                    console.log('Error parsing the response from DeepL:', e);
                }
            },
            onerror: (error) => {
                console.error('Error with DeepL API request:', error);
                alert('Error occurred while traslating. Please try again later.');
            }
        });
    }
})();