-
Notifications
You must be signed in to change notification settings - Fork 17
/
ajax.ts
76 lines (74 loc) · 2.01 KB
/
ajax.ts
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import { XhrMethod, XhrOptions, XhrResponseType } from '@/global';
import { MessageBox } from '@/lib/message';
import { IMPORTANCE } from './message';
export const getData = (
url: string,
type: XhrResponseType = XhrResponseType.DOCUMENT,
usermethod: XhrMethod = XhrMethod.GET
): Promise<Document> => {
return new Promise((resolve, reject) => {
GM_xmlhttpRequest({
method: usermethod,
url: url,
responseType: type,
timeout: 5 * 60 * 1000,
onload: function (response) {
if (response.status >= 200 && response.status < 400) {
resolve(response.response);
} else {
reject(response);
}
},
onerror: function (error) {
new MessageBox('网络错误');
reject(error);
},
ontimeout: () => {
new MessageBox('网络超时', 'none', IMPORTANCE.LOG_POP_GM);
reject('timeout');
},
});
});
};
export const postData = (
url: string,
postData: string,
{
responseType = XhrResponseType.FORM,
usermethod = XhrMethod.POST,
contentType = XhrResponseType.FORM,
}: XhrOptions = {
responseType: XhrResponseType.FORM,
usermethod: XhrMethod.POST,
contentType: XhrResponseType.FORM,
}
): Promise<MonkeyXhrResponse> => {
return new Promise((resolve, reject) => {
GM_xmlhttpRequest({
method: usermethod,
url: url,
headers: {
'Content-Type': contentType,
},
data: postData,
responseType: responseType,
timeout: 1 * 60 * 1000,
onload: function (response) {
if (response.status >= 200 && response.status < 400) {
resolve(response);
} else {
new MessageBox('请求错误:' + response.status);
reject(response.status);
}
},
onerror: function (error) {
new MessageBox('网络错误');
reject(error);
},
ontimeout: () => {
new MessageBox('网络超时', 'none', IMPORTANCE.LOG_POP_GM);
reject('timeout');
},
});
});
};