Skip to content

Commit

Permalink
refactor google translate
Browse files Browse the repository at this point in the history
  • Loading branch information
ttop32 committed Feb 5, 2024
1 parent 30404cb commit 17756b7
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 85 deletions.
3 changes: 3 additions & 0 deletions src/popup.vue
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,9 @@ var translatorList = {
"yandex (Experimental)": "yandex",
"deepl (Experimental)": "deepl",
"baidu (Experimental)": "baidu",
// googleGTX: "googleGTX",
// googleWeb: "googleWeb",
// googleV2: "googleV2",
};
var translateActionList = {
Expand Down
69 changes: 0 additions & 69 deletions src/translator/google.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
31 changes: 31 additions & 0 deletions src/translator/googleGTX.js
Original file line number Diff line number Diff line change
@@ -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: "",
};
}
}
73 changes: 73 additions & 0 deletions src/translator/googleV2.js
Original file line number Diff line number Diff line change
@@ -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;
}
16 changes: 2 additions & 14 deletions src/translator/googleDictionary.js → src/translator/googleWeb.js
Original file line number Diff line number Diff line change
@@ -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, {
Expand All @@ -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,
Expand Down
16 changes: 14 additions & 2 deletions src/translator/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

0 comments on commit 17756b7

Please sign in to comment.