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 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")