Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/alttiri/gm_fetch
[in dev] GM_fetch — a wrapper for GM_xmlhttpRequest of TamperMonkey and ViolentMonkey UserScript extensions. Compatible with Fetch API.
https://github.com/alttiri/gm_fetch
fetch gm-fetch library tampermonkey userscript util violentmonkey
Last synced: 4 days ago
JSON representation
[in dev] GM_fetch — a wrapper for GM_xmlhttpRequest of TamperMonkey and ViolentMonkey UserScript extensions. Compatible with Fetch API.
- Host: GitHub
- URL: https://github.com/alttiri/gm_fetch
- Owner: AlttiRi
- License: mit
- Created: 2022-05-22T01:45:32.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2023-08-03T23:57:47.000Z (over 1 year ago)
- Last Synced: 2025-01-26T15:47:48.153Z (4 days ago)
- Topics: fetch, gm-fetch, library, tampermonkey, userscript, util, violentmonkey
- Language: JavaScript
- Homepage:
- Size: 255 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# GM_fetch
`GM_fetch` — a wrapper for `GM_xmlhttpRequest` of TamperMonkey and ViolentMonkey UserScript extensions.
`GM_fetch` is compatible with [Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API).
---
**Currently it is in development. So, it's not complited.**
The more detail description will be later.
---
## Simple wrapper
Okay, if you just need a **simple** wrapper, here is it:
```js
async function GM_fetch(url, {method = "get", headers} = {}) {
return new Promise((resolve, _reject) => {
const blobPromise = new Promise((resolve, reject) => {
GM_xmlhttpRequest({
url,
method,
headers,
responseType: "blob",
onload: response => resolve(response.response),
onerror: reject,
ontimeout: reject,
onreadystatechange: onHeadersReceived
});
});
blobPromise.catch(_reject);
function onHeadersReceived(response) {
const {
readyState, responseHeaders, status, statusText
} = response;
if (readyState === 2) { // HEADERS_RECEIVED
const headers = parseHeaders(responseHeaders);
resolve({
headers,
status,
statusText,
ok: status.toString().startsWith("2"),
arrayBuffer: () => blobPromise.then(blob => blob.arrayBuffer()),
blob: () => blobPromise,
json: () => blobPromise.then(blob => blob.text()).then(text => JSON.parse(text)),
text: () => blobPromise.then(blob => blob.text()),
});
}
}
});
}
function parseHeaders(headersString) {
class Headers {
get(key) {
return this[key.toLowerCase()];
}
}
const headers = new Headers();
for (const line of headersString.trim().split("\n")) {
const [key, ...valueParts] = line.split(":"); // last-modified: Fri, 21 May 2021 14:46:56 GMT
headers[key.trim().toLowerCase()] = valueParts.join(":").trim();
}
return headers;
}
```
It supports only `method` and `headers` keys of [`fetch`'s options parameter](https://developer.mozilla.org/en-US/docs/Web/API/fetch#parameters).Demo for the simple wrapper ([gm_fetch_simple_demo.user.js](https://github.com/AlttiRi/gm_fetch/raw/master/gm_fetch_simple_demo.user.js)):
```js
// ...
// @grant GM_xmlhttpRequest
// ==/UserScript==(async function demo() {
const url = "https://i.imgur.com/2wD3VJA.jpg";
const refererHeader = "https://imgur.com/";
const response = await GM_fetch(url, {headers: {referer: refererHeader}});
const {status, statusText, ok} = response;
const lastModified = response.headers.get("last-modified");
console.log(url, {status, statusText, ok, lastModified});const blob = await response.blob();
const name = url.split("/").pop();
downloadBlob(blob, name, url);
})();function downloadBlob(blob, name, url) {
const anchor = document.createElement("a");
anchor.setAttribute("download", name || "");
const blobUrl = URL.createObjectURL(blob);
anchor.href = blobUrl + (url ? ("#" + url) : "");
anchor.click();
setTimeout(() => URL.revokeObjectURL(blobUrl), 5000);
}// ...
// async function GM_fetch(url, init = {}) {
// ...
```---
- https://www.tampermonkey.net/documentation.php#GM_xmlhttpRequest
- https://violentmonkey.github.io/api/gm/#gm_xmlhttprequest
- https://wiki.greasespot.net/GM.xmlHttpRequest---
- https://developer.mozilla.org/en-US/docs/Web/API/fetch
- https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Referrer-Policy