diff --git a/src/popup.vue b/src/popup.vue index 3cdc75c1..edcbe96c 100644 --- a/src/popup.vue +++ b/src/popup.vue @@ -328,6 +328,9 @@ var translatorList = { "yandex (Experimental)": "yandex", "deepl (Experimental)": "deepl", "baidu (Experimental)": "baidu", + // googleGTX: "googleGTX", + // googleWeb: "googleWeb", + // googleV2: "googleV2", }; var translateActionList = { diff --git a/src/translator/google.js b/src/translator/google.js index 8fc01e5b..8bbb7a14 100644 --- a/src/translator/google.js +++ b/src/translator/google.js @@ -5,12 +5,6 @@ import ky from "ky"; const googleTranslateTKK = "448487.932609646"; const apiPath = "https://translate.googleapis.com/translate_a/t"; -var tokenUrl = "https://translate.google.com"; -var newGoogleUrl = - "https://translate.google.com/_/TranslateWebserverUi/data/batchexecute"; -var token; -var tokenTTL = 60 * 60 * 1000; //1hour - export default class google extends BaseTranslator { static async requestTranslate(text, sourceLang, targetLang) { // code brought from https://github.com/translate-tools/core/blob/master/src/translators/GoogleTranslator/token.js @@ -126,66 +120,3 @@ function getToken(query, windowTkk) { const normalizedResult = encondingRound % 1000000; return normalizedResult.toString() + "." + (normalizedResult ^ tkkIndex); } - -async function googleTranslateRequestV2(text, sourceLang, targetLang) { - var { sid, bl, at } = await getTokenV2(); - - let req = JSON.stringify([ - [ - [ - "MkEWBc", - JSON.stringify([[text, sourceLang, targetLang, true], [null]]), - null, - "generic", - ], - ], - ]); - return await ky - .post(newGoogleUrl, { - headers: { - "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8", - }, - searchParams: { - rpcids: "MkEWBc", - "source-path": "/", - "f.sid": sid, - bl, - hl: "ko", - "soc-app": 1, - "soc-platform": 1, - "soc-device": 1, - _reqid: Math.floor(10000 + 10000 * Math.random()), - rt: "c", - }, - body: new URLSearchParams({ "f.req": req, at }), // - anonymous: true, - nocache: true, - }) - .text(); -} -function googleTranslateRequestWrapV2(res) { - var json = JSON.parse(JSON.parse(/\[.*\]/.exec(res))[0][2]); - var translatedText = json[1][0][0][5] - .map((text) => text?.[0]) - .filter((text) => text) - .join(" "); - - return { - translatedText, - detectedLang: json[0][2], - transliteration: json[1][0][0][1], - }; -} - -async function getTokenV2() { - if (token && token.time + tokenTTL > Date.now()) { - return token; - } - var res = await ky(tokenUrl).text(); - var sid = res.match(/"FdrFJe":"(.*?)"/)[1]; - let bl = res.match(/"cfb2h":"(.*?)"/)[1]; - let at = res.match(/"SNlM0e":"(.*?)"/)?.[1] || ""; - var time = Date.now(); - token = { sid, bl, at, time }; - return token; -} diff --git a/src/translator/googleGTX.js b/src/translator/googleGTX.js new file mode 100644 index 00000000..e9d37036 --- /dev/null +++ b/src/translator/googleGTX.js @@ -0,0 +1,31 @@ +import ky from "ky"; +import BaseTranslator from "./baseTranslator"; + +var apiUrl = "https://translate.googleapis.com/translate_a/single"; + +export default class googleGTX extends BaseTranslator { + static async requestTranslate(text, sourceLang, targetLang) { + return await ky(apiUrl, { + searchParams: { + client: "gtx", + q: text, + sl: sourceLang, + tl: targetLang, + dt: "t", + dj: 1, + }, + }).json(); + } + static async wrapResponse(res, text, sourceLang, targetLang) { + var translatedText = res.sentences + .map((sentence) => sentence.trans) + .join(" "); + var detectedLang = res.src; + + return { + translatedText, + detectedLang, + transliteration: "", + }; + } +} diff --git a/src/translator/googleV2.js b/src/translator/googleV2.js new file mode 100644 index 00000000..630b0306 --- /dev/null +++ b/src/translator/googleV2.js @@ -0,0 +1,73 @@ +import ky from "ky"; +import BaseTranslator from "./baseTranslator"; + +var tokenUrl = "https://translate.google.com"; +var newGoogleUrl = + "https://translate.google.com/_/TranslateWebserverUi/data/batchexecute"; +var token; +var tokenTTL = 60 * 60 * 1000; //1hour + +export default class googleV2 extends BaseTranslator { + static async requestTranslate(text, sourceLang, targetLang) { + var { sid, bl, at } = await getTokenV2(); + + let req = JSON.stringify([ + [ + [ + "MkEWBc", + JSON.stringify([[text, sourceLang, targetLang, true], [null]]), + null, + "generic", + ], + ], + ]); + return await ky + .post(newGoogleUrl, { + headers: { + "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8", + }, + searchParams: { + rpcids: "MkEWBc", + "source-path": "/", + "f.sid": sid, + bl, + hl: "ko", + "soc-app": 1, + "soc-platform": 1, + "soc-device": 1, + _reqid: Math.floor(10000 + 10000 * Math.random()), + rt: "c", + }, + body: new URLSearchParams({ "f.req": req, at }), // + anonymous: true, + nocache: true, + }) + .text(); + } + static async wrapResponse(res, text, sourceLang, targetLang) { + var json = JSON.parse(JSON.parse(/\[.*\]/.exec(res))[0][2]); + var translatedText = json[1][0][0][5] + .map((text) => text?.[0]) + .filter((text) => text) + .join(" "); + + return { + translatedText, + detectedLang: json[0][2], + transliteration: json[1][0][0][1], + }; + } +} + +async function getTokenV2() { + if (token && token.time + tokenTTL > Date.now()) { + return token; + } + var res = await ky(tokenUrl).text(); + var sid = res.match(/"FdrFJe":"(.*?)"/)[1]; + let bl = res.match(/"cfb2h":"(.*?)"/)[1]; + let at = res.match(/"SNlM0e":"(.*?)"/)?.[1] || ""; + var time = Date.now(); + token = { sid, bl, at, time }; + return token; +} diff --git a/src/translator/googleDictionary.js b/src/translator/googleWeb.js similarity index 67% rename from src/translator/googleDictionary.js rename to src/translator/googleWeb.js index 32331484..657abf16 100644 --- a/src/translator/googleDictionary.js +++ b/src/translator/googleWeb.js @@ -1,13 +1,12 @@ import ky from "ky"; import * as cheerio from "cheerio"; -import BaseTranslator from "./baseTranslator"; -import google from "./google.js"; +import BaseTranslator from "./baseTranslator.js"; import * as util from "/src/util"; var googleSearchUrl = "https://www.google.com/search"; -export default class googleDictionary extends BaseTranslator { +export default class googleWeb extends BaseTranslator { static async requestTranslate(text, sourceLang, targetLang) { var lang = "en"; return await ky(googleSearchUrl, { @@ -27,17 +26,6 @@ export default class googleDictionary extends BaseTranslator { var detectedLang2 = await util.detectLangBrowser(text); var detectedLang = sourceLang != "auto" ? sourceLang : detectedLang2; - //if no result use google translate - if (!translatedText) { - var res = await google.requestTranslate(text, sourceLang, targetLang); - var { translatedText, detectedLang } = await google.wrapResponse( - res, - text, - sourceLang, - targetLang - ); - } - return { translatedText, detectedLang, diff --git a/src/translator/index.js b/src/translator/index.js index 8b6d7fb9..234faf8a 100644 --- a/src/translator/index.js +++ b/src/translator/index.js @@ -4,6 +4,18 @@ import papago from "./papago.js"; import deepl from "./deepl.js"; import yandex from "./yandex.js"; import baidu from "./baidu.js"; -import googleDictionary from "./googleDictionary.js"; +import googleWeb from "./googleWeb.js"; +import googleV2 from "./googleV2.js"; +import googleGTX from "./googleGTX.js"; -export default { google, bing, papago, deepl, yandex, baidu, googleDictionary }; +export default { + google, + googleWeb, + googleV2, + googleGTX, + bing, + papago, + deepl, + yandex, + baidu, +};