Skip to content

Instantly share code, notes, and snippets.

@denniskupec
Created October 28, 2017 06:21
Show Gist options
  • Select an option

  • Save denniskupec/5b294d3e4c160831e3731f5845131ebe to your computer and use it in GitHub Desktop.

Select an option

Save denniskupec/5b294d3e4c160831e3731f5845131ebe to your computer and use it in GitHub Desktop.
GM_xmlhttpRequest + GM_download => Promises
// 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)
})
}
@ideacco
Copy link
Copy Markdown

ideacco commented Jun 27, 2020

good code!!! thx

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment