support deeplx api

This commit is contained in:
Gabe Yuan
2023-10-17 11:33:26 +08:00
parent f88aa159fc
commit 53181588cf
3 changed files with 57 additions and 0 deletions

View File

@@ -105,6 +105,9 @@ const userscriptWebpack = (config, env) => {
// @connect dav.jianguoyun.com
// @connect fanyi.baidu.com
// @connect localhost:3000
// @connect 127.0.0.1:3000
// @connect localhost:1188
// @connect 127.0.0.1:1188
// @run-at document-end
// ==/UserScript==

View File

@@ -4,6 +4,7 @@ import {
OPT_TRANS_GOOGLE,
OPT_TRANS_MICROSOFT,
OPT_TRANS_DEEPL,
OPT_TRANS_DEEPLX,
OPT_TRANS_OPENAI,
OPT_TRANS_CUSTOMIZE,
OPT_LANGS_SPECIAL,
@@ -154,6 +155,45 @@ const apiDeepLTranslate = async (
return [trText, isSame];
};
/**
* DeepLX翻译
* https://github.com/OwO-Network/DeepLX
* @param {*} text
* @param {*} to
* @param {*} from
* @returns
*/
const apiDeepLXTranslate = async (
translator,
text,
to,
from,
{ url, key, useCache = true }
) => {
const data = {
text,
target_lang: to,
};
if (from) {
data.source_lang = from;
}
const res = await fetchPolyfill(url, {
headers: {
"Content-type": "application/json",
},
method: "POST",
body: JSON.stringify(data),
useCache,
usePool: true,
translator,
token: key,
});
const trText = res.data;
const isSame = to === res.source_lang;
return [trText, isSame];
};
/**
* OpenAI 翻译
* @param {*} text
@@ -291,6 +331,8 @@ export const apiTranslate = ({
return callApi(apiMicrosoftTranslate);
case OPT_TRANS_DEEPL:
return callApi(apiDeepLTranslate);
case OPT_TRANS_DEEPLX:
return callApi(apiDeepLXTranslate);
case OPT_TRANS_OPENAI:
return callApi(apiOpenaiTranslate);
case OPT_TRANS_CUSTOMIZE:

View File

@@ -73,6 +73,7 @@ export const URL_BAIDU_LANGDETECT = "https://fanyi.baidu.com/langdetect";
export const OPT_TRANS_GOOGLE = "Google";
export const OPT_TRANS_MICROSOFT = "Microsoft";
export const OPT_TRANS_DEEPL = "DeepL";
export const OPT_TRANS_DEEPLX = "DeepLX";
export const OPT_TRANS_BAIDU = "Baidu";
export const OPT_TRANS_OPENAI = "OpenAI";
export const OPT_TRANS_CUSTOMIZE = "Custom";
@@ -80,6 +81,7 @@ export const OPT_TRANS_ALL = [
OPT_TRANS_GOOGLE,
OPT_TRANS_MICROSOFT,
OPT_TRANS_DEEPL,
OPT_TRANS_DEEPLX,
OPT_TRANS_OPENAI,
OPT_TRANS_CUSTOMIZE,
];
@@ -138,6 +140,12 @@ export const OPT_LANGS_SPECIAL = {
["zh-CN", "ZH"],
["zh-TW", "ZH"],
]),
[OPT_TRANS_DEEPLX]: new Map([
...OPT_LANGS_FROM.map(([key]) => [key, key.toUpperCase()]),
["auto", ""],
["zh-CN", "ZH"],
["zh-TW", "ZH"],
]),
[OPT_TRANS_BAIDU]: new Map([
...OPT_LANGS_FROM.map(([key]) => [key, key]),
["zh-CN", "zh"],
@@ -285,6 +293,10 @@ export const DEFAULT_TRANS_APIS = {
url: "https://api-free.deepl.com/v2/translate",
key: "",
},
[OPT_TRANS_DEEPLX]: {
url: "http://localhost:1188/translate",
key: "",
},
[OPT_TRANS_OPENAI]: {
url: "https://api.openai.com/v1/chat/completions",
key: "",