generated from pot-app/pot-app-translate-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
50 lines (46 loc) · 1.39 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
async function translate(text, from, to, options) {
const { utils } = options;
const { tauriFetch: fetch } = utils;
const URL = "https://transmart.qq.com/api/imt";
const body = {
"header": {
"fn": "auto_translation",
"client_key": "browser-chrome-110.0.0-Mac OS-df4bd4c5-a65d-44b2-a40f-42f34f3535f2-1677486696487"
},
"type": "plain",
"model_category": "normal",
"source": {
"lang": from,
"text_list": [
text
]
},
"target": {
"lang": to
}
};
const headers = {
"Content-Type": "application/json",
"user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36",
"referer": "https://transmart.qq.com/zh-CN/index"
};
let res = await fetch(URL, {
method: 'POST',
headers: headers,
body: {
type: 'Json',
payload: body
},
});
if (res.ok) {
let result = res.data;
const { auto_translation } = result;
if (auto_translation){
return auto_translation.join("\n").trim();
}else{
throw JSON.stringify(result);
}
} else {
throw `Http Request Error\nHttp Status: ${res.status}\n${JSON.stringify(res.data)}`;
}
}