-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
185 lines (174 loc) · 4.71 KB
/
index.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
const buildPromiseCommon = (asyncFunc, syncFunc, args) => {
return new Promise((resolve, reject) => {
if (pdown) {
if (pdown[asyncFunc]) {
pdown[asyncFunc](...args, response => resolve(response), error => reject(error))
} else if (pdown[syncFunc]) {
try {
resolve(pdown[syncFunc](...args))
} catch (error) {
reject(error)
}
}
}
})
}
export default {
/**
* 通过代理服务器模拟http请求,防止浏览器ajax跨域问题
* @param {Object} request http请求
*/
fetch(request) {
return buildPromiseCommon('fetchAsync', '', [request])
},
/**
* 根据下载请求解析响应相关内容
* @param {Object} request 下载请求
*/
resolve(request) {
return buildPromiseCommon('resolveAsync', 'resolve', [request])
},
/**
* 创建一个任务,会唤醒Proxyee Down并弹出下载框
* @param {Object} taskForm 下载任务请求
*/
createTask(taskForm) {
return buildPromiseCommon('createTaskAsync', 'createTask', [taskForm])
},
/**
* 创建一个任务,不弹下载框直接在后台下载
* @param {Object} taskForm 下载任务请求
*/
pushTask(taskForm) {
return buildPromiseCommon('pushTask', '', [taskForm])
},
/**
* 刷新一个任务的请求信息
* @param {String} id 任务ID
* @param {Object} request 下载请求
*/
refreshTask(id, request) {
return buildPromiseCommon('refreshTaskAsync', 'refreshTask', [id, request])
},
/**
* 暂停一个任务
* @param {String} id 任务ID
*/
pauseTask(id) {
return buildPromiseCommon('pauseTaskAsync', 'pauseTask', [id])
},
/**
* 恢复一个任务
* @param {String} id 任务ID
*/
resumeTask(id) {
return buildPromiseCommon('resumeTaskAsync', 'resumeTask', [id])
},
/**
* 删除一个任务
* @param {String} id 任务ID
* @param {Boolean} delFile 是否删除文件
*/
deleteTask(id, delFile) {
return buildPromiseCommon('deleteTaskAsync', 'deleteTask', [id, delFile])
},
/**
* 取下载相关配置信息
*/
getDownConfig() {
return buildPromiseCommon('getDownConfigAsync', 'getDownConfig', [])
},
/**
* 取目标网站的cookie,支持HttpOnly cookie,需要经过代理服务器才能生效
* @param {string} url 要获取的网站url,空则为当前网址
*/
getCookie(url) {
return buildPromiseCommon('getCookieAsync', 'getCookie', [url || '/'])
}
}
/**
* 简单模拟jQuery的ajax函数
*/
export const jQuery = window.jQuery || {
ajax() {
let settings
if (arguments.length == 2) {
settings = arguments[1]
settings.url = arguments[0]
} else {
settings = arguments[0]
}
const xhr = new XMLHttpRequest()
const options = {
...{
method: 'GET',
url: settings.url,
contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
headers: {},
data: null,
dataType: null,
success() {},
error() {},
complete() {}
},
...settings
}
let hasRequestBody = false
if (options.method.toUpperCase === 'POST' || options.method.toUpperCase === 'PUT') {
xhr.setRequestHeader('Content-Type', options.contentType)
hasRequestBody = true
}
if (document.cookie) {
xhr.setRequestHeader('Cookie', document.cookie)
}
for (const key in options.headers) {
xhr.setRequestHeader(key, options.headers[key])
}
xhr.onreadystatechange = () => {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
let result = xhr.responseText
if (options.dataType) {
if (options.dataType.toUpperCase() === 'JSON') {
result = JSON.parse(result)
}
} else if (xhr.getResponseHeader('content-type').match(/^.*json.*$/i)) {
result = JSON.parse(result)
}
options.success(result, xhr.status, xhr)
} else {
options.error(xhr, xhr.status)
}
options.complete(xhr, xhr.status)
}
}
let data
if (options.data) {
if (options.data.toString() === '[object Object]') {
data = ''
for (const key in options.data) {
if (data) {
data += '&'
}
data += key + '=' + options.data[key]
}
} else {
data = options.data
}
}
if(!hasRequestBody){
if(options.url.indexOf('?') === -1){
options.url += '?'
}
if(options.url.indexOf('&') !== -1){
options.url += '&'
}
options.url += data
xhr.open(options.method, options.url)
xhr.send()
}else{
xhr.open(options.method, options.url)
xhr.send(data ? data : null)
}
}
}