generated from pot-app/pot-app-translate-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
62 lines (60 loc) · 1.86 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
async function translate(text, from, to, options) {
const { utils, detect } = options;
const { tauriFetch: fetch } = utils;
if (from === "auto") {
let langMap = {
"zh_cn": "zh",
"zh_tw": "zh-TW",
"en": "en",
"ja": "ja",
"ko": "ko",
"fr": "fr",
"es": "es",
"ru": "ru",
"de": "de",
"it": "it",
"tr": "tr",
"pt_pt": "pt",
"pt_br": "pt",
"vi": "vi",
"id": "id",
"th": "th",
"ms": "ms",
"ar": "ar",
"hi": "hi"
};
if (langMap[detect]) {
from = langMap[detect];
} else {
from = "en"
}
}
const res = await fetch("https://wxapp.translator.qq.com/api/translate", {
method: 'GET',
headers: {
"User-Agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 16_3_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 MicroMessenger/8.0.32(0x18002035) NetType/WIFI Language/zh_TW",
"Content-Type": "application/json",
"Host": " wxapp.translator.qq.com",
"Referer": "https://servicewechat.com/wxb1070eabc6f9107e/117/page-frame.html"
},
query: {
"source": "auto",
"target": "auto",
"sourceText": text,
"platform": "WeChat_APP",
"guid": "oqdgX0SIwhvM0TmqzTHghWBvfk22",
"candidateLangs": `${from}|${to}`
}
});
if (res.ok) {
let result = res.data;
const { targetText } = result;
if (targetText) {
return targetText;
} else {
throw JSON.stringify(result);
}
} else {
throw `Http Request Error\nHttp Status: ${res.status}\n${JSON.stringify(res.data)}`;
}
}