Registry / testing / gmxhr-fetch

gmxhr-fetch

JSON →
library0.1.2jsnpmunverified

A fetch-like polyfill for userscript managers (Tampermonkey, Greasemonkey, Violentmonkey). Version 0.1.2 provides a familiar `fetch` interface over `GM_xmlhttpRequest`, allowing userscript authors to use Promise-based HTTP requests without managing raw GM API calls. Lightweight, no external dependencies, supports typical fetch options (method, headers, body). Key differentiator: simplifies migrating from standard fetch to userscript context by mimicking the Fetch API.

npm install gmxhr-fetch
INSTALL
IMPORT
SIG · GMXHR-FETCH
G
gmxhr-fetch
testingjavascriptv0.1.2
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

gmfetch
import { gmfetch } from 'gmxhr-fetch'
const gmfetch = require('gmxhr-fetch')
ESM-only; CommonJS require not supported. In userscripts, add @require directive in metablock.
default
import gmfetch from 'gmxhr-fetch'
import { gmfetch } from 'gmxhr-fetch'
When imported as default, the symbol is named `gmfetch`; but the library's default export is also named `gmfetch`. Both work.
GM_xmlhttpRequest
// @grant GM_xmlhttpRequest
// @grant GM.xmlHttpRequest
Must grant both `GM_xmlhttpRequest` and `GM.xmlHttpRequest` in metablock for cross-platform compatibility.

Demonstrates basic usage in a userscript: making a POST request with headers and body, handling response and errors.

// ==UserScript== // @name my script // @require https://unpkg.com/gmxhr-fetch // @grant GM_xmlhttpRequest // @grant GM.xmlHttpRequest // ==/UserScript== async function main() { try { const response = await gmfetch('https://api.example.com/data', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ key: 'value' }) }); const data = await response.json(); console.log('Success:', data); } catch (error) { console.error('Request failed:', error); } } main();
Debug
Known issues
gotcha`gmfetch` does not support streaming (e.g., response.body) or abort signals (AbortController).
fix
Do not rely on Streams or AbortController; use alternative methods if needed.
affects: <=0.1.2
gotchaThe response object's `.json()`, `.text()`, etc. may not fully mimic the Fetch API response (e.g., statusText, redirected).
fix
Test response properties before using non-standard ones.
affects: <=0.1.2
breakingRequires both `GM_xmlhttpRequest` and `GM.xmlHttpRequest` grants; missing one may cause silent failures.
fix
Add both @grant directives to userscript metablock.
affects: >=0.1.0
deprecatedPackage version 0.1.2 is early alpha; API may change without notice.
fix
Pin version in @require URL or monitor repository for updates.
affects: =0.1.2
Errors
Common errors & fixes
TypeError: gmfetch is not a function
Missing @require directive or incorrect import statement (e.g., using require instead of @require).
fix
Add `// @require https://unpkg.com/gmxhr-fetch` to metablock and ensure script runs in userscript context.
gmfetch is not defined
Incorrect @grant directives or using gmfetch outside userscript scope.
fix
Grant both `GM_xmlhttpRequest` and `GM.xmlHttpRequest` in metablock.
ReferenceError: GM_xmlhttpRequest is not defined
Missing @grant for GM_xmlhttpRequest.
fix
Add `// @grant GM_xmlhttpRequest` to metablock.
Upgrade
Version history
0.1.2latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
6 hits · last 30 days
node
6
Resources
gmxhr-fetch — npm install gmxhr-fetch · libregistry