GM.xmlHttpRequest
Description
This method performs a similar function to the standard XMLHttpRequest object, but allows these requests to cross the same origin policy boundaries.
Syntax
function GM.xmlHttpRequest( details )
Compatibility: Greasemonkey 4.0+
Arguments
This method only takes one argument, the details object.
Described below are the properties that may be defined on that object.
See #Examples for more detail on how to use each.
Fields:
binaryBooleanOptional, default false. When true, thedatais sent as a Blob.contextObject(Compatibility: 1.10+) Optional, any object. This object will also be thecontextproperty of the #Response Object.dataStringOptional. Data to send in the request body. Usually forPOSTmethod requests. [1]headersObjectOptional. A set of headers to include in the request. [2]methodStringRequired. Type of HTTP request to make (E.G."GET","POST")overrideMimeTypeStringOptional. A MIME type to specify with the request (E.G."text/html; charset=ISO-8859-1").passwordStringOptional. Password to use for authentication purposes.responseTypeStringOptional. Decode the response as specified type. Accepted values are"","arraybuffer","blob","document","json","text","ms-stream". Default value is"text". See XMLHttpRequest responseType.synchronousBooleanDefaults to false. When true, this is a synchronous request. Be careful: The entire Firefox UI will be locked and frozen until the request completes. In this mode, more data will be available in the return value.timeoutNumberThe number of milliseconds to wait before terminating the call; zero (the default) means wait forever.uploadObjectOptional. Object containing optional function callbacks (onabort,onerror,onload,onprogress) to monitor the upload of data. Each is passed one argument, the #Response Object.urlStringRequired. The URL to make the request to. Must be an absolute URL, beginning with the scheme. May be relative to the current page.userStringOptional. User name to use for authentication purposes.
Event handlers:
onabortFunctionOptional. Will be called when the request is aborted. Passed one argument, the #Response Object.onerrorFunctionOptional. Will be called if an error occurs while processing the request. Passed one argument, the #Response Object.onloadFunctionOptional. Will be called when the request has completed successfully. Passed one argument, the #Response Object.onprogressFunctionOptional. Will be called when the request progress changes. Passed one argument, the #Response Object.onreadystatechangeFunctionOptional. Will be called repeatedly while the request is in progress. Passed one argument, the #Response Object.ontimeoutFunctionOptional. Will be called if/when the request times out. Passed one argument, the #Response Object.
Response Object
All of the callback functions defined in the details object, if called, will receive this type of object as their first (and only) argument.
The data available will vary slightly, depending on the type of callback.
Properties based on a standard XMLHttpRequest object:
readyStateresponseHeaders:String, withCRLFline terminators.responseTextstatusstatusText
Greasemonkey custom properties:
contextObjectThe same object passed into the original request.
Properties for progress callbacks, based on nsIDOMProgressEvent:
lengthComputableloadedtotal
Returns
undefined
Examples
Bare Minimum
GM.xmlHttpRequest({
method: "GET",
url: "http://www.example.com/",
onload: function(response) {
alert(response.responseText);
}
});
GET request
GM.xmlHttpRequest({
method: "GET",
url: "http://www.example.net/",
headers: {
"User-Agent": "Mozilla/5.0", // If not specified, navigator.userAgent will be used.
"Accept": "text/xml" // If not specified, browser defaults will be used.
},
onload: function(response) {
var responseXML = null;
// Inject responseXML into existing Object (only appropriate for XML content).
if (!response.responseXML) {
responseXML = new DOMParser()
.parseFromString(response.responseText, "text/xml");
}
console.log([
response.status,
response.statusText,
response.readyState,
response.responseHeaders,
response.responseText,
response.finalUrl,
responseXML
].join("\n"));
}
});
POST request
When making a POST request, most sites require the Content-Type header to be defined as such:
GM.xmlHttpRequest({
method: "POST",
url: "http://www.example.net/login",
data: "username=johndoe&password=xyz123",
headers: {
"Content-Type": "application/x-www-form-urlencoded"
},
onload: function(response) {
if (response.responseText.indexOf("Logged in as") > -1) {
location.href = "http://www.example.net/dashboard";
}
}
});
HEAD request
As defined in HTTP, you may issue a HEAD request to get the response headers, without receiving the entire response body.
GM.xmlHttpRequest({
url: "http://www.example.com",
method: "HEAD",
onload: function(response) {
console.log(response.responseHeaders);
}
});
Notes
If the data field contains form-encoded data, you usually must also set the header 'Content-Type': 'application/x-www-form-urlencoded' in the headers field.