Skip to content

Commit

Permalink
feat(dicts): more robust google engine
Browse files Browse the repository at this point in the history
  • Loading branch information
crimx committed May 28, 2018
1 parent dbbc042 commit 701d1d4
Showing 1 changed file with 63 additions and 2 deletions.
65 changes: 63 additions & 2 deletions src/components/dictionaries/google/engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,33 @@ export default function search (
sl = 'en'
tl = chCode
}
const url = `https://translate.googleapis.com/translate_a/single?client=gtx&sl=${sl}&tl=${tl}&dt=t&q=${encodeURIComponent(text)}`

return fetch(url)
return fetchWithToken('https://translate.google.com', sl, tl, text)
.catch(() => fetchWithToken('https://translate.google.cn', sl, tl, text))
.catch(() => fetchWithoutToken(sl, tl, text))
}

function fetchWithToken (base: string, sl: string, tl: string, text: string): Promise<GoogleSearchResult> {
return fetch(base)
.then(r => r.text())
.then<Response>(body => {
const tkk = (body.match(/TKK=(.*?)\(\)\)'\);/) || [''])[0]
.replace(/\\x([0-9A-Fa-f]{2})/g, '') // remove hex chars
.match(/[+-]?\d+/g)
if (tkk) {
const tk = getTK(text, Number(tkk[2]), (Number(tkk[0]) + Number(tkk[1])))
if (tk) {
return fetch(`${base}/translate_a/single?client=t&sl=${sl}&tl=${tl}&q=${text}&tk=${tk}&hl=en&dt=at&dt=bd&dt=ex&dt=ld&dt=md&dt=qca&dt=rw&dt=rm&dt=ss&dt=t&ie=UTF-8&oe=UTF-8&otf=1&ssel=0&tsel=0&kc=5`)
}
}
return handleNoResult()
})
.then(r => r.text())
.then(handleText)
}

function fetchWithoutToken (sl: string, tl: string, text: string): Promise<GoogleSearchResult> {
return fetch(`https://translate.googleapis.com/translate_a/single?client=gtx&sl=${sl}&tl=${tl}&dt=t&q=${text}`)
.then(r => r.text())
.then(handleText)
}
Expand All @@ -45,3 +69,40 @@ function handleText (

return handleNoResult()
}

/* tslint:disable */

function getTK (a, b, c) {
b = Number(b) || 0
let e: any = []
let f = 0
let g = 0
for (; g < a.length; g++) {
let l = a.charCodeAt(g)
128 > l ? e[f++] = l : (2048 > l ? e[f++] = l >> 6 | 192 : (55296 == (l & 64512) && g + 1 < a.length && 56320 == (a.charCodeAt(g + 1) & 64512) ? (l = 65536 + ((l & 1023) << 10) + (a.charCodeAt(++g) & 1023),
e[f++] = l >> 18 | 240,
e[f++] = l >> 12 & 63 | 128) : e[f++] = l >> 12 | 224,
e[f++] = l >> 6 & 63 | 128),
e[f++] = l & 63 | 128)
}
a = b
for (f = 0; f < e.length; f++) {
a += e[f], a = _magic(a, '+-a^+6')
}
a = _magic(a, '+-3^+b+-f')
a ^= Number(c) || 0;
0 > a && (a = (a & 2147483647) + 2147483648)
a %= 1E6
return (a.toString() + '.' + (a ^ b))
}

function _magic (a, b) {
for (var c = 0; c < b.length - 2; c += 3) {
// @ts-ignore
var d = b.charAt(c + 2), d = "a" <= d ? d.charCodeAt(0) - 87 : Number(d), d = "+" == b.charAt(c + 1) ? a >>> d : a << d;
a = "+" == b.charAt(c) ? a + d & 4294967295 : a ^ d
}
return a
}

/* tslint:enable */

0 comments on commit 701d1d4

Please sign in to comment.