From 24d904b32cc8bc3b421eb1c32ebcbfe0c8ae009b Mon Sep 17 00:00:00 2001 From: Gabe Date: Mon, 30 Jun 2025 21:34:37 +0800 Subject: [PATCH] feat: support volcengine api --- config-overrides.js | 1 + src/apis/index.js | 5 +++++ src/apis/trans.js | 22 ++++++++++++++++++++++ src/config/index.js | 13 +++++++++++++ src/views/Options/Apis.js | 2 ++ 5 files changed, 43 insertions(+) diff --git a/config-overrides.js b/config-overrides.js index 500deb0..654e556 100644 --- a/config-overrides.js +++ b/config-overrides.js @@ -110,6 +110,7 @@ const userscriptWebpack = (config, env) => { // @connect fanyi.baidu.com // @connect transmart.qq.com // @connect niutrans.com +// @connect translate.volcengine.com // @connect localhost:3000 // @connect 127.0.0.1:3000 // @connect localhost:1188 diff --git a/src/apis/index.js b/src/apis/index.js index 5156d5e..41f3302 100644 --- a/src/apis/index.js +++ b/src/apis/index.js @@ -10,6 +10,7 @@ import { OPT_TRANS_NIUTRANS, OPT_TRANS_BAIDU, OPT_TRANS_TENCENT, + OPT_TRANS_VOLCENGINE, OPT_TRANS_OPENAI, OPT_TRANS_OPENAI_2, OPT_TRANS_OPENAI_3, @@ -305,6 +306,10 @@ export const apiTranslate = async ({ trText = res?.auto_translation?.[0]; isSame = text === trText; break; + case OPT_TRANS_VOLCENGINE: + trText = res?.translation || ""; + isSame = to === res?.detected_language; + break; case OPT_TRANS_OPENAI: case OPT_TRANS_OPENAI_2: case OPT_TRANS_OPENAI_3: diff --git a/src/apis/trans.js b/src/apis/trans.js index 0f95517..5cab6fc 100644 --- a/src/apis/trans.js +++ b/src/apis/trans.js @@ -9,6 +9,7 @@ import { OPT_TRANS_NIUTRANS, OPT_TRANS_BAIDU, OPT_TRANS_TENCENT, + OPT_TRANS_VOLCENGINE, OPT_TRANS_OPENAI, OPT_TRANS_OPENAI_2, OPT_TRANS_OPENAI_3, @@ -25,6 +26,7 @@ import { OPT_TRANS_CUSTOMIZE_5, URL_MICROSOFT_TRAN, URL_TENCENT_TRANSMART, + URL_VOLCENGINE_TRAN, INPUT_PLACE_URL, INPUT_PLACE_FROM, INPUT_PLACE_TO, @@ -206,6 +208,24 @@ const genTencent = ({ text, from, to }) => { return [URL_TENCENT_TRANSMART, init]; }; +const genVolcengine = ({ text, from, to }) => { + const data = { + source_language: from, + target_language: to, + text: text, + }; + + const init = { + headers: { + "Content-type": "application/json", + }, + method: "POST", + body: JSON.stringify(data), + }; + + return [URL_VOLCENGINE_TRAN, init]; +}; + const genOpenAI = ({ text, from, @@ -476,6 +496,8 @@ export const genTransReq = ({ translator, text, from, to }, apiSetting) => { return genBaidu(args); case OPT_TRANS_TENCENT: return genTencent(args); + case OPT_TRANS_VOLCENGINE: + return genVolcengine(args); case OPT_TRANS_OPENAI: case OPT_TRANS_OPENAI_2: case OPT_TRANS_OPENAI_3: diff --git a/src/config/index.js b/src/config/index.js index e12802d..11ecff7 100644 --- a/src/config/index.js +++ b/src/config/index.js @@ -101,6 +101,7 @@ export const URL_BAIDU_TRANSAPI = "https://fanyi.baidu.com/transapi"; export const URL_BAIDU_TRANSAPI_V2 = "https://fanyi.baidu.com/v2transapi"; export const URL_DEEPLFREE_TRAN = "https://www2.deepl.com/jsonrpc"; export const URL_TENCENT_TRANSMART = "https://transmart.qq.com/api/imt"; +export const URL_VOLCENGINE_TRAN = "https://translate.volcengine.com/crx/translate/v1"; export const URL_NIUTRANS_REG = "https://niutrans.com/login?active=3&userSource=kiss-translator"; @@ -118,6 +119,7 @@ export const OPT_TRANS_DEEPLFREE = "DeepLFree"; export const OPT_TRANS_NIUTRANS = "NiuTrans"; export const OPT_TRANS_BAIDU = "Baidu"; export const OPT_TRANS_TENCENT = "Tencent"; +export const OPT_TRANS_VOLCENGINE = "Volcengine"; export const OPT_TRANS_OPENAI = "OpenAI"; export const OPT_TRANS_OPENAI_2 = "OpenAI2"; export const OPT_TRANS_OPENAI_3 = "OpenAI3"; @@ -138,6 +140,7 @@ export const OPT_TRANS_ALL = [ OPT_TRANS_MICROSOFT, OPT_TRANS_BAIDU, OPT_TRANS_TENCENT, + OPT_TRANS_VOLCENGINE, OPT_TRANS_DEEPL, OPT_TRANS_DEEPLFREE, OPT_TRANS_DEEPLX, @@ -238,6 +241,12 @@ export const OPT_LANGS_SPECIAL = { ["zh-CN", "zh"], ["zh-TW", "cht"], ]), + [OPT_TRANS_VOLCENGINE]: new Map([ + ...OPT_LANGS_FROM.map(([key]) => [key, key]), + ["auto", "auto"], + ["zh-CN", "zh"], + ["zh-TW", "zh-Hant"], + ]), [OPT_TRANS_BAIDU]: new Map([ ...OPT_LANGS_FROM.map(([key]) => [key, key]), ["zh-CN", "zh"], @@ -578,6 +587,10 @@ export const DEFAULT_TRANS_APIS = { fetchLimit: DEFAULT_FETCH_LIMIT, fetchInterval: DEFAULT_FETCH_INTERVAL, }, + [OPT_TRANS_VOLCENGINE]: { + fetchLimit: DEFAULT_FETCH_LIMIT, + fetchInterval: DEFAULT_FETCH_INTERVAL, + }, [OPT_TRANS_DEEPL]: { url: "https://api-free.deepl.com/v2/translate", key: "", diff --git a/src/views/Options/Apis.js b/src/views/Options/Apis.js index adb952c..034c7c1 100644 --- a/src/views/Options/Apis.js +++ b/src/views/Options/Apis.js @@ -11,6 +11,7 @@ import { OPT_TRANS_DEEPLFREE, OPT_TRANS_BAIDU, OPT_TRANS_TENCENT, + OPT_TRANS_VOLCENGINE, OPT_TRANS_OPENAI, OPT_TRANS_OPENAI_2, OPT_TRANS_OPENAI_3, @@ -157,6 +158,7 @@ function ApiFields({ translator }) { OPT_TRANS_DEEPLFREE, OPT_TRANS_BAIDU, OPT_TRANS_TENCENT, + OPT_TRANS_VOLCENGINE, ]; const mulkeysTranslators = [