| OLD | NEW |
| (Empty) |
| 1 <!DOCTYPE html><!-- This page is a placeholder for generated extensions api doc.
Note: | |
| 2 1) The <head> information in this page is significant, should be uniform | |
| 3 across api docs and should be edited only with knowledge of the | |
| 4 templating mechanism. | |
| 5 3) All <body>.innerHTML is genereated as an rendering step. If viewed in a | |
| 6 browser, it will be re-generated from the template, json schema and | |
| 7 authored overview content. | |
| 8 4) The <body>.innerHTML is also generated by an offline step so that this | |
| 9 page may easily be indexed by search engines. | |
| 10 --><html xmlns="http://www.w3.org/1999/xhtml"><head> | |
| 11 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> | |
| 12 <link href="css/ApiRefStyles.css" rel="stylesheet" type="text/css"> | |
| 13 <link href="css/print.css" rel="stylesheet" type="text/css" media="print"> | |
| 14 <script type="text/javascript" src="../../../third_party/jstemplate/jstempla
te_compiled.js"> | |
| 15 </script> | |
| 16 <script type="text/javascript" src="../../../../third_party/json_minify/mini
fy-sans-regexp.js"> | |
| 17 </script> | |
| 18 <script type="text/javascript" src="js/api_page_generator.js"></script> | |
| 19 <script type="text/javascript" src="js/bootstrap.js"></script> | |
| 20 <script type="text/javascript" src="js/sidebar.js"></script> | |
| 21 <title>Cross-Origin XMLHttpRequest - Google Chrome Extensions - Google Code</t
itle></head> | |
| 22 <body> <div id="devModeWarning" class="displayModeWarning"> | |
| 23 You are viewing extension docs in chrome via the 'file:' scheme: are you exp
ecting to see local changes when you refresh? You'll need run chrome with --allo
w-file-access-from-files. | |
| 24 </div> | |
| 25 <div id="branchWarning" class="displayModeWarning"> | |
| 26 <span>WARNING: This is the <span id="branchName">BETA</span> documentation. | |
| 27 It may not work with the stable release of Chrome.</span> | |
| 28 <select id="branchChooser"> | |
| 29 <option>Choose a different version... | |
| 30 </option><option value="">Stable | |
| 31 </option><option value="beta">Beta | |
| 32 </option><option value="dev">Dev | |
| 33 </option><option value="trunk">Trunk | |
| 34 </option></select> | |
| 35 </div> | |
| 36 <div id="unofficialWarning" class="displayModeWarning"> | |
| 37 <span>WARNING: This is unofficial documentation. It may not work with the | |
| 38 current release of Chrome.</span> | |
| 39 <button id="goToOfficialDocs">Go to the official docs</button> | |
| 40 </div> | |
| 41 <div id="gc-container" class="labs"> | |
| 42 <!-- SUBTEMPLATES: DO NOT MOVE FROM THIS LOCATION --> | |
| 43 <!-- In particular, sub-templates that recurse, must be used by allowing | |
| 44 jstemplate to make a copy of the template in this section which | |
| 45 are not operated on by way of the jsskip="true" --> | |
| 46 <!-- /SUBTEMPLATES --> | |
| 47 <a id="top"></a> | |
| 48 <div id="skipto"> | |
| 49 <a href="#gc-pagecontent">Skip to page content</a> | |
| 50 <a href="#gc-toc">Skip to main navigation</a> | |
| 51 </div> | |
| 52 <!-- API HEADER --> | |
| 53 <table id="header" width="100%" cellspacing="0" border="0"> | |
| 54 <tbody><tr> | |
| 55 <td valign="middle"><a href="http://code.google.com/"><img src="images/c
ode_labs_logo.gif" height="43" width="161" alt="Google Code Labs" style="border:
0; margin:0;"></a></td> | |
| 56 <td valign="middle" width="100%" style="padding-left:0.6em;"> | |
| 57 <form action="http://www.google.com/cse" id="cse" style="margin-top:0.
5em"> | |
| 58 <div id="gsc-search-box"> | |
| 59 <input type="hidden" name="cx" value="002967670403910741006:61_cvz
fqtno"> | |
| 60 <input type="hidden" name="ie" value="UTF-8"> | |
| 61 <input type="text" name="q" value="" size="55"> | |
| 62 <input class="gsc-search-button" type="submit" name="sa" value="Se
arch"> | |
| 63 <br> | |
| 64 <span class="greytext">e.g. "page action" or "tabs"</span> | |
| 65 </div> | |
| 66 </form> | |
| 67 <script type="text/javascript" src="https://www.google.com/jsapi"></sc
ript> | |
| 68 <script type="text/javascript">google.load("elements", "1", {packages:
"transliteration"});</script> | |
| 69 <script type="text/javascript" src="https://www.google.com/coop/cse/t1
3n?form=cse&t13n_langs=en"></script> | |
| 70 <script type="text/javascript" src="https://www.google.com/coop/cse/br
and?form=cse&lang=en"></script> | |
| 71 </td> | |
| 72 </tr> | |
| 73 </tbody></table> | |
| 74 <div id="codesiteContent" class=""> | |
| 75 <a id="gc-topnav-anchor"></a> | |
| 76 <div id="gc-topnav"> | |
| 77 <h1>Google Chrome Extensions (<a href="http://code.google.com/labs/">Lab
s</a>)</h1> | |
| 78 <ul id="home" class="gc-topnav-tabs"> | |
| 79 <li id="home_link"> | |
| 80 <a href="index.html" title="Google Chrome Extensions home page">Home
</a> | |
| 81 </li> | |
| 82 <li id="docs_link"> | |
| 83 <a href="docs.html" title="Official Google Chrome Extensions documen
tation">Docs</a> | |
| 84 </li> | |
| 85 <li id="faq_link"> | |
| 86 <a href="faq.html" title="Answers to frequently asked questions abou
t Google Chrome Extensions">FAQ</a> | |
| 87 </li> | |
| 88 <li id="samples_link"> | |
| 89 <a href="samples.html" title="Sample extensions (with source code)">
Samples</a> | |
| 90 </li> | |
| 91 <li id="group_link"> | |
| 92 <a href="http://groups.google.com/a/chromium.org/group/chromium-exte
nsions" title="Google Chrome Extensions developer forum">Group</a> | |
| 93 </li> | |
| 94 <li id="so_link"> | |
| 95 <a href="http://stackoverflow.com/questions/tagged/google-chrome-ext
ension" title="[google-chrome-extension] tag on Stack Overflow">Questions?</a> | |
| 96 </li> | |
| 97 </ul> | |
| 98 </div> <!-- end gc-topnav --> | |
| 99 <div class="g-section g-tpl-170"> | |
| 100 <!-- SIDENAV --> | |
| 101 <div class="g-unit g-first" id="gc-toc"> | |
| 102 <ul> | |
| 103 <li><a href="getstarted.html">Getting Started</a></li> | |
| 104 <li><a href="overview.html">Overview</a></li> | |
| 105 <li><a href="whats_new.html">What's New?</a></li> | |
| 106 <li><h2><a href="devguide.html">Developer's Guide</a></h2> | |
| 107 <ul> | |
| 108 <li>Browser UI | |
| 109 <ul> | |
| 110 <li><a href="browserAction.html">Browser Actions</a></li> | |
| 111 <li><a href="contextMenus.html">Context Menus</a></li> | |
| 112 <li><a href="notifications.html">Desktop Notifications</a></li
> | |
| 113 <li><a href="omnibox.html">Omnibox</a></li> | |
| 114 <li><a href="options.html">Options Pages</a></li> | |
| 115 <li><a href="override.html">Override Pages</a></li> | |
| 116 <li><a href="pageAction.html">Page Actions</a></li> | |
| 117 </ul> | |
| 118 </li> | |
| 119 <li>Browser Interaction | |
| 120 <ul> | |
| 121 <li><a href="bookmarks.html">Bookmarks</a></li> | |
| 122 <li><a href="cookies.html">Cookies</a></li> | |
| 123 <li><a href="devtools.html">Developer Tools</a></li> | |
| 124 <li><a href="events.html">Events</a></li> | |
| 125 <li><a href="history.html">History</a></li> | |
| 126 <li><a href="management.html">Management</a></li> | |
| 127 <li><a href="tabs.html">Tabs</a></li> | |
| 128 <li><a href="windows.html">Windows</a></li> | |
| 129 </ul> | |
| 130 </li> | |
| 131 <li>Implementation | |
| 132 <ul> | |
| 133 <li><a href="a11y.html">Accessibility</a></li> | |
| 134 <li><a href="background_pages.html">Background Pages</a></li> | |
| 135 <li><a href="content_scripts.html">Content Scripts</a></li> | |
| 136 <li class="leftNavSelected">Cross-Origin XHR</li> | |
| 137 <li><a href="i18n.html">Internationalization</a></li> | |
| 138 <li><a href="messaging.html">Message Passing</a></li> | |
| 139 <li><a href="permissions.html">Optional Permissions</a></li> | |
| 140 <li><a href="npapi.html">NPAPI Plugins</a></li> | |
| 141 </ul> | |
| 142 </li> | |
| 143 <li>Finishing | |
| 144 <ul> | |
| 145 <li><a href="hosting.html">Hosting</a></li> | |
| 146 <li><a href="external_extensions.html">Other Deployment Option
s</a></li> | |
| 147 </ul> | |
| 148 </li> | |
| 149 </ul> | |
| 150 </li> | |
| 151 <li><h2><a href="apps.html">Packaged Apps</a></h2></li> | |
| 152 <li><h2><a href="tutorials.html">Tutorials</a></h2> | |
| 153 <ul> | |
| 154 <li><a href="tut_debugging.html">Debugging</a></li> | |
| 155 <li><a href="tut_analytics.html">Google Analytics</a></li> | |
| 156 <li><a href="tut_oauth.html">OAuth</a></li> | |
| 157 </ul> | |
| 158 </li> | |
| 159 <li><h2>Reference</h2> | |
| 160 <ul> | |
| 161 <li>Formats | |
| 162 <ul> | |
| 163 <li><a href="manifest.html">Manifest Files</a></li> | |
| 164 <li><a href="match_patterns.html">Match Patterns</a></li> | |
| 165 </ul> | |
| 166 </li> | |
| 167 <li><a href="permission_warnings.html">Permission Warnings</a></li
> | |
| 168 <li><a href="api_index.html">chrome.* APIs</a></li> | |
| 169 <li><a href="api_other.html">Other APIs</a></li> | |
| 170 </ul> | |
| 171 </li> | |
| 172 <li><h2><a href="samples.html">Samples</a></h2></li> | |
| 173 <div class="line"> </div> | |
| 174 <li><h2>More</h2> | |
| 175 <ul> | |
| 176 <li><a href="http://code.google.com/chrome/webstore/docs/index.htm
l">Chrome Web Store</a></li> | |
| 177 <li><a href="http://code.google.com/chrome/apps/docs/developers_gu
ide.html">Hosted Apps</a></li> | |
| 178 <li><a href="themes.html">Themes</a></li> | |
| 179 </ul> | |
| 180 </li> | |
| 181 </ul> | |
| 182 </div> | |
| 183 <script> | |
| 184 initToggles(); | |
| 185 </script> | |
| 186 <div class="g-unit" id="gc-pagecontent"> | |
| 187 <div id="pageTitle"> | |
| 188 <h1 class="page_title">Cross-Origin XMLHttpRequest</h1> | |
| 189 </div> | |
| 190 <!-- TABLE OF CONTENTS --> | |
| 191 <!-- /TABLE OF CONTENTS --> | |
| 192 <!-- Standard content lead-in for experimental API pages --> | |
| 193 <!-- STATIC CONTENT PLACEHOLDER --> | |
| 194 <div id="static"><div id="pageData-name" class="pageData">Cross-Origin X
MLHttpRequest</div> | |
| 195 <!-- BEGIN AUTHORED CONTENT --> | |
| 196 <p id="classSummary"> | |
| 197 Regular web pages can use the | |
| 198 <a href="http://www.w3.org/TR/XMLHttpRequest/">XMLHttpRequest</a> | |
| 199 object to send and receive data from remote servers, | |
| 200 but they're limited by the | |
| 201 <a href="http://en.wikipedia.org/wiki/Same_origin_policy">same origin policy</a>
. | |
| 202 Extensions aren't so limited. | |
| 203 An extension can talk to remote servers outside of its origin, | |
| 204 as long as it first requests cross-origin permissions.</p> | |
| 205 <p class="note"> | |
| 206 <b>Version note:</b> | |
| 207 As of Chrome 13, | |
| 208 content scripts can make cross-origin requests | |
| 209 to the same servers as the rest of the extension. | |
| 210 Before Chrome 13, a content script couldn't directly make requests; | |
| 211 instead, it had to | |
| 212 send a message to its parent extension | |
| 213 asking the extension to make a cross-origin request. | |
| 214 </p> | |
| 215 <h2 id="extension-origin">Extension origin</h2> | |
| 216 <p>Each running extension exists within its own separate security origin. Withou
t | |
| 217 requesting additional privileges, the extension can use | |
| 218 XMLHttpRequest to get resources within its installation. For example, if | |
| 219 an extension contains a JSON configuration file called <code>config.json</code>, | |
| 220 in a <code>config_resources</code> folder, the extension can retrieve the file's
contents like | |
| 221 this:</p> | |
| 222 <pre>var xhr = new XMLHttpRequest(); | |
| 223 xhr.onreadystatechange = handleStateChange; // Implemented elsewhere. | |
| 224 xhr.open("GET", chrome.extension.getURL('/config_resources/config.json'), true); | |
| 225 xhr.send(); | |
| 226 </pre> | |
| 227 <p>If the extension attempts to use a security origin other than itself, | |
| 228 say http://www.google.com, | |
| 229 the browser disallows it | |
| 230 unless the extension has requested the appropriate cross-origin permissions. | |
| 231 </p> | |
| 232 <h2 id="requesting-permission">Requesting cross-origin permissions</h2> | |
| 233 <p>By adding hosts or host match patterns (or both) to the | |
| 234 <a href="manifest.html#permissions">permissions</a> section of the | |
| 235 <a href="manifest.html">manifest</a> file, the extension can request access to | |
| 236 remote servers outside of its origin.</p> | |
| 237 <pre>{ | |
| 238 "name": "My extension", | |
| 239 ... | |
| 240 <b>"permissions": [ | |
| 241 "http://www.google.com/" | |
| 242 ]</b>, | |
| 243 ... | |
| 244 }</pre> | |
| 245 <p>Cross-origin permission values can be fully qualified host names, | |
| 246 like these:</p> | |
| 247 <ul> | |
| 248 <li> "http://www.google.com/" </li> | |
| 249 <li> "http://www.gmail.com/" </li> | |
| 250 </ul> | |
| 251 <p>Or they can be match patterns, like these:</p> | |
| 252 <ul> | |
| 253 <li> "http://*.google.com/" </li> | |
| 254 <li> "http://*/" </li> | |
| 255 </ul> | |
| 256 <p> | |
| 257 A match pattern of "http://*/" allows HTTP access to all reachable domains. | |
| 258 Note that here, | |
| 259 match patterns are similar to <a href="match_patterns.html">content script | |
| 260 match patterns</a>, | |
| 261 but any path information following the host is ignored.</p> | |
| 262 <p>Also note that access is granted both by host and by scheme. If an extension | |
| 263 wants both secure and non-secure HTTP access to a given host or set | |
| 264 of hosts, it must declare the permissions separately:</p> | |
| 265 <pre>"permissions": [ | |
| 266 "http://www.google.com/", | |
| 267 "https://www.google.com/" | |
| 268 ] | |
| 269 </pre> | |
| 270 <h2 id="security-considerations">Security considerations</h2> | |
| 271 <p> | |
| 272 When using resources retrieved via XMLHttpRequest, your background page should | |
| 273 be careful not to fall victim to <a href="http://en.wikipedia.org/wiki/Cross-sit
e_scripting">cross-site | |
| 274 scripting</a>. Specifically, avoid using dangerous APIs such as the below: | |
| 275 </p> | |
| 276 <pre>background.html | |
| 277 =============== | |
| 278 var xhr = new XMLHttpRequest(); | |
| 279 xhr.open("GET", "http://api.example.com/data.json", true); | |
| 280 xhr.onreadystatechange = function() { | |
| 281 if (xhr.readyState == 4) { | |
| 282 // WARNING! Might be evaluating an evil script! | |
| 283 var resp = eval("(" + xhr.responseText + ")"); | |
| 284 ... | |
| 285 } | |
| 286 } | |
| 287 xhr.send(); | |
| 288 background.html | |
| 289 =============== | |
| 290 var xhr = new XMLHttpRequest(); | |
| 291 xhr.open("GET", "http://api.example.com/data.json", true); | |
| 292 xhr.onreadystatechange = function() { | |
| 293 if (xhr.readyState == 4) { | |
| 294 // WARNING! Might be injecting a malicious script! | |
| 295 document.getElementById("resp").innerHTML = xhr.responseText; | |
| 296 ... | |
| 297 } | |
| 298 } | |
| 299 xhr.send(); | |
| 300 </pre> | |
| 301 <p> | |
| 302 Instead, prefer safer APIs that do not run scripts: | |
| 303 </p> | |
| 304 <pre>background.html | |
| 305 =============== | |
| 306 var xhr = new XMLHttpRequest(); | |
| 307 xhr.open("GET", "http://api.example.com/data.json", true); | |
| 308 xhr.onreadystatechange = function() { | |
| 309 if (xhr.readyState == 4) { | |
| 310 // JSON.parse does not evaluate the attacker's scripts. | |
| 311 var resp = JSON.parse(xhr.responseText); | |
| 312 } | |
| 313 } | |
| 314 xhr.send(); | |
| 315 background.html | |
| 316 =============== | |
| 317 var xhr = new XMLHttpRequest(); | |
| 318 xhr.open("GET", "http://api.example.com/data.json", true); | |
| 319 xhr.onreadystatechange = function() { | |
| 320 if (xhr.readyState == 4) { | |
| 321 // innerText does not let the attacker inject HTML elements. | |
| 322 document.getElementById("resp").innerText = xhr.responseText; | |
| 323 } | |
| 324 } | |
| 325 xhr.send(); | |
| 326 </pre> | |
| 327 <p> | |
| 328 Additionally, be especially careful of resources retrieved via HTTP. If your | |
| 329 extension is used on a hostile network, an network attacker (aka a <a href="http
://en.wikipedia.org/wiki/Man-in-the-middle_attack">"man-in-the-middle"</a>) | |
| 330 could modify the response and, potentially, attack your extension. Instead, | |
| 331 prefer HTTPS whenever possible. | |
| 332 </p> | |
| 333 <!-- END AUTHORED CONTENT --> | |
| 334 </div> | |
| 335 <!-- API PAGE --> | |
| 336 <!-- /apiPage --> | |
| 337 </div> <!-- /gc-pagecontent --> | |
| 338 </div> <!-- /g-section --> | |
| 339 </div> <!-- /codesiteContent --> | |
| 340 <div id="gc-footer" --=""> | |
| 341 <div class="text"> | |
| 342 <p> | |
| 343 Except as otherwise <a href="http://code.google.com/policies.html#restrictions
">noted</a>, | |
| 344 the content of this page is licensed under the <a rel="license" href="http://c
reativecommons.org/licenses/by/3.0/">Creative Commons | |
| 345 Attribution 3.0 License</a>, and code samples are licensed under the | |
| 346 <a rel="license" href="http://code.google.com/google_bsd_license.html">BSD Lic
ense</a>. | |
| 347 </p> | |
| 348 <p> | |
| 349 ©2011 Google | |
| 350 </p> | |
| 351 <!-- begin analytics --> | |
| 352 <script src="https://www.google-analytics.com/urchin.js" type="text/javascript">
</script> | |
| 353 <script src="https://www.google-analytics.com/ga.js" type="text/javascript"></sc
ript> | |
| 354 <script type="text/javascript"> | |
| 355 // chrome doc tracking | |
| 356 try { | |
| 357 var engdocs = _gat._getTracker("YT-10763712-2"); | |
| 358 engdocs._trackPageview(); | |
| 359 } catch(err) {} | |
| 360 // code.google.com site-wide tracking | |
| 361 try { | |
| 362 _uacct="UA-18071-1"; | |
| 363 _uanchor=1; | |
| 364 _uff=0; | |
| 365 urchinTracker(); | |
| 366 } | |
| 367 catch(e) {/* urchinTracker not available. */} | |
| 368 </script> | |
| 369 <!-- end analytics --> | |
| 370 </div> | |
| 371 </div> <!-- /gc-footer --> | |
| 372 </div> <!-- /gc-container --> | |
| 373 </body></html> | |
| OLD | NEW |