Created
October 28, 2017 06:21
-
-
Save denniskupec/5b294d3e4c160831e3731f5845131ebe to your computer and use it in GitHub Desktop.
GM_xmlhttpRequest + GM_download => Promises
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// GM_download | |
function Download(url, name, opt={}) { | |
Object.assign(opt, { url, name }) | |
return new Promise((resolve, reject) => { | |
opt.onerror = reject | |
opt.onload = resolve | |
GM_download(opt) | |
}) | |
} | |
// GM_xmlhttpRequest | |
function Request(url, opt={}) { | |
Object.assign(opt, { | |
url, | |
timeout: 2000, | |
responseType: 'json' | |
}) | |
return new Promise((resolve, reject) => { | |
/* | |
for (let f of ['onerror', 'ontimeout']) | |
opt[f] = reject | |
*/ | |
opt.onerror = opt.ontimeout = reject | |
opt.onload = resolve | |
GM_xmlhttpRequest(opt) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
good code!!! thx