Skip to content

Commit

Permalink
Add support deepl pro api
Browse files Browse the repository at this point in the history
  • Loading branch information
vladkryv committed Sep 19, 2023
1 parent 6a8b1d9 commit 9397ea7
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 30 deletions.
20 changes: 10 additions & 10 deletions src/background/translationService.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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)
Expand All @@ -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));
}
});

Expand Down
8 changes: 4 additions & 4 deletions src/options/options.html
Original file line number Diff line number Diff line change
Expand Up @@ -576,13 +576,13 @@ <h3>Configure your Libretranslate server for text (not page) translation.</h3>
</div>
<hr>
<div>
<h3>Configure a DeepL Free API API key to translate text (not pages) without opening new tabs.</h3>
<h3>Configure a DeepL Free or Pro API key to translate text (not pages) without opening new tabs.</h3>
<label for="deeplKEY">Your API key</label><br>
<input type="text" id="deeplKEY" placeholder="7cad3e19-32b8-4cac-a03d-64f3c1e1c1be:fx" style="width: 90%;"
autocomplete="off" spellcheck="false"><br>
<br>
<button id="addDeepL">Add DeepL Free API</button>
<button id="removeDeepL">Remove DeepL Free API</button>
<button id="addDeepL">Add DeepL API</button>
<button id="removeDeepL">Remove DeepL API</button>
<p id="deeplApiResponse"></p>
</div>
</div>
Expand Down Expand Up @@ -692,4 +692,4 @@ <h3>Configure a DeepL Free API API key to translate text (not pages) without ope
<script src="options.js"></script>
</body>

</html>
</html>
32 changes: 16 additions & 16 deletions src/options/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = () => {
Expand All @@ -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");
Expand All @@ -1337,26 +1337,26 @@ 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
);
}
$("#deeplKEY").value = "";
$("#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);
});
}
Expand Down

0 comments on commit 9397ea7

Please sign in to comment.