-
Notifications
You must be signed in to change notification settings - Fork 480
/
tool.js
167 lines (164 loc) · 6.06 KB
/
tool.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
// 统一 Surge、QuanX、Node 相关脚本 API, 方便开发、调试
// 包括 Node(request 模块)、Surge($httpClient,$notification,$persistentStore 模块)、QuanX($task,$notify,$prefs 模块)
const $tool = new Tool()
// notify
$tool.notify("title", "subtitle", "message")
// cache
$tool.write("value", "key")
$tool.read("key")
// get
$tool.get("http://www.baidu.com", function (error, response, body) {
// response.status response.statusCode response.headers
if (!error) {
if (response.statusCode == 200) {
console.log(body)
}
} else {
console.log(error)
}
})
// post
const request = {
url: "https://www.baidu.com",
body: ""
//...
}
$tool.post(request, function (error, response, body) {
// response.status response.statusCode response.headers
if (!error) {
if (response.statusCode == 200) {
console.log(body)
}
} else {
console.log(error)
}
})
function Tool() {
_node = (() => {
if (typeof require == "function") {
const request = require('request')
return ({ request })
} else {
return (null)
}
})()
_isSurge = typeof $httpClient != "undefined"
_isQuanX = typeof $task != "undefined"
this.isSurge = _isSurge
this.isQuanX = _isQuanX
this.isResponse = typeof $response != "undefined"
this.notify = (title, subtitle, message) => {
if (_isQuanX) $notify(title, subtitle, message)
if (_isSurge) $notification.post(title, subtitle, message)
if (_node) console.log(JSON.stringify({ title, subtitle, message }));
}
this.write = (value, key) => {
if (_isQuanX) return $prefs.setValueForKey(value, key)
if (_isSurge) return $persistentStore.write(value, key)
}
this.read = (key) => {
if (_isQuanX) return $prefs.valueForKey(key)
if (_isSurge) return $persistentStore.read(key)
}
this.get = (options, callback) => {
if (_isQuanX) {
if (typeof options == "string") options = { url: options }
options["method"] = "GET"
$task.fetch(options).then(response => { callback(null, _status(response), response.body) }, reason => callback(reason.error, null, null))
}
if (_isSurge) $httpClient.get(options, (error, response, body) => { callback(error, _status(response), body) })
if (_node) _node.request(options, (error, response, body) => { callback(error, _status(response), body) })
}
this.post = (options, callback) => {
if (_isQuanX) {
if (typeof options == "string") options = { url: options }
options["method"] = "POST"
$task.fetch(options).then(response => { callback(null, _status(response), response.body) }, reason => callback(reason.error, null, null))
}
if (_isSurge) $httpClient.post(options, (error, response, body) => { callback(error, _status(response), body) })
if (_node) _node.request.post(options, (error, response, body) => { callback(error, _status(response), body) })
}
_status = (response) => {
if (response) {
if (response.status) {
response["statusCode"] = response.status
} else if (response.statusCode) {
response["status"] = response.statusCode
}
}
return response
}
}
function tool() {
const isSurge = typeof $httpClient != "undefined"
const isQuanX = typeof $task != "undefined"
const isResponse = typeof $response != "undefined"
const node = (() => {
if (typeof require == "function") {
const request = require('request')
return ({ request })
} else {
return (null)
}
})()
const notify = (title, subtitle, message) => {
if (isQuanX) $notify(title, subtitle, message)
if (isSurge) $notification.post(title, subtitle, message)
if (node) console.log(JSON.stringify({ title, subtitle, message }));
}
const write = (value, key) => {
if (isQuanX) return $prefs.setValueForKey(value, key)
if (isSurge) return $persistentStore.write(value, key)
}
const read = (key) => {
if (isQuanX) return $prefs.valueForKey(key)
if (isSurge) return $persistentStore.read(key)
}
const adapterStatus = (response) => {
if (response) {
if (response.status) {
response["statusCode"] = response.status
} else if (response.statusCode) {
response["status"] = response.statusCode
}
}
return response
}
const get = (options, callback) => {
if (isQuanX) {
if (typeof options == "string") options = { url: options }
options["method"] = "GET"
$task.fetch(options).then(response => {
callback(null, adapterStatus(response), response.body)
}, reason => callback(reason.error, null, null))
}
if (isSurge) $httpClient.get(options, (error, response, body) => {
callback(error, adapterStatus(response), body)
})
if (node) {
node.request(options, (error, response, body) => {
callback(error, adapterStatus(response), body)
})
}
}
const post = (options, callback) => {
if (isQuanX) {
if (typeof options == "string") options = { url: options }
options["method"] = "POST"
$task.fetch(options).then(response => {
callback(null, adapterStatus(response), response.body)
}, reason => callback(reason.error, null, null))
}
if (isSurge) {
$httpClient.post(options, (error, response, body) => {
callback(error, adapterStatus(response), body)
})
}
if (node) {
node.request.post(options, (error, response, body) => {
callback(error, adapterStatus(response), body)
})
}
}
return { isQuanX, isSurge, isResponse, notify, write, read, get, post }
}