-
Notifications
You must be signed in to change notification settings - Fork 65
GM_xmlhttpRequest
GM_xmlhttpRequest is a cross-origin version of XMLHttpRequest. The beauty of this function is that a user script can make requests that do not use the same-origin policy, creating opportunities for powerful mashups.
GM_xmlhttpRequest restricts access to the http, https, ftp, data, blob, and moz-blob protocols.
If a script uses one or more @domains then the GM_xmlhttpRequest api will be restricted to those domains.
If the url provided does not pass the above criteria then a error will be thrown when calling GM_xmlhttpRequest
A single object with properties defining the request behavior.
-
Stringmethod: Optional. The HTTP method to utilize. Currently only "GET" and "POST" are supported. Defaults to "GET". -
Stringurl: The URL to which the request will be sent. This value may be relative to the page the user script is running on. -
Functiononload: Optional. A function called if the request finishes successfully. Passed a Scriptish response object (see below). -
Functiononerror: Optional. A function called if the request fails. Passed a Scriptish response object (see below). -
Functiononreadystatechange: Optional. A function called whenever the request'sreadyStatechanges. Passed a Scriptish response object (see below). -
Stringdata: Optional. Content to send as the body of the request. -
Objectheaders: Optional. An object containing headers to be sent as part of the request. -
Booleanbinary: Optional. Forces the request to senddataas binary. Defaults tofalse. -
BooleanmakePrivate: Optional. Forces the request to be a private request (same as initiated from a private window). (0.1.9+) -
BooleanmozBackgroundRequest: Optional. Iftruesecurity dialogs will not be shown, and the request will fail. Defaults totrue. -
Stringuser: Optional. The user name to use for authentication purposes. Defaults to the empty string"". -
Stringpassword: Optional. The password to use for authentication purposes. Defaults to the empty string"". -
StringoverrideMimeType: Optional. Overrides the MIME type returned by the server. -
BooleanignoreCache: Optional. Forces a request to the server, bypassing the cache. Defaults tofalse. -
BooleanignoreRedirect: Optional. Forces the request to ignore both temporary and permanent redirects. -
BooleanignoreTempRedirect: Optional. Forces the request to ignore only temporary redirects. -
BooleanignorePermanentRedirect: Optional. Forces the request to ignore only permanent redirects. -
BooleanfailOnRedirect: Optional. Forces the request to fail if a redirect occurs. -
IntegerredirectionLimit: Optional. Range allowed: 0-10. Forces the request to fail if a certain number of redirects occur.- Note: A
redirectionLimitof0is equivalent to settingfailOnRedirecttotrue.
- Note: A
Note: If both are set, redirectionLimit will take priority over failOnRedirect.
Note: When ignore*Redirect is set and a redirect is encountered the request will still succeed, and subsequently call onload. failOnRedirect or redirectionLimit exhaustion, however, will produce an error when encountering a redirect, and subsequently call onerror.
This is the response object passed to the onload, onerror, and onreadystatechange callbacks described for the details object above.
-
StringresponseText: The response to the request in text form. -
StringresponseJSON: If the content type is JSON (example:application/json,text/x-json, and more..) thenresponseJSONwill be available. -
IntegerreadyState: The state of the request. Refer to https://developer.mozilla.org/en/XMLHttpRequest#Properties -
StringresponseHeaders: The string value of all response headers.nullif no response has been received. -
Integerstatus: The HTTP status code from the server.nullif the request hasn't yet completed, or resulted in an error. -
StringstatusText: The entire HTTP status response string from the server.nullif the request hasn't yet completed, or resulted in an error. -
StringfinalUrl: The final URL used for the request. Takes redirects into account.nullif the request hasn't yet completed, or resulted in an error.
For "onprogress" only:
-
BooleanlengthComputable: Whether it is currently possible to know the total size of the response. -
Integerloaded: The number of bytes loaded thus far. -
Integertotal: The total size of the response.
-
Functionabort
Aborts the associated request if it has already been sent.
// Logs the contents of http://www.google.com
var ret = GM_xmlhttpRequest({
method: "GET",
url: "http://www.google.com",
onload: function(res) {
GM_log(res.responseText);
}
});// Create a request, and then abort it.
var ret = GM_xmlhttpRequest({
method: "GET",
url: "http://www.google.com",
onload: function(res) {
GM_log(res.responseText);
}
});
ret.abort();// POST some data to a site
// Uses `FormData` introduced in Gecko 2.0
// See https://developer.mozilla.org/en/XMLHttpRequest/FormData
var myData = new FormData();
myData.append("foo", "bar");
myData.append("baz", 12345);
var ret = GM_xmlhttpRequest({
method: "POST",
data: myData,
url: "http://www.someinvalidsite.com/acceptsPOSTreqs.php",
onload: function(res) {
GM_log(res.responseText);
}
});// Create a request, asking the server for a JSON response via the Accept header
// Also logging whenever the state of the request changes
var ret = GM_xmlhttpRequest({
method: "GET",
headers: {"Accept": "application/json"},
url: "http://www.somejsonfriendlysite.com",
onreadystatechange: function(res) {
GM_log("Request state changed to: " + res.readyState);
},
onload: function(res) {
// Lets assume we get JSON back...
var myJSON = res.responseJSON;
// Profit!
}
});// onerror will be called if the request fails
var ret = GM_xmlhttpRequest({
method: "GET",
url: "http://www.someinvalidsite.com",
onload: function(res) {
GM_log(res.responseText);
},
onerror: function(res) {
var msg = "An error occurred."
+ "\nresponseText: " + res.responseText
+ "\nreadyState: " + res.readyState
+ "\nresponseHeaders: " + res.responseHeaders
+ "\nstatus: " + res.status
+ "\nstatusText: " + res.statusText
+ "\nfinalUrl: " + res.finalUrl;
GM_log(msg);
}
});// Bypasses the cache and fails if we get redirected
// This request will fail, since http://www.github.com redirects to https://www.github.com
var ret = GM_xmlhttpRequest({
method: "GET",
url: "http://www.github.com",
ignoreCache: true,
redirectionLimit: 0, // this is equivalent to 'failOnRedirect: true'
onload: function(res) {
GM_log(res.responseText);
},
onerror: function(res) {
GM_log("Error!");
}
});