GM_fetch

fetch pollyfill with resp.text/resp.json/resp.blob/resp.arrayBuffer support 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://greasyfork.org/scripts/421384-gm-fetch/code/GM_fetch.js?version=898562

Author
nyarau
Ratings
0 0 0
Version
0.0.1.20210207152600
Created
2021-02-07
Updated
2021-02-07
License
N/A
Applies to
All sites

(personal uses, with no guarantee what so ever)

fetch pollyfill with resp.text/resp.json/resp.blob/resp.arrayBuffer support using GM_xmlhttpRequest, not yet 100% replication of fetch API (eg. errors)

Written for / tested with TamperMonkey, may work for GeaseMonkey or others

Don't forget to add permissions

// @grant    GM_xmlhttpRequest
// @connect  api.example.com

Example

GM_fetch("https://api.example.com/favicon.png", {
    method: 'POST',
    body: JSON.stringify({message: "hello world"}),
    headers: { 'Content-Type': 'application/json' }
}).then(resp=>resp.blob()).then(blob=>{
    open(URL.createObjectURL(blob), "_blank")
})