GM xmlhttpRequest
From GreaseSpot
- The correct title of this article is GM_xmlhttpRequest. The substitution or omission of an _ is due to technical restrictions.
Greasemonkey Manual |
| Using Greasemonkey |
|---|
| Installing Scripts |
| Manage Dialog |
| Monkey Menu |
| User Script Authoring |
| Editing |
| Other Useful Tools |
| Environment |
| API |
| User Contributions |
| Code Snippets |
[edit] Description
This API method provides access to the chrome-privileged XMLHttpRequest object. This means that it is possible to issue requests to domains other than that of the current page.
Additional reference may be found at:
[edit] Syntax
GM_xmlhttpRequest( details )
- Value: Function
- Returns: undefined, response Object
- Compatibility: Greasemonkey 0.2.5+
details Object Properties Event Handler Callbacks methodonloadurlonreadystatechangeheadersonerroroverrideMimeTypedata
[edit] Properties
[edit] method
- Value: String
- Usage:
details.method = "GET"';
[edit] url
- Value: String
- Usage:
details.url = "http://www.greasespot.net/";
- Must be an absolute URL.
[edit] headers
- Value: Object
- Usage:
details.headers = {"User-Agent":"Mozilla/5.0"};
- The
headersproperty is an object which is typically used to override the default browser generated headers, also known as atoms. It should contain the atom-value pairs of the headers to send.[2]
- The
[edit] overrideMimeType
- Value: String
- Compatibility: Greasemonkey 0.6.8+
- Usage:
details.overrideMimeType = "text/html; charset=ISO-8859-1";
- While each character set name may have multiple aliases defined, there may be a preferred name which can be found at IANA
[edit] data
- Value: String
- Usage:
details.data = null; /* some code */ if (details.data) { /* some code */ }- [1]
- Note that if the
datafield contains form-encoded data, you must also set the header'Content-Type': 'application/x-www-form-urlencoded'in theheadersfield.
[edit] Event Handler Callbacks
[edit] onload
- Usage:
details.onload = function (response) { /* some code */ }; - Returns: undefined, response Object
[edit] onreadystatechange
- Usage:
details.onreadystatechange = function (response) { /* some code */ }; - Returns: undefined, response Object
[edit] onerror
- Usage:
details.onerror = function (response) { /* some code */ }; - Returns: undefined, response Object
response Object Properties statusreadyStateresponseTextfinalUrlstatusTextresponseHeaders
- Note: there is no
responseXMLproperty, contrary to what people used to XMLHttpRequest might expect. This is due to security restrictions imposed on the XMLDocument created in the privileged GM core context. One can easily create its own XMLDocument out ofresponseTextusing a DOMParser (see examples below).
- Note: there is no
[edit] Properties
[edit] status
- Value: Number
- Usage:
if (response.status == 200) { /* some code */ }
[edit] statusText
- Value: String
- Usage:
if (response.statusText == "OK") { /* some code */ }
[edit] readyState
- Value: Number
- Usage:
if (response.readyState == 4) { /* some code */ }
[edit] responseText
- Value: String
- Usage:
alert(response.responseText);
[edit] responseHeaders
- Value: String
- Usage:
if (response.responseHeaders) { /* some code */ }
- The
responseHeadersis the string representation of response headers returned byXMLHTTPRequest.getAllResponseHeaders().
- The
[edit] finalUrl
- Value: String
- Compatibility: Greasemonkey 0.8.0+
- Usage:
if (response.finalUrl) { /* some code */ }
[edit] Examples
GM_xmlhttpRequest({
method: "GET",
url: "http://www.example.net/",
headers: {
"User-Agent": "Mozilla/5.0", // Recommend using navigator.userAgent when possible
"Accept": "text/xml"
},
onload: function(response) {
// Inject responseXML into existing Object if not present
if (!response.responseXML)
response.responseXML = new DOMParser().parseFromString(response.responseText, "text/xml");
GM_log([
response.status,
response.statusText,
response.readyState,
response.responseHeaders,
response.responseText,
response.finalUrl,
response.responseXML
].join("\n"));
}
});
When making a POST request, some sites require the Content-Type header to be included 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";
}
});
[edit] Notes
[1]Some users have reported problems with international character sets. See these mailing list threads
- http://www.mozdev.org/pipermail/greasemonkey/2006-June/008785.html
- http://www.mozdev.org/pipermail/greasemonkey/2006-April/008064.html
Resolved: Greasemonkey exposed the overrideMimeType method to allow changing of the desired charset.
[2]Some header atoms may not actually work through GM_xmlhttpRequest. The Referer atom is one that is known to be recognized but appears to be content filtered by the browser. This may be due to security restrictions to keep user scripts from doing unsafe things.


