| 219 | } |
| 220 | |
| 221 | function tool() { |
| 222 | const isSurge = typeof $httpClient != "undefined" |
| 223 | const isQuanX = typeof $task != "undefined" |
| 224 | const isResponse = typeof $response != "undefined" |
| 225 | const node = (() => { |
| 226 | if (typeof require == "function") { |
| 227 | const request = require('request') |
| 228 | return ({ request }) |
| 229 | } else { |
| 230 | return (null) |
| 231 | } |
| 232 | })() |
| 233 | const notify = (title, subtitle, message) => { |
| 234 | if (isQuanX) $notify(title, subtitle, message) |
| 235 | if (isSurge) $notification.post(title, subtitle, message) |
| 236 | if (node) console.log(JSON.stringify({ title, subtitle, message })); |
| 237 | } |
| 238 | const write = (value, key) => { |
| 239 | if (isQuanX) return $prefs.setValueForKey(value, key) |
| 240 | if (isSurge) return $persistentStore.write(value, key) |
| 241 | } |
| 242 | const read = (key) => { |
| 243 | if (isQuanX) return $prefs.valueForKey(key) |
| 244 | if (isSurge) return $persistentStore.read(key) |
| 245 | } |
| 246 | const adapterStatus = (response) => { |
| 247 | if (response) { |
| 248 | if (response.status) { |
| 249 | response["statusCode"] = response.status |
| 250 | } else if (response.statusCode) { |
| 251 | response["status"] = response.statusCode |
| 252 | } |
| 253 | } |
| 254 | return response |
| 255 | } |
| 256 | const get = (options, callback) => { |
| 257 | if (isQuanX) { |
| 258 | if (typeof options == "string") options = { url: options } |
| 259 | options["method"] = "GET" |
| 260 | $task.fetch(options).then(response => { |
| 261 | callback(null, adapterStatus(response), response.body) |
| 262 | }, reason => callback(reason.error, null, null)) |
| 263 | } |
| 264 | if (isSurge) $httpClient.get(options, (error, response, body) => { |
| 265 | callback(error, adapterStatus(response), body) |
| 266 | }) |
| 267 | if (node) { |
| 268 | node.request(options, (error, response, body) => { |
| 269 | callback(error, adapterStatus(response), body) |
| 270 | }) |
| 271 | } |
| 272 | } |
| 273 | const post = (options, callback) => { |
| 274 | if (isQuanX) { |
| 275 | if (typeof options == "string") options = { url: options } |
| 276 | options["method"] = "POST" |
| 277 | $task.fetch(options).then(response => { |
| 278 | callback(null, adapterStatus(response), response.body) |