Skip to content

Instantly share code, notes, and snippets.

@Hillzacky
Created June 15, 2023 16:53
Show Gist options
  • Save Hillzacky/01d6fe725dd3a20e45568be961bd8ade to your computer and use it in GitHub Desktop.
Save Hillzacky/01d6fe725dd3a20e45568be961bd8ade to your computer and use it in GitHub Desktop.
class Tr {
static request({
method = "GET",
url,
params,
responseType,
data,
headers
}) {
return new Promise((resolve, reject) => {
if (params) {
const sep = url.includes("?") ? "&" : "?"
url += sep + new URLSearchParams(params).toString()
}
GM_xmlhttpRequest({
method,
url,
responseType,
data,
headers,
onload(res) {
if (res.status >= 300) return reject()
resolve(res.response)
},
onerror: reject
})
})
}
static async google(text, to) {
const data = await request({
url: "https://translate.google.com/translate_a/single",
params: {
q: text,
client: "gtx",
sl: "auto",
tl: to,
dt: "at"
},
responseType: "json"
})
const language = { from: data[8][0][0], to }
const translations = data[5]?.map(item => item[2]?.[0]?.[0]).filter(Boolean)
return { language, translations }
}
}
const TRANSLATION_URL = "https://translate.google.com/translate_a/single?client=gtx&sl=auto&tl=zh-CN&hl=zh-CN&dt=at&dt=bd&dt=ex&dt=ld&dt=md&dt=qca&dt=rw&dt=rm&dt=ss&dt=t&otf=1&ssel=0&tsel=0&kc=3&q=";
const TRANSLATION_AUDIO_URL = "https://translate.google.com/translate_tts?client=gtx&ie=UTF-8&tl=en&q=";
const BING_TRANSLATION_URL = "https://cn.bing.com/ttranslate";
const service = {
'com_apis':'https://translate.googleapis.com/translate_a/single',
'clients5':'https://clients5.google.com/translate_a/t',
'com':'https://translate.google.com/translate_a/single',
'tw':'https://translate.google.com.tw/translate_a/single',
'cn':'https://translate.google.cn/translate_a/single',
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment