| 246 | } |
| 247 | |
| 248 | function Tool() { |
| 249 | _node = (() => { |
| 250 | if (typeof require == "function") { |
| 251 | const request = require('request') |
| 252 | return ({ request }) |
| 253 | } else { |
| 254 | return (null) |
| 255 | } |
| 256 | })() |
| 257 | _isSurge = typeof $httpClient != "undefined" |
| 258 | _isQuanX = typeof $task != "undefined" |
| 259 | this.isSurge = _isSurge |
| 260 | this.isQuanX = _isQuanX |
| 261 | this.isResponse = typeof $response != "undefined" |
| 262 | this.notify = (title, subtitle, message) => { |
| 263 | if (_isQuanX) $notify(title, subtitle, message) |
| 264 | if (_isSurge) $notification.post(title, subtitle, message) |
| 265 | if (_node) console.log(JSON.stringify({ title, subtitle, message })); |
| 266 | } |
| 267 | this.write = (value, key) => { |
| 268 | if (_isQuanX) return $prefs.setValueForKey(value, key) |
| 269 | if (_isSurge) return $persistentStore.write(value, key) |
| 270 | } |
| 271 | this.read = (key) => { |
| 272 | if (_isQuanX) return $prefs.valueForKey(key) |
| 273 | if (_isSurge) return $persistentStore.read(key) |
| 274 | } |
| 275 | this.get = (options, callback) => { |
| 276 | if (_isQuanX) { |
| 277 | if (typeof options == "string") options = { url: options } |
| 278 | options["method"] = "GET" |
| 279 | $task.fetch(options).then(response => { callback(null, _status(response), response.body) }, reason => callback(reason.error, null, null)) |
| 280 | } |
| 281 | if (_isSurge) $httpClient.get(options, (error, response, body) => { callback(error, _status(response), body) }) |
| 282 | if (_node) _node.request(options, (error, response, body) => { callback(error, _status(response), body) }) |
| 283 | } |
| 284 | this.post = (options, callback) => { |
| 285 | if (_isQuanX) { |
| 286 | if (typeof options == "string") options = { url: options } |
| 287 | options["method"] = "POST" |
| 288 | $task.fetch(options).then(response => { callback(null, _status(response), response.body) }, reason => callback(reason.error, null, null)) |
| 289 | } |
| 290 | if (_isSurge) $httpClient.post(options, (error, response, body) => { callback(error, _status(response), body) }) |
| 291 | if (_node) _node.request.post(options, (error, response, body) => { callback(error, _status(response), body) }) |
| 292 | } |
| 293 | _status = (response) => { |
| 294 | if (response) { |
| 295 | if (response.status) { |
| 296 | response["statusCode"] = response.status |
| 297 | } else if (response.statusCode) { |
| 298 | response["status"] = response.statusCode |
| 299 | } |
| 300 | } |
| 301 | return response |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | function Base64() { |