Table of Contents Previous entry (api:GM_removeValueChangeListener) Next entry (api:GM_webRequest)
The GM_xmlhttpRequest allows a userscripts to send an HTTP request and handle the response.
The function takes a single parameter: an object containing the details of the request to be sent and the callback functions to be called when the response is received.
The object can have the following properties:
Blob or File objectv5.4.6226+user-agent, referer, ...
(some special headers are not supported by Safari and Android browsers)follow, error or manual; controls what to happen when a redirect is detected (build 6180+, enforces fetch mode)arraybuffer, blob, json or streamfetch mode)fetch instead of a XMLHttpRequest request
(at Chrome this causes details.timeout and xhr.onprogress to not work and makes xhr.onreadystatechange receive only readyState DONE (==4) events)streamreadyState changed function(response) {
// response is an object containing the details of the response
}
response has the following attributes:readyStatedetails.responseType was setGM_xmlhttpRequest is dispatched by Tampermonkey's background context.
Proxy. Tampermonkey does not implement its own proxy resolution (PAC, WPAD, WinHTTP, or NO_PROXY). Requests use the browser's native networking stack, so proxy settings follow whatever the browser is configured to use. The proxy property on the request details allows you to override this and route a specific request through an explicit proxy server.
Certificates. No custom certificate or trust-store handling is performed. Requests use the browser's TLS stack, so they trust whichever root certificates the browser trusts (e.g., OS trust store on Chrome, Firefox's own certificate database on Firefox).
Authentication Challenges (401/407). HTTP 401 and 407 responses come from the destination server or proxy before Tampermonkey can intercept them. They must be resolved at the browser or OS proxy-authentication level, or by providing credentials via the user/password properties.
GM_xmlhttpRequest returns an object with the following property:
GM.xmlHttpRequest returns a promise that resolves to the response and also has an abort function.
Here is an example of how the GM_xmlhttpRequest function might be used in a userscript:
GM_xmlhttpRequest({
method: "GET",
url: "https://example.com/",
headers: {
"Content-Type": "application/json"
},
onload: function(response) {
console.log(response.responseText);
}
});
const r = await GM.xmlHttpRequest({ url: "https://example.com/" }).catch(e => console.error(e));
console.log(r.responseText);
Note: the synchronous flag at details is not supported
Important::
@connectGM.xmlHttpRequest (with a uppercase "h" in "http")