GM_fetch

fetch pollyfill using GM_xmlhttpRequest

Tätä skriptiä ei tulisi asentaa suoraan. Se on kirjasto muita skriptejä varten sisällytettäväksi metadirektiivillä // @require https://update.greasyfork.org/scripts/421384/1134973/GM_fetch.js.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

function GM_fetch(url, opt={}){
	return new Promise((resolve, reject)=>{
		// https://www.tampermonkey.net/documentation.php?ext=dhdg#GM_xmlhttpRequest
		// https://violentmonkey.github.io/api/gm/#gm_xmlhttprequest
		opt.url = url
		opt.data = opt.body
		opt.responseType = "blob"
		opt.onload = resp=>{
			resolve(new Response(resp.response, {status: resp.status,
				// https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/getAllResponseHeaders#examples
				headers: Object.fromEntries(resp.responseHeaders.trim().split(/[\r\n]+/).map(line=>{parts=line.split(': ');return [parts.shift(), parts.join(': ')]}))
			}))
		}
		opt.ontimeout = ()=>reject("fetch timeout")
		opt.onerror   = error=>reject(error)
		opt.onabort   = ()=>reject("fetch abort")
		GM_xmlhttpRequest(opt)
	})
}