| 40 | }) |
| 41 | |
| 42 | function Tool() { |
| 43 | _node = (() => { |
| 44 | if (typeof require == "function") { |
| 45 | const request = require('request') |
| 46 | return ({ request }) |
| 47 | } else { |
| 48 | return (null) |
| 49 | } |
| 50 | })() |
| 51 | _isSurge = typeof $httpClient != "undefined" |
| 52 | _isQuanX = typeof $task != "undefined" |
| 53 | this.isSurge = _isSurge |
| 54 | this.isQuanX = _isQuanX |
| 55 | this.isResponse = typeof $response != "undefined" |
| 56 | this.notify = (title, subtitle, message, option) => { |
| 57 | if (_isQuanX) $notify(title, subtitle, message, option) |
| 58 | if (_isSurge) $notification.post(title, subtitle, message, option) |
| 59 | if (_node) console.log(JSON.stringify({ title, subtitle, message })); |
| 60 | } |
| 61 | this.write = (value, key) => { |
| 62 | if (_isQuanX) return $prefs.setValueForKey(value, key) |
| 63 | if (_isSurge) return $persistentStore.write(value, key) |
| 64 | } |
| 65 | this.read = (key) => { |
| 66 | if (_isQuanX) return $prefs.valueForKey(key) |
| 67 | if (_isSurge) return $persistentStore.read(key) |
| 68 | } |
| 69 | this.get = (options, callback) => { |
| 70 | if (_isQuanX) { |
| 71 | if (typeof options == "string") options = { url: options } |
| 72 | options["method"] = "GET" |
| 73 | $task.fetch(options).then(response => { callback(null, _status(response), response.body) }, reason => callback(reason.error, null, null)) |
| 74 | } |
| 75 | if (_isSurge) $httpClient.get(options, (error, response, body) => { callback(error, _status(response), body) }) |
| 76 | if (_node) _node.request(options, (error, response, body) => { callback(error, _status(response), body) }) |
| 77 | } |
| 78 | this.post = (options, callback) => { |
| 79 | if (_isQuanX) { |
| 80 | if (typeof options == "string") options = { url: options } |
| 81 | options["method"] = "POST" |
| 82 | $task.fetch(options).then(response => { callback(null, _status(response), response.body) }, reason => callback(reason.error, null, null)) |
| 83 | } |
| 84 | if (_isSurge) $httpClient.post(options, (error, response, body) => { callback(error, _status(response), body) }) |
| 85 | if (_node) _node.request.post(options, (error, response, body) => { callback(error, _status(response), body) }) |
| 86 | } |
| 87 | _status = (response) => { |
| 88 | if (response) { |
| 89 | if (response.status) { |
| 90 | response["statusCode"] = response.status |
| 91 | } else if (response.statusCode) { |
| 92 | response["status"] = response.statusCode |
| 93 | } |
| 94 | } |
| 95 | return response |
| 96 | } |
| 97 | } |