From e674c7ff28693fc9a535e42fab29598a6ba8b27a Mon Sep 17 00:00:00 2001 From: Ling-yunchi Date: Wed, 18 Dec 2024 20:28:06 +0800 Subject: [PATCH] feat(services): add bing sentence translate service --- addon/locale/en-US/addon.ftl | 1 + addon/locale/en-US/panel.ftl | 2 + addon/locale/it-IT/addon.ftl | 1 + addon/locale/it-IT/panel.ftl | 2 + addon/locale/zh-CN/addon.ftl | 1 + addon/locale/zh-CN/panel.ftl | 2 + src/modules/services/bing.ts | 89 +++++++++++++++++++++++++++++++++++ src/modules/services/index.ts | 3 ++ src/utils/config.ts | 4 ++ 9 files changed, 105 insertions(+) create mode 100644 src/modules/services/bing.ts diff --git a/addon/locale/en-US/addon.ftl b/addon/locale/en-US/addon.ftl index 9aa3febb..0d33ed30 100644 --- a/addon/locale/en-US/addon.ftl +++ b/addon/locale/en-US/addon.ftl @@ -21,6 +21,7 @@ service-chatgpt=ChatGPT🗝️ service-azuregpt=AzureGPT🗝️ service-gemini=Gemini🗝️ service-haici=Haici +service-bing=Bing service-bingdict=Bing Dict(en↔zh)🔊 service-haicidict=Haici Dict(en↔zh)🔊 service-collinsdict=Collins Dict(en↔zh)🔊 diff --git a/addon/locale/en-US/panel.ftl b/addon/locale/en-US/panel.ftl index 4e79a002..8d307723 100644 --- a/addon/locale/en-US/panel.ftl +++ b/addon/locale/en-US/panel.ftl @@ -77,6 +77,8 @@ service-gemini = .label = Gemini🗝️ service-haici = .label = Haici +service-bing = + .label = Bing service-bingdict = .label = Bing Dict(en↔zh)🔊 service-haicidict = diff --git a/addon/locale/it-IT/addon.ftl b/addon/locale/it-IT/addon.ftl index d7a992a3..8f47497a 100644 --- a/addon/locale/it-IT/addon.ftl +++ b/addon/locale/it-IT/addon.ftl @@ -20,6 +20,7 @@ service-xftrans=Xftrans🗝️ service-chatgpt=ChatGPT🗝️ service-azuregpt=AzureGPT🗝️ service-haici=Haici +service-bing=Bing service-bingdict=Bing Dict(en↔zh)🔊 service-haicidict=Haici Dict(en↔zh)🔊 service-collinsdict=Collins Dict(en↔zh)🔊 diff --git a/addon/locale/it-IT/panel.ftl b/addon/locale/it-IT/panel.ftl index 4e79a002..8d307723 100644 --- a/addon/locale/it-IT/panel.ftl +++ b/addon/locale/it-IT/panel.ftl @@ -77,6 +77,8 @@ service-gemini = .label = Gemini🗝️ service-haici = .label = Haici +service-bing = + .label = Bing service-bingdict = .label = Bing Dict(en↔zh)🔊 service-haicidict = diff --git a/addon/locale/zh-CN/addon.ftl b/addon/locale/zh-CN/addon.ftl index c16f9806..d344561b 100644 --- a/addon/locale/zh-CN/addon.ftl +++ b/addon/locale/zh-CN/addon.ftl @@ -20,6 +20,7 @@ service-xftrans=讯飞🗝️ service-chatgpt=ChatGPT🗝️ service-azuregpt=AzureGPT🗝️ service-haici=海词 +service-bing=必应 service-bingdict=必应词典(en↔zh)🔊 service-haicidict=海词词典(en↔zh)🔊 service-collinsdict=科林斯词典(en↔zh)🔊 diff --git a/addon/locale/zh-CN/panel.ftl b/addon/locale/zh-CN/panel.ftl index 2ceda373..329ff440 100644 --- a/addon/locale/zh-CN/panel.ftl +++ b/addon/locale/zh-CN/panel.ftl @@ -77,6 +77,8 @@ service-gemini = .label = Gemini🗝️ service-haici = .label = 海词 +service-bing = + .label = 必应 service-bingdict = .label = 必应词典(en↔zh)🔊 service-haicidict = diff --git a/src/modules/services/bing.ts b/src/modules/services/bing.ts new file mode 100644 index 00000000..1a409b7a --- /dev/null +++ b/src/modules/services/bing.ts @@ -0,0 +1,89 @@ +import { getPref, getPrefJSON, setPref } from "../../utils/prefs"; +import { TranslateTaskProcessor } from "../../utils/task"; + +export default async function (data) { + const xhr = await Zotero.HTTP.request( + "POST", + `https://api-edge.cognitive.microsofttranslator.com/translate?from=${data.langfrom}&to=${data.langto}&api-version=3.0&includeSentenceLength=true`, + { + headers: { + accept: "*/*", + "accept-language": + "zh-TW,zh;q=0.9,ja;q=0.8,zh-CN;q=0.7,en-US;q=0.6,en;q=0.5", + authorization: `Bearer ${await getToken()}`, + "cache-control": "no-cache", + "content-type": "application/json", + pragma: "no-cache", + "sec-ch-ua": + '"Microsoft Edge";v="113", "Chromium";v="113", "Not-A.Brand";v="24"', + "sec-ch-ua-mobile": "?0", + "sec-ch-ua-platform": '"Windows"', + "sec-fetch-dest": "empty", + "sec-fetch-mode": "cors", + "sec-fetch-site": "cross-site", + Referer: "https://appsumo.com/", + "Referrer-Policy": "strict-origin-when-cross-origin", + "User-Agent": + "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36 Edg/113.0.1774.42", + }, + body: JSON.stringify([{ text: data.raw }]), + responseType: "json", + }, + ); + if (xhr?.status !== 200) { + throw `Request error: ${xhr?.status}`; + } + + try { + data.result = xhr.response[0].translations[0].text; + } catch { + throw `Service error: ${xhr.response}`; + } +}; + +const bingTokenKey = "bingToken"; +const tokenExpTime = 5 * 60 * 1000; // 5 minutes refresh token + +async function getToken(forceRefresh: boolean = false) { + let token = ""; + // Just in case the update fails + let doRefresh = true; + try { + const tokenObj = getPrefJSON(bingTokenKey); + if ( + !forceRefresh && + tokenObj && + tokenObj.token && + new Date().getTime() < tokenObj.exp + ) { + token = tokenObj.token; + doRefresh = false; + } + } catch (e) { + ztoolkit.log(e); + } + if (doRefresh) { + const xhr = await Zotero.HTTP.request( + "GET", + "https://edge.microsoft.com/translate/auth", + { + headers: { + "User-Agent": + "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36 Edg/113.0.1774.42", + }, + responseType: "text", + }, + ); + if (xhr && xhr.response) { + token = xhr.response; + setPref( + bingTokenKey, + JSON.stringify({ + exp: new Date().getTime() + tokenExpTime, + token: token, + }), + ); + } + } + return token; +} diff --git a/src/modules/services/index.ts b/src/modules/services/index.ts index 8262b5a8..2152b187 100644 --- a/src/modules/services/index.ts +++ b/src/modules/services/index.ts @@ -20,6 +20,9 @@ export class TranslationServices { import("./baidufield").then( (e) => (this.baidufield = new TranslateTaskRunner(e.default)), ); + import("./bing").then( + (e) => (this.bing = new TranslateTaskRunner(e.default)), + ); import("./bingdict").then( (e) => (this.bingdict = new TranslateTaskRunner(e.default)), ); diff --git a/src/utils/config.ts b/src/utils/config.ts index b6ac46bf..697994f3 100644 --- a/src/utils/config.ts +++ b/src/utils/config.ts @@ -36,6 +36,10 @@ export const SERVICES: Readonly[]> = [ type: "sentence", id: "youdao", }, + { + type: "sentence", + id: "bing", + }, { type: "sentence", id: "huoshan",