Smart Translate with DeepL API

Translate chosen text to another language with DeepL API.

ของเมื่อวันที่ 28-12-2024 ดู เวอร์ชันล่าสุด

คุณจะต้องติดตั้งส่วนขยาย เช่น Tampermonkey, Greasemonkey หรือ Violentmonkey เพื่อติดตั้งสคริปต์นี้

คุณจะต้องติดตั้งส่วนขยาย เช่น Tampermonkey หรือ Violentmonkey เพื่อติดตั้งสคริปต์นี้

คุณจะต้องติดตั้งส่วนขยาย เช่น Tampermonkey หรือ Violentmonkey เพื่อติดตั้งสคริปต์นี้

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

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

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

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

(function () {
    'use strict';

    // APIキーを取得または保存
    const getApiKey = async () => {
        let apiKey = await GM_getValue('DEEPL_API_KEY', null);
        if (!apiKey) {
            apiKey = prompt('Enter your DeepL API key:');
            if (apiKey) {
                await GM_setValue('DEEPL_API_KEY', apiKey);
            } else {
                alert('API key is required to use this script.');
            }
        }
        return apiKey;
    };

    const translateText = async (text, lang) => {
        const apiKey = await getApiKey();
        if (!apiKey) return;

        const url = 'https://api-free.deepl.com/v2/translate';
        const params = {
            auth_key: apiKey,
            text: text,
            target_lang: lang,
        };

        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.error('Error parsing the response from DeepL:', e);
                    alert('An error occurred while processing the translation.');
                }
            },
            onerror: (error) => {
                console.error('Error with DeepL API request:', error);
                alert('Error occurred while translating. Please try again later.');
            },
        });
    };

    console.log('Smart-Translate is started!');
    document.addEventListener('keydown', async (event) => {
        if (event.altKey && event.key === 't') {
            const selectedText = window.getSelection().toString().trim();
            if (selectedText) {
                await translateText(selectedText, 'JA');
            } else {
                alert('Please select text!');
            }
        }

        if (event.altKey && event.key === 'r') {
            const apiKey = await getApiKey();
            const newKey = prompt(`Please input your new API key\nNow API key: ${apiKey}`);
            if (newKey) {
                await GM_setValue('DEEPL_API_KEY', newKey);
                alert('API key updated successfully.');
            }
        }
    });
})();