From 9397ea7178610df469e18a6b0773dddf21019dee Mon Sep 17 00:00:00 2001 From: vladkryv <83036866+vladkryv@users.noreply.github.com> Date: Tue, 19 Sep 2023 20:51:54 +0300 Subject: [PATCH] Add support deepl pro api --- src/background/translationService.js | 20 ++++++++--------- src/options/options.html | 8 +++---- src/options/options.js | 32 ++++++++++++++-------------- 3 files changed, 30 insertions(+), 30 deletions(-) diff --git a/src/background/translationService.js b/src/background/translationService.js index 938cc563..772dc034 100644 --- a/src/background/translationService.js +++ b/src/background/translationService.js @@ -1260,16 +1260,16 @@ const translationService = (function () { }; /** - * Creates the DeepLFreeApi translation service + * Creates the DeepLApi translation service * @param {string} apiKey * @returns {Service} libreService */ - const createDeeplFreeApiService = (apiKey) => { + const createDeeplApiService = (apiKey) => { return new (class extends Service { constructor() { super( "deepl", - "https://api-free.deepl.com/v2/translate", + apiKey.endsWith(":fx") ? "https://api-free.deepl.com/v2/translate" : "https://api.deepl.com/v2/translate", "POST", function cbTransformRequest(sourceArray) { return sourceArray[0]; @@ -1510,12 +1510,12 @@ const translationService = (function () { ); } else if (request.action === "removeLibreService") { serviceList.delete("libre"); - } else if (request.action === "createDeeplFreeApiService") { + } else if (request.action === "createDeeplApiService") { serviceList.set( "deepl", - createDeeplFreeApiService(request.deepl_freeapi.apiKey) + createDeeplApiService(request.deepl_api.apiKey) ); - } else if (request.action === "removeDeeplFreeApiService") { + } else if (request.action === "removeDeeplApiService") { serviceList.set( "deepl", /** @type {Service} */ /** @type {?} */ (deeplService) @@ -1532,12 +1532,12 @@ const translationService = (function () { } if ( - twpConfig.get("customServices").find((cs) => cs.name === "deepl_freeapi") + twpConfig.get("customServices").find((cs) => cs.name === "deepl_api") ) { - const deepl_freeapi = twpConfig + const deepl_api = twpConfig .get("customServices") - .find((cs) => cs.name === "deepl_freeapi"); - serviceList.set("deepl", createDeeplFreeApiService(deepl_freeapi.apiKey)); + .find((cs) => cs.name === "deepl_api"); + serviceList.set("deepl", createDeeplApiService(deepl_api.apiKey)); } }); diff --git a/src/options/options.html b/src/options/options.html index e467b51b..7ca0a799 100644 --- a/src/options/options.html +++ b/src/options/options.html @@ -576,13 +576,13 @@

Configure your Libretranslate server for text (not page) translation.


-

Configure a DeepL Free API API key to translate text (not pages) without opening new tabs.

+

Configure a DeepL Free or Pro API key to translate text (not pages) without opening new tabs.




- - + +

@@ -692,4 +692,4 @@

Configure a DeepL Free API API key to translate text (not pages) without ope - \ No newline at end of file + diff --git a/src/options/options.js b/src/options/options.js index 9f6aeebf..92b02813 100644 --- a/src/options/options.js +++ b/src/options/options.js @@ -1289,10 +1289,10 @@ twpConfig $("#libreKEY").value = libre.apiKey; } - async function testDeepLFreeApiKey(apiKey) { + async function testDeepLApiKey(apiKey) { return await new Promise((resolve) => { const xhttp = new XMLHttpRequest(); - xhttp.open("GET", "https://api-free.deepl.com/v2/usage"); + xhttp.open("GET", apiKey.endsWith(":fx") ? "https://api-free.deepl.com/v2/usage" : "https://api.deepl.com/v2/usage"); xhttp.responseType = "json"; xhttp.setRequestHeader("Authorization", "DeepL-Auth-Key " + apiKey); xhttp.onload = () => { @@ -1303,28 +1303,28 @@ twpConfig } $("#addDeepL").onclick = async () => { - const deepl_freeapi = { - name: "deepl_freeapi", + const deepl_api = { + name: "deepl_api", apiKey: $("#deeplKEY").value, }; try { - const response = await testDeepLFreeApiKey(deepl_freeapi.apiKey); + const response = await testDeepLApiKey(deepl_api.apiKey); $("#deeplApiResponse").textContent = JSON.stringify(response); if (response) { const customServices = twpConfig.get("customServices"); const index = customServices.findIndex( - (cs) => cs.name === "deepl_freeapi" + (cs) => cs.name === "deepl_api" ); if (index !== -1) { customServices.splice(index, 1); } - customServices.push(deepl_freeapi); + customServices.push(deepl_api); twpConfig.set("customServices", customServices); chrome.runtime.sendMessage({ - action: "createDeeplFreeApiService", - deepl_freeapi, + action: "createDeeplApiService", + deepl_api, }); } else { alert("Invalid API key"); @@ -1337,13 +1337,13 @@ twpConfig $("#removeDeepL").onclick = () => { const customServices = twpConfig.get("customServices"); const index = customServices.findIndex( - (cs) => cs.name === "deepl_freeapi" + (cs) => cs.name === "deepl_api" ); if (index !== -1) { customServices.splice(index, 1); twpConfig.set("customServices", customServices); chrome.runtime.sendMessage( - { action: "removeDeeplFreeApiService" }, + { action: "removeDeeplApiService" }, checkedLastError ); } @@ -1351,12 +1351,12 @@ twpConfig $("#deeplApiResponse").textContent = ""; }; - const deepl_freeapi = twpConfig + const deepl_api = twpConfig .get("customServices") - .find((cs) => cs.name === "deepl_freeapi"); - if (deepl_freeapi) { - $("#deeplKEY").value = deepl_freeapi.apiKey; - testDeepLFreeApiKey(deepl_freeapi.apiKey).then((response) => { + .find((cs) => cs.name === "deepl_api"); + if (deepl_api) { + $("#deeplKEY").value = deepl_api.apiKey; + testDeepLApiKey(deepl_api.apiKey).then((response) => { $("#deeplApiResponse").textContent = JSON.stringify(response); }); }