GM.xmlHttpRequest: Difference between revisions
From GreaseSpot Wiki
Jump to navigationJump to search
m →Syntax: Monkey colored tables |
m Italicized details and response for emphasis |
||
Line 21: | Line 21: | ||
:{| cellpadding="5" style="border-style:solid; background:#FFFFE0;" | :{| cellpadding="5" style="border-style:solid; background:#FFFFE0;" | ||
|+ details Object | |+ ''details'' Object | ||
!style="background:#CC9900;"|'''Properties''' || style="background:#CC9900;"|'''Event Handler Callbacks''' | !style="background:#CC9900;"|'''Properties''' || style="background:#CC9900;"|'''Event Handler Callbacks''' | ||
|- | |- | ||
Line 42: | Line 42: | ||
==== <code>method</code> ==== | ==== <code>method</code> ==== | ||
:Value: String | :Value: String | ||
:Usage: <code>details.'''method''' = "GET"';</code> | :Usage: <code>''details''.'''method''' = "GET"';</code> | ||
[[#top|top]] | [[#Syntax|back]]'' | [[#top|top]] | [[#Syntax|back]]'' | ||
Line 48: | Line 48: | ||
==== <code>url</code> ==== | ==== <code>url</code> ==== | ||
:Value: String | :Value: String | ||
: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]] | [[#top|top]] | [[#Syntax|back]] | ||
Line 54: | Line 54: | ||
==== <code>headers</code> ==== | ==== <code>headers</code> ==== | ||
:Value: Object | :Value: Object | ||
:Usage: <code>details.'''headers''' = {"User-Agent":"Mozilla/5.0"};</code> | :Usage: <code>''details''.'''headers''' = {"User-Agent":"Mozilla/5.0"};</code> | ||
:* 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> | ||
Line 63: | Line 63: | ||
:Value: String | :Value: String | ||
:Compatibility: [[Version_history#0.6.8|Greasemonkey 0.6.8+]] | :Compatibility: [[Version_history#0.6.8|Greasemonkey 0.6.8+]] | ||
:Usage: <code>details.'''overrideMimeType''' = "text/html; charset=ISO-8859-1";</code> | :Usage: <code>''details''.'''overrideMimeType''' = "text/html; charset=ISO-8859-1";</code> | ||
:* While each character set name may have multiple aliases defined, there may be a preferred name which can be found at [http://www.iana.org/assignments/character-sets IANA] | :* While each character set name may have multiple aliases defined, there may be a preferred name which can be found at [http://www.iana.org/assignments/character-sets IANA] | ||
Line 70: | Line 70: | ||
==== <code>data</code> ==== | ==== <code>data</code> ==== | ||
:Value: String | :Value: String | ||
:Usage: <code>details.'''data''' = null; ''/* some code */'' if (details.'''data''') { ''/* some code */'' }</code> | :Usage: <code>''details''.'''data''' = null; ''/* some code */'' if (''details''.'''data''') { ''/* some code */'' }</code> | ||
:* <sup>[[#Notes|[1]]]</sup> | :* <sup>[[#Notes|[1]]]</sup> | ||
Line 78: | Line 78: | ||
---- | ---- | ||
==== onload ==== | ==== onload ==== | ||
:Usage: <code>details.'''onload''' = function (''response'') { ''/* some code */'' };</code> | :Usage: <code>''details''.'''onload''' = function (''response'') { ''/* some code */'' };</code> | ||
:Returns: Nothing, response Object | :Returns: Nothing, response Object | ||
Line 84: | Line 84: | ||
==== onreadystatechange ==== | ==== onreadystatechange ==== | ||
:Usage: <code>details.'''onreadystatechange''' = function (''response'') { ''/* some code */'' };</code> | :Usage: <code>''details''.'''onreadystatechange''' = function (''response'') { ''/* some code */'' };</code> | ||
:Returns: Nothing, response Object | :Returns: Nothing, response Object | ||
Line 90: | Line 90: | ||
==== onerror ==== | ==== onerror ==== | ||
:Usage: <code>details.'''onerror''' = function (''response'') { ''/* some code */'' };</code> | :Usage: <code>''details''.'''onerror''' = function (''response'') { ''/* some code */'' };</code> | ||
:Returns: Nothing, response Object | :Returns: Nothing, response Object | ||
[[#top|top]] | [[#Syntax|back]] | [[#top|top]] | [[#Syntax|back]] | ||
:{| cellpadding="5" style="border-style:solid; background:#FFFFE0;" | :{| cellpadding="5" style="border-style:solid; background:#FFFFE0;" | ||
|+ response Object | |+ ''response'' Object | ||
! colspan="3" style="background:#CC9900;" |'''Properties''' | ! colspan="3" style="background:#CC9900;" |'''Properties''' | ||
|- | |- | ||
Line 110: | Line 110: | ||
==== status ==== | ==== status ==== | ||
:Value: Number | :Value: Number | ||
:Usage: <code>if (response.'''status''' == 200) { ''/* some code */'' }</code> | :Usage: <code>if (''response''.'''status''' == 200) { ''/* some code */'' }</code> | ||
[[#top|top]] | [[#Event_Handler_Callbacks|back]] | [[#top|top]] | [[#Event_Handler_Callbacks|back]] | ||
Line 116: | Line 116: | ||
==== statusText ==== | ==== statusText ==== | ||
:Value: String | :Value: String | ||
:Usage: <code>if (response.'''statusText''' == "OK") { ''/* some code */'' }</code> | :Usage: <code>if (''response''.'''statusText''' == "OK") { ''/* some code */'' }</code> | ||
[[#top|top]] | [[#Event_Handler_Callbacks|back]] | [[#top|top]] | [[#Event_Handler_Callbacks|back]] | ||
Line 122: | Line 122: | ||
==== readyState ==== | ==== readyState ==== | ||
:Value: Number | :Value: Number | ||
:Usage: <code>if (response.'''readyState''' == 4) { ''/* some code */'' }</code> | :Usage: <code>if (''response''.'''readyState''' == 4) { ''/* some code */'' }</code> | ||
[[#top|top]] | [[#Event_Handler_Callbacks|back]] | [[#top|top]] | [[#Event_Handler_Callbacks|back]] | ||
Line 128: | Line 128: | ||
==== responseText ==== | ==== responseText ==== | ||
:Value: String | :Value: String | ||
:Usage: <code>alert(response.'''responseText''');</code> | :Usage: <code>alert(''response''.'''responseText''');</code> | ||
[[#top|top]] | [[#Event_Handler_Callbacks|back]] | [[#top|top]] | [[#Event_Handler_Callbacks|back]] | ||
Line 134: | Line 134: | ||
==== responseHeaders ==== | ==== responseHeaders ==== | ||
:Value: String | :Value: String | ||
:Usage: <code>if (response.'''responseHeaders''') { ''/* some code */'' }</code> | :Usage: <code>if (''response''.'''responseHeaders''') { ''/* some code */'' }</code> | ||
:* 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>. |
Revision as of 22:53, 11 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";
- While each character set name may have multiple aliases defined, there may be a preferred name which can be found at IANA
data
- Value: String
- Usage:
details.data = null; /* some code */ if (details.data) { /* some code */ }
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 status
readyState
responseText
statusText
responseHeaders
Properties
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 */ }
responseText
- Value: String
- Usage:
alert(response.responseText);
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
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.