GM_fetch

fetch pollyfill using GM_xmlhttpRequest

This script should not be not be installed directly. It is a library for other scripts to include with the meta directive // @require https://update.greasyfork.org/scripts/421384/1134973/GM_fetch.js

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)
	})
}