GM.xmlHttpRequest: Difference between revisions
From GreaseSpot Wiki
Jump to navigationJump to search
m →Notes: Nope... reverting |
mNo edit summary |
||
Line 37: | Line 37: | ||
:* All properties and event handler callbacks are optional except [[#method|method]] and [[#url|url]]. | :* All properties and event handler callbacks are optional except [[#method|method]] and [[#url|url]]. | ||
[[# | [[#top|top]] | ||
=== Properties === | === Properties === | ||
---- | ---- | ||
Line 44: | Line 44: | ||
:Usage: <code>details.'''method''' = "GET"';</code> | :Usage: <code>details.'''method''' = "GET"';</code> | ||
[[# | [[#top|top]] | [[#Syntax|back]]'' | ||
==== <code>url</code> ==== | ==== <code>url</code> ==== | ||
Line 50: | Line 50: | ||
:Usage: <code>details.'''url''' = "<nowiki>http://www.greasespot.net/</nowiki>"';</code> | :Usage: <code>details.'''url''' = "<nowiki>http://www.greasespot.net/</nowiki>"';</code> | ||
[[# | [[#top|top]] | [[#Syntax|back]] | ||
==== <code>headers</code> ==== | ==== <code>headers</code> ==== | ||
Line 58: | Line 58: | ||
:* The <code>headers</code> property 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.<sup>[[#Notes|[2]]]</sup> | :* The <code>headers</code> property 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.<sup>[[#Notes|[2]]]</sup> | ||
[[# | [[#top|top]] | [[#Syntax|back]] | ||
==== <code>overrideMimeType</code> ==== | ==== <code>overrideMimeType</code> ==== | ||
Line 65: | Line 65: | ||
:Usage: <code>details.'''overrideMimeType''' = "text/html; charset=iso-8859-1";</code> | :Usage: <code>details.'''overrideMimeType''' = "text/html; charset=iso-8859-1";</code> | ||
[[# | [[#top|top]] | [[#Syntax|back]] | ||
==== <code>data</code> ==== | ==== <code>data</code> ==== | ||
Line 72: | Line 72: | ||
:* <sup>[[#Notes|[1]]]</sup> | :* <sup>[[#Notes|[1]]]</sup> | ||
[[# | [[#top|top]] | [[#Syntax|back]] | ||
=== Event Handler Callbacks === | === Event Handler Callbacks === | ||
Line 80: | Line 80: | ||
:Returns: Nothing, response Object | :Returns: Nothing, response Object | ||
[[# | [[#top|top]] | [[#Syntax|back]] | ||
==== onreadystatechange ==== | ==== onreadystatechange ==== | ||
Line 86: | Line 86: | ||
:Returns: Nothing, response Object | :Returns: Nothing, response Object | ||
[[# | [[#top|top]] | [[#Syntax|back]] | ||
==== onerror ==== | ==== onerror ==== | ||
Line 92: | Line 92: | ||
:Returns: Nothing, response Object | :Returns: Nothing, response Object | ||
[[ | [[top|top]] | [[#Syntax|back]] | ||
:{| border="1" cellpadding="5" | :{| border="1" cellpadding="5" | ||
|+ response Object | |+ response Object | ||
Line 114: | Line 114: | ||
:Usage: <code>alert(response.'''responseText''');</code> | :Usage: <code>alert(response.'''responseText''');</code> | ||
[[# | [[#top|top]] | [[#Event_Handler_Callbacks|back]] | ||
==== status ==== | ==== status ==== | ||
Line 120: | Line 120: | ||
:Usage: <code>if (response.'''status''' == 200) { ''//Some code'' };</code> | :Usage: <code>if (response.'''status''' == 200) { ''//Some code'' };</code> | ||
[[# | [[#top|top]] | [[#Event_Handler_Callbacks|back]] | ||
==== statusText ==== | ==== statusText ==== | ||
Line 126: | Line 126: | ||
:Usage: <code>if (response.'''statusText''' == "OK") { ''//Some code'' };</code> | :Usage: <code>if (response.'''statusText''' == "OK") { ''//Some code'' };</code> | ||
[[# | [[#top|top]] | [[#Event_Handler_Callbacks|back]] | ||
==== readyState ==== | ==== readyState ==== | ||
Line 132: | Line 132: | ||
:Usage: <code>if (response.'''readyState''' == 4) { ''//Some code'' };</code> | :Usage: <code>if (response.'''readyState''' == 4) { ''//Some code'' };</code> | ||
[[# | [[#top|top]] | [[#Event_Handler_Callbacks|back]] | ||
==== responseHeaders ==== | ==== responseHeaders ==== | ||
Line 140: | Line 140: | ||
:* The <code>responseHeaders</code> is the string representation of response headers returned by <code>XMLHTTPRequest.getAllResponseHeaders()</code>. | :* The <code>responseHeaders</code> is the string representation of response headers returned by <code>XMLHTTPRequest.getAllResponseHeaders()</code>. | ||
[[# | [[#top|top]] | [[#Event_Handler_Callbacks|back]] | ||
== Examples == | == Examples == | ||
Line 163: | Line 163: | ||
</pre></code> | </pre></code> | ||
[[# | [[#top|top]] | ||
== Notes == | == Notes == | ||
Line 176: | Line 176: | ||
* http://userscripts.org/forums/1/topics/1302?page=1#posts-5312 | * http://userscripts.org/forums/1/topics/1302?page=1#posts-5312 | ||
[[# | [[#top|top]] | ||
[[Category:API_Reference|G]] | [[Category:API_Reference|G]] |
Revision as of 09:14, 8 December 2007
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:
Syntax
GM_xmlhttpRequest( details )
- Value: Function
- Returns: Nothing, details Object
- Compatibility: Greasemonkey 0.2.5+
details Object Properties Event Handler Callbacks method
onload
url
onreadystatechange
headers
onerror
overrideMimeType
data
Properties
method
- Value: String
- Usage:
details.method = "GET"';
url
- Value: String
- Usage:
details.url = "http://www.greasespot.net/"';
headers
- Value: Object
- Usage:
details.headers = {"User-Agent":"Mozilla/5.0"};
- The
headers
property 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
overrideMimeType
- Value: String
- Compatibility: Greasemonkey 0.6.8+
- Usage:
details.overrideMimeType = "text/html; charset=iso-8859-1";
data
- Value: String
- Usage:
details.data = null;
Event Handler Callbacks
onload
- Usage:
details.onload = function (response) { // Some code };
- Returns: Nothing, response Object
onreadystatechange
- Usage:
details.onreadystatechange = function (response) { // Some code };
- Returns: Nothing, response Object
onerror
- Usage:
details.onerror = function (response) { // Some code };
- Returns: Nothing, response Object
response Object Properties Event Handler Callbacks responseText
status
statusText
readyState
responseHeaders
Properties
responseText
- Value: String
- Usage:
alert(response.responseText);
status
- Value: Number
- Usage:
if (response.status == 200) { //Some code };
statusText
- Value: String
- Usage:
if (response.statusText == "OK") { //Some code };
readyState
- Value: Number
- Usage:
if (response.readyState == 4) { //Some code };
responseHeaders
- Value: String
- Usage:
if (response.responseHeaders) { //Some code };
- The
responseHeaders
is the string representation of response headers returned byXMLHTTPRequest.getAllResponseHeaders()
.
- The
Examples
GM_xmlhttpRequest({
method:"GET",
url:"http://www.greasespot.net/",
headers:{
"User-Agent":"Mozilla/5.0", // Recommend using navigator.userAgent when possible
"Accept":"text/xml"
},
onload:function(response) {
alert([
response.status,
response.statusText,
response.readyState,
response.responseHeaders,
response.responseText
].join("\n"));
}
});
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
[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.