OLD | NEW |
1 <!-- BEGIN AUTHORED CONTENT --> | 1 |
| 2 |
| 3 |
2 <p id="classSummary"> | 4 <p id="classSummary"> |
3 Use the <code>chrome.webRequest</code> module to intercept, block, | 5 Use the <code>chrome.webRequest</code> module to intercept, block, |
4 or modify requests in-flight and to observe and analyze traffic. | 6 or modify requests in-flight and to observe and analyze traffic. |
5 </p> | 7 </p> |
| 8 |
6 <h2 id="manifest">Manifest</h2> | 9 <h2 id="manifest">Manifest</h2> |
7 <p>You must declare the "webRequest" permission in the <a | 10 <p>You must declare the "webRequest" permission in the <a |
8 href="manifest.html">extension manifest</a> to use the web request | 11 href="manifest.html">extension manifest</a> to use the web request |
9 API, along with <a href="manifest.html#permissions">host permissions</a> | 12 API, along with <a href="manifest.html#permissions">host permissions</a> |
10 for any hosts whose network requests you want to access. If you want to | 13 for any hosts whose network requests you want to access. If you want to |
11 use the web request API in a blocking fashion, you need to request | 14 use the web request API in a blocking fashion, you need to request |
12 the "webRequestBlocking" permission in addition. | 15 the "webRequestBlocking" permission in addition. |
13 For example:</p> | 16 For example:</p> |
14 <pre>{ | 17 <pre>{ |
15 "name": "My extension", | 18 "name": "My extension", |
16 ... | 19 ... |
17 <b>"permissions": [ | 20 <b>"permissions": [ |
18 "webRequest", | 21 "webRequest", |
19 "*://*.google.com" | 22 "*://*.google.com" |
20 ]</b>, | 23 ]</b>, |
21 ... | 24 ... |
22 }</pre> | 25 }</pre> |
| 26 |
23 <p class="note"> | 27 <p class="note"> |
24 <b>Node:</b> If you request the "webRequestBlocking" permission, web requests | 28 <b>Node:</b> If you request the "webRequestBlocking" permission, web requests |
25 are delayed until the background page of your extension has been loaded. This | 29 are delayed until the background page of your extension has been loaded. This |
26 allows you to register event listeners before any web requests are processed. | 30 allows you to register event listeners before any web requests are processed. |
27 In order to avoid deadlocks, you must not start synchronous XmlHttpRequests or | 31 In order to avoid deadlocks, you must not start synchronous XmlHttpRequests or |
28 include scripts from the internet via <code><script src="..."></code> tags | 32 include scripts from the internet via <code><script src="..."></code> tags |
29 in your background page. | 33 in your background page. |
30 </p> | 34 </p> |
| 35 |
31 <h2 id="life_cycle">Life cycle of requests</h2> | 36 <h2 id="life_cycle">Life cycle of requests</h2> |
| 37 |
32 <p> | 38 <p> |
33 The web request API defines a set of events that follow the life cycle of a web | 39 The web request API defines a set of events that follow the life cycle of a web |
34 request. You can use these events to observe and analyze traffic. Certain | 40 request. You can use these events to observe and analyze traffic. Certain |
35 synchronous events will allow you to intercept, block, or modify a request. | 41 synchronous events will allow you to intercept, block, or modify a request. |
36 </p> | 42 </p> |
| 43 |
37 <p> | 44 <p> |
38 The event life cycle for successful requests is illustrated here, followed by | 45 The event life cycle for successful requests is illustrated here, followed by |
39 event definitions:<br/> | 46 event definitions:<br/> |
40 <img src="{{static}}/images/webrequestapi.png" | 47 <img src="{{static}}/images/webrequestapi.png" |
41 width="385" height="503" | 48 width="385" height="503" |
42 alt="Life cycle of a web request from the perspective of the webrequest API" | 49 alt="Life cycle of a web request from the perspective of the webrequest API" |
43 style="margin-left: auto; margin-right: auto; display: block"/> | 50 style="margin-left: auto; margin-right: auto; display: block"/> |
44 </p> | 51 </p> |
| 52 |
45 <p> | 53 <p> |
46 <dl> | 54 <dl> |
47 <dt><code>onBeforeRequest</code> (optionally synchronous)</dt> | 55 <dt><code>onBeforeRequest</code> (optionally synchronous)</dt> |
48 <dd>Fires when a request is about to occur. This event is sent before any TCP | 56 <dd>Fires when a request is about to occur. This event is sent before any TCP |
49 connection is made and can be used to cancel or redirect requests.</dd> | 57 connection is made and can be used to cancel or redirect requests.</dd> |
50 <dt><code>onBeforeSendHeaders</code> (optionally synchronous)</dt> | 58 <dt><code>onBeforeSendHeaders</code> (optionally synchronous)</dt> |
51 <dd>Fires when a request is about to occur and the initial headers have been | 59 <dd>Fires when a request is about to occur and the initial headers have been |
52 prepared. The event is intended to allow extensions to add, modify, and delete | 60 prepared. The event is intended to allow extensions to add, modify, and delete |
53 request headers <a href="#life_cycle_footnote">(*)</a>. The | 61 request headers <a href="#life_cycle_footnote">(*)</a>. The |
54 <code>onBeforeSendHeaders</code> event is passed to all subscribers, so | 62 <code>onBeforeSendHeaders</code> event is passed to all subscribers, so |
(...skipping 29 matching lines...) Expand all Loading... |
84 <dt><code>onCompleted</code></dt> | 92 <dt><code>onCompleted</code></dt> |
85 <dd>Fires when a request has been processed successfully.</dd> | 93 <dd>Fires when a request has been processed successfully.</dd> |
86 <dt><code>onErrorOccurred</code></dt> | 94 <dt><code>onErrorOccurred</code></dt> |
87 <dd>Fires when a request could not be processed successfully.</dd> | 95 <dd>Fires when a request could not be processed successfully.</dd> |
88 </dl> | 96 </dl> |
89 The web request API guarantees that for each request either | 97 The web request API guarantees that for each request either |
90 <code>onCompleted</code> or <code>onErrorOccurred</code> is fired as the final | 98 <code>onCompleted</code> or <code>onErrorOccurred</code> is fired as the final |
91 event with one exception: If a request is redirected to a <code>data://</code> | 99 event with one exception: If a request is redirected to a <code>data://</code> |
92 URL, <code>onBeforeRedirect</code> is the last reported event. | 100 URL, <code>onBeforeRedirect</code> is the last reported event. |
93 </p> | 101 </p> |
| 102 |
94 <p id="life_cycle_footnote">(*) Note that the web request API presents an | 103 <p id="life_cycle_footnote">(*) Note that the web request API presents an |
95 abstraction of the network stack to the extension. Internally, one URL request | 104 abstraction of the network stack to the extension. Internally, one URL request |
96 can be split into several HTTP requests (for example to fetch individual byte | 105 can be split into several HTTP requests (for example to fetch individual byte |
97 ranges from a large file) or can be handled by the network stack without | 106 ranges from a large file) or can be handled by the network stack without |
98 communicating with the network. For this reason, the API does not provide the | 107 communicating with the network. For this reason, the API does not provide the |
99 final HTTP headers that are sent to the network. For example, all headers that | 108 final HTTP headers that are sent to the network. For example, all headers that |
100 are related to caching are invisible to the extension.</p> | 109 are related to caching are invisible to the extension.</p> |
| 110 |
101 <p>The following headers are currently <b>not provided</b> to the | 111 <p>The following headers are currently <b>not provided</b> to the |
102 <code>onBeforeSendHeaders</code> event. This list is not guaranteed to be | 112 <code>onBeforeSendHeaders</code> event. This list is not guaranteed to be |
103 complete nor stable. | 113 complete nor stable. |
104 <ul> | 114 <ul> |
105 <li>Authorization</li> | 115 <li>Authorization</li> |
106 <li>Cache-Control</li> | 116 <li>Cache-Control</li> |
107 <li>Connection</li> | 117 <li>Connection</li> |
108 <li>Content-Length</li> | 118 <li>Content-Length</li> |
109 <li>Host</li> | 119 <li>Host</li> |
110 <li>If-Modified-Since</li> | 120 <li>If-Modified-Since</li> |
111 <li>If-None-Match</li> | 121 <li>If-None-Match</li> |
112 <li>If-Range</li> | 122 <li>If-Range</li> |
113 <li>Partial-Data</li> | 123 <li>Partial-Data</li> |
114 <li>Pragma</li> | 124 <li>Pragma</li> |
115 <li>Proxy-Authorization</li> | 125 <li>Proxy-Authorization</li> |
116 <li>Proxy-Connection</li> | 126 <li>Proxy-Connection</li> |
117 <li>Transfer-Encoding</li> | 127 <li>Transfer-Encoding</li> |
118 </ul> | 128 </ul> |
119 </p> | 129 </p> |
| 130 |
120 <p> | 131 <p> |
121 The webRequest API only exposes requests that the extension has | 132 The webRequest API only exposes requests that the extension has |
122 permission to see, given its | 133 permission to see, given its |
123 <a href="manifest.html#permissions">host permissions</a>. | 134 <a href="manifest.html#permissions">host permissions</a>. |
124 Moreover, only the following schemes are accessible: | 135 Moreover, only the following schemes are accessible: |
125 <code>http://</code>, | 136 <code>http://</code>, |
126 <code>https://</code>, | 137 <code>https://</code>, |
127 <code>ftp://</code>, | 138 <code>ftp://</code>, |
128 <code>file://</code>, or | 139 <code>file://</code>, or |
129 <code>chrome-extension://</code>. | 140 <code>chrome-extension://</code>. |
130 In addition, even certain requests with URLs using one of the above schemes | 141 In addition, even certain requests with URLs using one of the above schemes |
131 are hidden, e.g., | 142 are hidden, e.g., |
132 <code>chrome-extension://other_extension_id</code> where | 143 <code>chrome-extension://other_extension_id</code> where |
133 <code>other_extension_id</code> is not the ID of the extension to handle | 144 <code>other_extension_id</code> is not the ID of the extension to handle |
134 the request, | 145 the request, |
135 <code>https://www.google.com/chrome</code>, | 146 <code>https://www.google.com/chrome</code>, |
136 and others (this list is not complete). | 147 and others (this list is not complete). |
137 </p> | 148 </p> |
| 149 |
138 <h2 id="concepts">Concepts</h2> | 150 <h2 id="concepts">Concepts</h2> |
| 151 |
139 <p>As the following sections explain, events in the web request API use request | 152 <p>As the following sections explain, events in the web request API use request |
140 IDs, and you can optionally specify filters and extra information when you | 153 IDs, and you can optionally specify filters and extra information when you |
141 register event listeners.</p> | 154 register event listeners.</p> |
| 155 |
142 <h3 id="Request IDs">Request IDs</h3> | 156 <h3 id="Request IDs">Request IDs</h3> |
| 157 |
143 <p>Each request is identified by a request ID. This ID is unique within a | 158 <p>Each request is identified by a request ID. This ID is unique within a |
144 browser session and the context of an extension. It remains constant during the | 159 browser session and the context of an extension. It remains constant during the |
145 the life cycle of a request and can be used to match events for the same | 160 the life cycle of a request and can be used to match events for the same |
146 request. Note that several HTTP requests are mapped to one web request in case | 161 request. Note that several HTTP requests are mapped to one web request in case |
147 of HTTP redirection or HTTP authentication.</p> | 162 of HTTP redirection or HTTP authentication.</p> |
| 163 |
148 <h3 id="subscription">Registering event listeners</h3> | 164 <h3 id="subscription">Registering event listeners</h3> |
| 165 |
149 <p>To register an event listener for a web request, you use a variation on the | 166 <p>To register an event listener for a web request, you use a variation on the |
150 <a href="events.html">usual <code>addListener()</code> function</a>. | 167 <a href="events.html">usual <code>addListener()</code> function</a>. |
151 In addition to specifying a callback function, | 168 In addition to specifying a callback function, |
152 you have to specify a filter argument and you may specify an optional extra info | 169 you have to specify a filter argument and you may specify an optional extra info |
153 argument.</p> | 170 argument.</p> |
| 171 |
154 <p>The three arguments to the web request API's <code>addListener()</code> have | 172 <p>The three arguments to the web request API's <code>addListener()</code> have |
155 the following definitions:</p> | 173 the following definitions:</p> |
156 <pre> | 174 <pre> |
157 var callback = function(details) {...}; | 175 var callback = function(details) {...}; |
158 var filter = {...}; | 176 var filter = {...}; |
159 var opt_extraInfoSpec = [...]; | 177 var opt_extraInfoSpec = [...]; |
160 </pre> | 178 </pre> |
| 179 |
161 <p>Here's an example of listening for the <code>onBeforeRequest</code> | 180 <p>Here's an example of listening for the <code>onBeforeRequest</code> |
162 event:</p> | 181 event:</p> |
163 <pre> | 182 <pre> |
164 chrome.webRequest.onBeforeRequest.addListener( | 183 chrome.webRequest.onBeforeRequest.addListener( |
165 callback, filter, opt_extraInfoSpec); | 184 callback, filter, opt_extraInfoSpec); |
166 </pre> | 185 </pre> |
| 186 |
167 <p>Each <code>addListener()</code> call takes a mandatory callback function as | 187 <p>Each <code>addListener()</code> call takes a mandatory callback function as |
168 the first parameter. This callback function is passed a dictionary containing | 188 the first parameter. This callback function is passed a dictionary containing |
169 information about the current URL request. The information in this dictionary | 189 information about the current URL request. The information in this dictionary |
170 depends on the specific event type as well as the content of | 190 depends on the specific event type as well as the content of |
171 <code>opt_extraInfoSpec</code>.</p> | 191 <code>opt_extraInfoSpec</code>.</p> |
| 192 |
172 <p>If the optional <code>opt_extraInfoSpec</code> array contains the string | 193 <p>If the optional <code>opt_extraInfoSpec</code> array contains the string |
173 <code>'blocking'</code> (only allowed for specific events), the callback | 194 <code>'blocking'</code> (only allowed for specific events), the callback |
174 function is handled synchronously. That means that the request is blocked until | 195 function is handled synchronously. That means that the request is blocked until |
175 the callback function returns. In this case, the callback can return a <a | 196 the callback function returns. In this case, the callback can return a <a |
176 href="#type-webRequest.BlockingResponse">BlockingResponse</a> that determines
the further | 197 href="#type-webRequest.BlockingResponse">BlockingResponse</a> that determines
the further |
177 life cycle of the request. Depending on the context, this response allows | 198 life cycle of the request. Depending on the context, this response allows |
178 cancelling or redirecting a request (<code>onBeforeRequest</code>), cancelling a | 199 cancelling or redirecting a request (<code>onBeforeRequest</code>), cancelling a |
179 request or modifying headers (<code>onBeforeSendHeaders</code>, | 200 request or modifying headers (<code>onBeforeSendHeaders</code>, |
180 <code>onHeadersReceived</code>), or providing authentication credentials | 201 <code>onHeadersReceived</code>), or providing authentication credentials |
181 (<code>onAuthRequired</code>).</p> | 202 (<code>onAuthRequired</code>).</p> |
| 203 |
182 <p>The <a href="#type-webRequest.RequestFilter">RequestFilter</a> | 204 <p>The <a href="#type-webRequest.RequestFilter">RequestFilter</a> |
183 <code>filter</code> allows limiting the requests for which events are | 205 <code>filter</code> allows limiting the requests for which events are |
184 triggered in various dimensions: | 206 triggered in various dimensions: |
185 <dl> | 207 <dl> |
186 <dt>URLs</dt> | 208 <dt>URLs</dt> |
187 <dd><a href="match_patterns.html">URL patterns</a> such as | 209 <dd><a href="match_patterns.html">URL patterns</a> such as |
188 <code>*://www.google.com/foo*bar</code>.</dd> | 210 <code>*://www.google.com/foo*bar</code>.</dd> |
189 <dt>Types</dt> | 211 <dt>Types</dt> |
190 <dd>Request types such as <code>main_frame</code> (a document that is loaded | 212 <dd>Request types such as <code>main_frame</code> (a document that is loaded |
191 for a top-level frame), <code>sub_frame</code> (a document that is loaded for | 213 for a top-level frame), <code>sub_frame</code> (a document that is loaded for |
192 an embedded frame), and <code>image</code> (an image on a web site). | 214 an embedded frame), and <code>image</code> (an image on a web site). |
193 See <a href="#type-webRequest.RequestFilter">RequestFilter</a>.</dd> | 215 See <a href="#type-webRequest.RequestFilter">RequestFilter</a>.</dd> |
194 <dt>Tab ID</dt> | 216 <dt>Tab ID</dt> |
195 <dd>The identifier for one tab.</dd> | 217 <dd>The identifier for one tab.</dd> |
196 <dt>Window ID</dt> | 218 <dt>Window ID</dt> |
197 <dd>The identifier for a window.</dd> | 219 <dd>The identifier for a window.</dd> |
198 </p> | 220 </p> |
| 221 |
199 <p>Depending on the event type, you can specify strings in | 222 <p>Depending on the event type, you can specify strings in |
200 <code>opt_extraInfoSpec</code> to ask for additional information about the | 223 <code>opt_extraInfoSpec</code> to ask for additional information about the |
201 request. This is used to provide detailed information on request's data only | 224 request. This is used to provide detailed information on request's data only |
202 if explicitly requested.</p> | 225 if explicitly requested.</p> |
| 226 |
203 <h2 id="implementation">Implementation details</h2> | 227 <h2 id="implementation">Implementation details</h2> |
| 228 |
204 <p>Several implementation details can be important to understand when developing | 229 <p>Several implementation details can be important to understand when developing |
205 an extension that uses the web request API:</p> | 230 an extension that uses the web request API:</p> |
| 231 |
206 <h3>Conflict resolution</h3> | 232 <h3>Conflict resolution</h3> |
207 <p>In the current implementation of the web request API, a request is considered | 233 <p>In the current implementation of the web request API, a request is considered |
208 as cancelled if at least one extension instructs to cancel the request. If | 234 as cancelled if at least one extension instructs to cancel the request. If |
209 an extension cancels a request, all extensions are notified by an | 235 an extension cancels a request, all extensions are notified by an |
210 <code>onErrorOccurred</code> event. Only one extension is allowed to redirect a | 236 <code>onErrorOccurred</code> event. Only one extension is allowed to redirect a |
211 request or modify a header at a time. If more than one extension attempts to | 237 request or modify a header at a time. If more than one extension attempts to |
212 modify the request, the most recently installed extension wins and all others | 238 modify the request, the most recently installed extension wins and all others |
213 are ignored. An extension is not notified if its instruction to modify or | 239 are ignored. An extension is not notified if its instruction to modify or |
214 redirect has been ignored.</p> | 240 redirect has been ignored.</p> |
| 241 |
215 <h3>Caching</h3> | 242 <h3>Caching</h3> |
216 <p> | 243 <p> |
217 Chrome employs two caches — an on-disk cache and a very fast in-memory | 244 Chrome employs two caches — an on-disk cache and a very fast in-memory |
218 cache. The lifetime of an in-memory cache is attached to the lifetime of a | 245 cache. The lifetime of an in-memory cache is attached to the lifetime of a |
219 render process, which roughly corresponds to a tab. Requests that are answered | 246 render process, which roughly corresponds to a tab. Requests that are answered |
220 from the in-memory cache are invisible to the web request API. If a request | 247 from the in-memory cache are invisible to the web request API. If a request |
221 handler changes its behavior (for example, the behavior according to which | 248 handler changes its behavior (for example, the behavior according to which |
222 requests are blocked), a simple page refresh might not respect this changed | 249 requests are blocked), a simple page refresh might not respect this changed |
223 behavior. To make sure the behavior change goes through, call | 250 behavior. To make sure the behavior change goes through, call |
224 <code>handlerBehaviorChanged()</code> to flush the in-memory cache. But don't do | 251 <code>handlerBehaviorChanged()</code> to flush the in-memory cache. But don't do |
225 it often; flushing the cache is a very expensive operation. You don't need to | 252 it often; flushing the cache is a very expensive operation. You don't need to |
226 call <code>handlerBehaviorChanged()</code> after registering or unregistering an | 253 call <code>handlerBehaviorChanged()</code> after registering or unregistering an |
227 event listener.</p> | 254 event listener.</p> |
| 255 |
228 <h3>Timestamps</h3> | 256 <h3>Timestamps</h3> |
229 <p> | 257 <p> |
230 The <code>timestamp</code> property of web request events is only guaranteed to | 258 The <code>timestamp</code> property of web request events is only guaranteed to |
231 be <i>internally</i> consistent. Comparing one event to another event will give | 259 be <i>internally</i> consistent. Comparing one event to another event will give |
232 you the correct offset between them, but comparing them to the current time | 260 you the correct offset between them, but comparing them to the current time |
233 inside the extension (via <code>(new Date()).getTime()</code>, for instance) | 261 inside the extension (via <code>(new Date()).getTime()</code>, for instance) |
234 might give unexpected results. | 262 might give unexpected results. |
235 </p> | 263 </p> |
| 264 |
236 <h2 id="examples">Examples</h2> | 265 <h2 id="examples">Examples</h2> |
| 266 |
237 <p>The following example illustrates how to block all requests to | 267 <p>The following example illustrates how to block all requests to |
238 <code>www.evil.com</code>:</p> | 268 <code>www.evil.com</code>:</p> |
239 <pre> | 269 <pre> |
240 chrome.webRequest.onBeforeRequest.addListener( | 270 chrome.webRequest.onBeforeRequest.addListener( |
241 function(details) { | 271 function(details) { |
242 return {cancel: details.url.indexOf("://www.evil.com/") != -1}; | 272 return {cancel: details.url.indexOf("://www.evil.com/") != -1}; |
243 }, | 273 }, |
244 {urls: ["<all_urls>"]}, | 274 {urls: ["<all_urls>"]}, |
245 ["blocking"]); | 275 ["blocking"]); |
246 </pre> | 276 </pre> |
| 277 |
247 <p>As this function uses a blocking event handler, it requires the "webRequest" | 278 <p>As this function uses a blocking event handler, it requires the "webRequest" |
248 as well as the "webRequestBlocking" permission in the manifest file.</p> | 279 as well as the "webRequestBlocking" permission in the manifest file.</p> |
| 280 |
249 <p>The following example achieves the same goal in a more efficient way because | 281 <p>The following example achieves the same goal in a more efficient way because |
250 requests that are not targeted to <code>www.evil.com</code> do not need to be | 282 requests that are not targeted to <code>www.evil.com</code> do not need to be |
251 passed to the extension:</p> | 283 passed to the extension:</p> |
252 <pre> | 284 <pre> |
253 chrome.webRequest.onBeforeRequest.addListener( | 285 chrome.webRequest.onBeforeRequest.addListener( |
254 function(details) { return {cancel: true}; }, | 286 function(details) { return {cancel: true}; }, |
255 {urls: ["*://www.evil.com/*"]}, | 287 {urls: ["*://www.evil.com/*"]}, |
256 ["blocking"]); | 288 ["blocking"]); |
257 </pre> | 289 </pre> |
| 290 |
258 <p>The following example illustrates how to delete the User-Agent header from | 291 <p>The following example illustrates how to delete the User-Agent header from |
259 all requests:</p> | 292 all requests:</p> |
260 <pre> | 293 <pre> |
261 chrome.webRequest.onBeforeSendHeaders.addListener( | 294 chrome.webRequest.onBeforeSendHeaders.addListener( |
262 function(details) { | 295 function(details) { |
263 for (var i = 0; i < details.requestHeaders.length; ++i) { | 296 for (var i = 0; i < details.requestHeaders.length; ++i) { |
264 if (details.requestHeaders[i].name === 'User-Agent') { | 297 if (details.requestHeaders[i].name === 'User-Agent') { |
265 details.requestHeaders.splice(i, 1); | 298 details.requestHeaders.splice(i, 1); |
266 break; | 299 break; |
267 } | 300 } |
268 } | 301 } |
269 return {requestHeaders: details.requestHeaders}; | 302 return {requestHeaders: details.requestHeaders}; |
270 }, | 303 }, |
271 {urls: ["<all_urls>"]}, | 304 {urls: ["<all_urls>"]}, |
272 ["blocking", "requestHeaders"]); | 305 ["blocking", "requestHeaders"]); |
273 </pre> | 306 </pre> |
| 307 |
274 <p> For more example code, see the <a href="samples.html#webrequest">web request | 308 <p> For more example code, see the <a href="samples.html#webrequest">web request |
275 samples</a>.</p> | 309 samples</a>.</p> |
276 <!-- END AUTHORED CONTENT --> | 310 |
OLD | NEW |