{"id":14989067,"url":"https://github.com/alttiri/gm_fetch","last_synced_at":"2026-01-04T09:09:54.496Z","repository":{"id":104052951,"uuid":"494934341","full_name":"AlttiRi/gm_fetch","owner":"AlttiRi","description":"[in dev] GM_fetch — a wrapper for GM_xmlhttpRequest of TamperMonkey and ViolentMonkey UserScript extensions. Compatible with Fetch API.","archived":false,"fork":false,"pushed_at":"2023-08-03T23:57:47.000Z","size":261,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-21T21:15:04.088Z","etag":null,"topics":["fetch","gm-fetch","library","tampermonkey","userscript","util","violentmonkey"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/AlttiRi.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-05-22T01:45:32.000Z","updated_at":"2023-09-08T18:34:46.000Z","dependencies_parsed_at":null,"dependency_job_id":"759e2e40-2630-48eb-b5d2-e02648c13f52","html_url":"https://github.com/AlttiRi/gm_fetch","commit_stats":{"total_commits":95,"total_committers":1,"mean_commits":95.0,"dds":0.0,"last_synced_commit":"bc0b0cf9589976206c3fd67b64377ce1df8ebd75"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AlttiRi%2Fgm_fetch","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AlttiRi%2Fgm_fetch/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AlttiRi%2Fgm_fetch/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AlttiRi%2Fgm_fetch/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/AlttiRi","download_url":"https://codeload.github.com/AlttiRi/gm_fetch/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244868753,"owners_count":20523591,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["fetch","gm-fetch","library","tampermonkey","userscript","util","violentmonkey"],"created_at":"2024-09-24T14:17:39.154Z","updated_at":"2026-01-04T09:09:54.471Z","avatar_url":"https://github.com/AlttiRi.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# GM_fetch\n\n`GM_fetch` — a wrapper for `GM_xmlhttpRequest` of TamperMonkey and ViolentMonkey UserScript extensions.\n\n`GM_fetch` is compatible with [Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API).\n\n---\n\n**Currently it is in development. So, it's not complited.** \n\nThe more detail description will be later.\n\n---\n\n## Simple wrapper\n\nOkay, if you just need a **simple** wrapper, here is it:\n```js\nasync function GM_fetch(url, {method = \"get\", headers} = {}) {\n    return new Promise((resolve, _reject) =\u003e {\n        const blobPromise = new Promise((resolve, reject) =\u003e {\n            GM_xmlhttpRequest({\n                url,\n                method,\n                headers,\n                responseType: \"blob\",\n                onload: response =\u003e resolve(response.response),\n                onerror: reject,\n                ontimeout: reject,\n                onreadystatechange: onHeadersReceived\n            });\n        });\n        blobPromise.catch(_reject);\n        function onHeadersReceived(response) {\n            const {\n                readyState, responseHeaders, status, statusText\n            } = response;\n            if (readyState === 2) { // HEADERS_RECEIVED\n                const headers = parseHeaders(responseHeaders);\n                resolve({\n                    headers,\n                    status,\n                    statusText,\n                    ok: status.toString().startsWith(\"2\"),\n                    arrayBuffer: () =\u003e blobPromise.then(blob =\u003e blob.arrayBuffer()),\n                    blob: () =\u003e blobPromise,\n                    json: () =\u003e blobPromise.then(blob =\u003e blob.text()).then(text =\u003e JSON.parse(text)),\n                    text: () =\u003e blobPromise.then(blob =\u003e blob.text()),\n                });\n            }\n        }\n    });\n}\nfunction parseHeaders(headersString) {\n    class Headers {\n        get(key) {\n            return this[key.toLowerCase()];\n        }\n    }\n    const headers = new Headers();\n    for (const line of headersString.trim().split(\"\\n\")) {\n        const [key, ...valueParts] = line.split(\":\"); // last-modified: Fri, 21 May 2021 14:46:56 GMT\n        headers[key.trim().toLowerCase()] = valueParts.join(\":\").trim();\n    }\n    return headers;\n}\n```\nIt supports only `method` and `headers` keys of [`fetch`'s options parameter](https://developer.mozilla.org/en-US/docs/Web/API/fetch#parameters).\n\nDemo for the simple wrapper ([gm_fetch_simple_demo.user.js](https://github.com/AlttiRi/gm_fetch/raw/master/gm_fetch_simple_demo.user.js)):\n```js\n// ...\n// @grant       GM_xmlhttpRequest\n// ==/UserScript==\n\n(async function demo() {\n  const url = \"https://i.imgur.com/2wD3VJA.jpg\";\n  const refererHeader = \"https://imgur.com/\";\n  const response = await GM_fetch(url, {headers: {referer: refererHeader}});\n  const {status, statusText, ok} = response;\n  const lastModified = response.headers.get(\"last-modified\");\n  console.log(url, {status, statusText, ok, lastModified});\n\n  const blob = await response.blob();\n  const name = url.split(\"/\").pop();\n  downloadBlob(blob, name, url);\n})();\n\nfunction downloadBlob(blob, name, url) {\n  const anchor = document.createElement(\"a\");\n  anchor.setAttribute(\"download\", name || \"\");\n  const blobUrl = URL.createObjectURL(blob);\n  anchor.href = blobUrl + (url ? (\"#\" + url) : \"\");\n  anchor.click();\n  setTimeout(() =\u003e URL.revokeObjectURL(blobUrl), 5000);\n}\n\n// ...\n// async function GM_fetch(url, init = {}) {\n// ...\n```\n\n---\n\n- https://www.tampermonkey.net/documentation.php#GM_xmlhttpRequest\n- https://violentmonkey.github.io/api/gm/#gm_xmlhttprequest\n- https://wiki.greasespot.net/GM.xmlHttpRequest\n\n---\n\n- https://developer.mozilla.org/en-US/docs/Web/API/fetch\n- https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Referrer-Policy\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falttiri%2Fgm_fetch","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falttiri%2Fgm_fetch","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falttiri%2Fgm_fetch/lists"}