GM.xmlHttpRequest: Difference between revisions
From GreaseSpot Wiki
Jump to navigationJump to search
m →Notes |
mNo edit summary |
||
Line 10: | Line 10: | ||
* [http://www.w3.org/TR/XMLHttpRequest/| http://www.w3.org/TR/XMLHttpRequest] | * [http://www.w3.org/TR/XMLHttpRequest/| http://www.w3.org/TR/XMLHttpRequest] | ||
[[#Examples| | [[#Examples|Examples]] | [[#Notes|Notes]] | ||
= Syntax = | = Syntax = | ||
Line 42: | Line 42: | ||
:Usage: <code>details.'''method''' = "GET"';</code> | :Usage: <code>details.'''method''' = "GET"';</code> | ||
[[#Description| | [[#Description|top]] | [[#Syntax|back]]'' | ||
==== <code>url</code> ==== | ==== <code>url</code> ==== | ||
Line 48: | Line 48: | ||
:Usage: <code>details.'''url''' = "<nowiki>http://www.greasespot.net/</nowiki>"';</code> | :Usage: <code>details.'''url''' = "<nowiki>http://www.greasespot.net/</nowiki>"';</code> | ||
[[#Description| | [[#Description|top]] | [[#Syntax|back]] | ||
==== <code>headers</code> ==== | ==== <code>headers</code> ==== | ||
Line 56: | Line 56: | ||
: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. | :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. | ||
[[#Description| | [[#Description|top]] | [[#Syntax|back]] | ||
==== <code>overrideMimeType</code> ==== | ==== <code>overrideMimeType</code> ==== | ||
Line 62: | Line 62: | ||
:Usage: <code>details.'''overrideMimeType''' = "application/xhtml+xml";</code> | :Usage: <code>details.'''overrideMimeType''' = "application/xhtml+xml";</code> | ||
[[#Description| | [[#Description|top]] | [[#Syntax|back]] | ||
==== <code>data</code> ==== | ==== <code>data</code> ==== | ||
Line 68: | Line 68: | ||
:Usage: <code>details.'''data''' = null;</code> | :Usage: <code>details.'''data''' = null;</code> | ||
[[#Description| | [[#Description|top]] | [[#Syntax|back]] | ||
=== Event Handler Callbacks === | === Event Handler Callbacks === | ||
Line 76: | Line 76: | ||
:Returns: Nothing | :Returns: Nothing | ||
[[#Description| | [[#Description|top]] | [[#Syntax|back]] | ||
==== onreadystatechange ==== | ==== onreadystatechange ==== | ||
Line 82: | Line 82: | ||
:Returns: Nothing | :Returns: Nothing | ||
[[#Description| | [[#Description|top]] | [[#Syntax|back]] | ||
==== onerror ==== | ==== onerror ==== | ||
Line 88: | Line 88: | ||
:Returns: Nothing | :Returns: Nothing | ||
[[#Description| | [[#Description|top]] | [[#Syntax|back]] | ||
:{| border="1" cellpadding="5" | :{| border="1" cellpadding="5" | ||
|+ response Object | |+ response Object | ||
Line 110: | Line 110: | ||
:Usage: <code>alert(response.'''responseText''');</code> | :Usage: <code>alert(response.'''responseText''');</code> | ||
[[#Description| | [[#Description|top]] | [[#Event_Handler_Callbacks|back]] | ||
===== status ===== | ===== status ===== | ||
Line 116: | Line 116: | ||
:Usage: <code>if (response.'''status''' == 200) { ''//Some code'' };</code> | :Usage: <code>if (response.'''status''' == 200) { ''//Some code'' };</code> | ||
[[#Description| | [[#Description|top]] | [[#Event_Handler_Callbacks|back]] | ||
===== statusText ===== | ===== statusText ===== | ||
Line 122: | Line 122: | ||
:Usage: <code>if (response.'''statusText''' == "OK") { ''//Some code'' };</code> | :Usage: <code>if (response.'''statusText''' == "OK") { ''//Some code'' };</code> | ||
[[#Description| | [[#Description|top]] | [[#Event_Handler_Callbacks|back]] | ||
===== readyState ===== | ===== readyState ===== | ||
Line 128: | Line 128: | ||
:Usage: <code>if (response.'''readyState''' == 4) { ''//Some code'' }</code> | :Usage: <code>if (response.'''readyState''' == 4) { ''//Some code'' }</code> | ||
[[#Description| | [[#Description|top]] | [[#Event_Handler_Callbacks|back]] | ||
===== responseHeaders ===== | ===== responseHeaders ===== | ||
Line 136: | Line 136: | ||
: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>. | ||
[[#Description| | [[#Description|top]] | [[#Event_Handler_Callbacks|back]] | ||
= Examples = | = Examples = | ||
Line 159: | Line 159: | ||
</code> | </code> | ||
[[#Description| | [[#Description|top]] | ||
= Notes = | = Notes = | ||
Line 174: | Line 174: | ||
The <code>overrideMimeType</code> parameter became available in [[Version history#0.6.8|version 0.6.8]]. | The <code>overrideMimeType</code> parameter became available in [[Version history#0.6.8|version 0.6.8]]. | ||
[[#Description| | [[#Description|top]] | ||
[[Category:API_Reference]] | [[Category:API_Reference]] |
Revision as of 13:50, 6 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 )
- Compatibility: Greasemonkey 0.2.5+
- Returns: Nothing
details Object Properties Event Handler Callbacks method
onload
url
onreadystatechange
headers
onerror
overrideMimeType
data
- All properties and event handler callbacks are optional except method and url.
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.
overrideMimeType
- Value: String
- Usage:
details.overrideMimeType = "application/xhtml+xml";
data
- Value: String
- Usage:
details.data = null;
Event Handler Callbacks
onload
- Usage:
details.onload = function (response) { // Some code };
- Returns: Nothing
onreadystatechange
- Usage:
details.onreadystatechange = function (details) { // Some code };
- Returns: Nothing
onerror
- Usage:
details.onerror = function (details) { // Some code };
- Returns: Nothing
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()
.
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
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.
- http://groups.google.com/group/greasemonkey-dev/browse_thread/thread/77c94cc17c6b2669
- http://userscripts.org/forums/1/topics/1302?page=1#posts-5312
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
The overrideMimeType
parameter became available in version 0.6.8.