@@ -4,11 +4,11 @@ api: webRequest
4
4
5
5
## Manifest
6
6
7
- You must declare the "webRequest" permission in the [ extension manifest] [ manifest-json ] to use the
8
- web request API, along with the necessary [ host permissions] [ declare-perms ] . To intercept a
9
- sub-resource request, the extension needs to have access to both the requested URL and its
10
- initiator. If you want to use the web request API in a blocking fashion, you need to request the
11
- "webRequestBlocking" permission in addition. For example:
7
+ You must declare the "webRequest" permission in the [ extension manifest] [ 1 ] to use the web request
8
+ API, along with the necessary [ host permissions] [ 2 ] . To intercept a sub-resource request, the
9
+ extension needs to have access to both the requested URL and its initiator. If you want to use the
10
+ web request API in a blocking fashion, you need to request the "webRequestBlocking" permission in
11
+ addition. For example:
12
12
13
13
``` json
14
14
{
@@ -34,40 +34,37 @@ The event life cycle for successful requests is illustrated here, followed by ev
34
34
35
35
` onBeforeRequest ` (optionally synchronous)
36
36
37
- : Fires when a request is about to occur. This event is sent before any TCP connection is made and
38
- can be used to cancel or redirect requests.
37
+ : Fires when a request is about to occur. This event is sent before any TCP connection is made and can
38
+ be used to cancel or redirect requests.
39
39
40
40
` onBeforeSendHeaders ` (optionally synchronous)
41
41
42
42
: Fires when a request is about to occur and the initial headers have been prepared. The event is
43
- intended to allow extensions to add, modify, and delete request headers
44
- [ (\* )] [ footnote-lifecycle ] . The ` onBeforeSendHeaders ` event is passed to all subscribers, so
45
- different subscribers may attempt to modify the request; see the [ Implementation
46
- details] [ impl-details ] section for how this is handled. This event can be used to cancel the
47
- request.
43
+ intended to allow extensions to add, modify, and delete request headers [ (\* )] [ 3 ] . The
44
+ ` onBeforeSendHeaders ` event is passed to all subscribers, so different subscribers may attempt to
45
+ modify the request; see the [ Implementation details] [ 4 ] section for how this is handled. This event
46
+ can be used to cancel the request.
48
47
49
48
` onSendHeaders `
50
49
51
50
: Fires after all extensions have had a chance to modify the request headers, and presents the final
52
- [ (\* )] [ footnote-lifecycle ] version. The event is triggered before the headers are sent to the
53
- network. This event is informational and handled asynchronously. It does not allow modifying or
54
- cancelling the request.
51
+ [ (\* )] [ 5 ] version. The event is triggered before the headers are sent to the network. This event is
52
+ informational and handled asynchronously. It does not allow modifying or cancelling the request.
55
53
56
54
` onHeadersReceived ` (optionally synchronous)
57
55
58
56
: Fires each time that an HTTP(S) response header is received. Due to redirects and authentication
59
57
requests this can happen multiple times per request. This event is intended to allow extensions to
60
58
add, modify, and delete response headers, such as incoming Content-Type headers. The caching
61
- directives are processed before this event is triggered, so modifying headers such as
62
- ` Cache-Control ` has no influence on the browser's cache. It also allows you to cancel or redirect
63
- the request.
59
+ directives are processed before this event is triggered, so modifying headers such as Cache-Control
60
+ has no influence on the browser's cache. It also allows you to cancel or redirect the request.
64
61
65
62
` onAuthRequired ` (optionally synchronous)
66
63
67
- : Fires when a request requires authentication of the user. This event can be handled synchronously
68
- to provide authentication credentials. Note that extensions may provide invalid credentials. Take
69
- care not to enter an infinite loop by repeatedly providing invalid credentials. This can also be
70
- used to cancel the request.
64
+ : Fires when a request requires authentication of the user. This event can be handled synchronously to
65
+ provide authentication credentials. Note that extensions may provide invalid credentials. Take care
66
+ not to enter an infinite loop by repeatedly providing invalid credentials. This can also be used to
67
+ cancel the request.
71
68
72
69
` onBeforeRedirect `
73
70
@@ -93,7 +90,7 @@ The web request API guarantees that for each request either `onCompleted` or `on
93
90
fired as the final event with one exception: If a request is redirected to a ` data:// ` URL,
94
91
` onBeforeRedirect ` is the last reported event.
95
92
96
- <a id =" life-cycle-footnote " >* </a >
93
+ <a id =" #life_cycle_footnote " >* </a >
97
94
Note that the web request API presents an abstraction of the network stack to the extension.
98
95
Internally, one URL request can be split into several HTTP requests (for example to fetch individual
99
96
byte ranges from a large file) or can be handled by the network stack without communicating with the
@@ -138,50 +135,32 @@ removed without specifying `'extraHeaders'` in `opt_extraInfoSpec`:
138
135
{% Aside %}
139
136
140
137
** Note:** Modifying the ` Origin ` request header might not work as intended and may result in
141
- unexpected errors in the response's [ CORS checks] [ cors-checks ] . This is because while extensions can
142
- only modify the [ Origin] [ origin-header ] request header, they can't change the ` request origin ` or
143
- initiator, which is a concept defined in the Fetch spec to represent who initiates the request. In
144
- such a scenario, the server may allow the CORS access for the modified request and put the header's
145
- ` Origin ` into the ` Access-Control-Allow-Origin ` header in the response. But it won't match the
146
- immutable ` request origin ` and result in a CORS failure.
138
+ unexpected errors in the response's [ CORS checks] [ 6 ] . This is because while extensions can only
139
+ modify the [ Origin] [ 7 ] request header, they can't change the ` request origin ` or initiator, which is
140
+ a concept defined in the Fetch spec to represent who initiates the request. In such a scenario, the
141
+ server may allow the CORS access for the modified request and put the header's ` Origin ` into the
142
+ ` Access-Control-Allow-Origin ` header in the response. But it won't match the immutable
143
+ ` request origin ` and result in a CORS failure.
147
144
148
145
{% endAside %}
149
146
150
147
Starting from Chrome 72, if you need to modify responses before [ Cross Origin Read Blocking
151
- (CORB)] [ corb ] can block the response, you need to specify ` 'extraHeaders' ` in ` opt_extraInfpSpec ` .
148
+ (CORB)] [ 8 ] can block the response, you need to specify ` 'extraHeaders' ` in ` opt_extraInfpSpec ` .
152
149
153
150
Starting from Chrome 72, the following request headers are ** not provided** and cannot be modified
154
151
or removed without specifying ` 'extraHeaders' ` in ` opt_extraInfoSpec ` :
155
152
156
- - ` Accept-Language `
157
- - ` Accept-Encoding `
158
- - ` Referer `
159
- - ` Cookie `
153
+ - Accept-Language
154
+ - Accept-Encoding
155
+ - Referer
156
+ - Cookie
160
157
161
158
Starting from Chrome 72, the ` Set-Cookie ` response header is ** not provided** and cannot be modified
162
159
or removed without specifying ` 'extraHeaders' ` in ` opt_extraInfoSpec ` .
163
160
164
- Starting from Chrome 84 , the ` Accept-CH ` response header is ** not provided ** and cannot be modified
161
+ Starting from Chrome 89 , the ` X-Frame-Options ` response header cannot be effectively modified
165
162
or removed without specifying ` 'extraHeaders' ` in ` opt_extraInfoSpec ` .
166
163
167
- Starting from Chrome 86, the ` Accept-CH-Lifetime ` response header is ** not provided** and cannot be
168
- modified or removed without specifying ` 'extraHeaders' ` in ` opt_extraInfoSpec ` .
169
-
170
- Starting from Chrome 88, the ` Critical-CH ` response header is ** not provided** and cannot be
171
- modified or removed without specifying ` 'extraHeaders' ` in ` opt_extraInfoSpec ` .
172
-
173
- Starting from Chrome 89, the ` Origin-Agent-Cluster ` request header and ` X-Frame-Options ` response
174
- header are ** not provided** and cannot be effectively modified or removed without specifying
175
- ` 'extraHeaders' ` in ` opt_extraInfoSpec ` .
176
-
177
- Starting from Chrome 91, the ` Content-Security-Policy ` , ` Content-Security-Policy-Report-Only ` , and
178
- ` Link ` response headers are ** not provided** and cannot be modified or removed without specifying
179
- ` 'extraHeaders' ` in ` opt_extraInfoSpec ` .
180
-
181
- Starting from Chrome 92, the ` Timing-Allow-Origin ` and ` BFCache-Opt-In ` response headers are ** not
182
- provided** and cannot be modified or removed without specifying ` 'extraHeaders' ` in
183
- ` opt_extraInfoSpec ` .
184
-
185
164
{% Aside %}
186
165
187
166
** Note:** Specifying ` 'extraHeaders' ` in ` opt_extraInfoSpec ` may have a negative impact on
@@ -190,16 +169,16 @@ performance, hence it should only be used when really necessary.
190
169
{% endAside %}
191
170
192
171
The webRequest API only exposes requests that the extension has permission to see, given its [ host
193
- permissions] [ declare-perms ] . Moreover, only the following schemes are accessible: ` http:// ` ,
194
- ` https:// ` , ` ftp:// ` , ` file:// ` , ` ws:// ` (since Chrome 58), ` wss:// ` (since Chrome 58), ` urn: `
195
- (since Chrome 91), or ` chrome-extension:// ` . In addition, even certain requests with URLs using one
196
- of the above schemes are hidden. These include ` chrome-extension://other_extension_id ` where
197
- ` other_extension_id ` is not the ID of the extension to handle the request,
198
- ` https://www.google.com/chrome ` , and other sensitive requests core to browser functionality. Also
199
- synchronous XMLHttpRequests from your extension are hidden from blocking event handlers in order to
200
- prevent deadlocks. Note that for some of the supported schemes the set of available events might be
201
- limited due to the nature of the corresponding protocol. For example, for the file: scheme, only
202
- ` onBeforeRequest ` , ` onResponseStarted ` , ` onCompleted ` , and ` onErrorOccurred ` may be dispatched.
172
+ permissions] [ 9 ] . Moreover, only the following schemes are accessible: ` http:// ` , ` https ://` ,
173
+ ` ftp:// ` , ` file:// ` , ` ws:// ` (since Chrome 58), ` wss:// ` (since Chrome 58), ` urn: ` (since Chrome 91), or
174
+ ` chrome-extension:// ` . In addition, even certain requests with URLs using one of the above schemes
175
+ are hidden. These include ` chrome-extension://other_extension_id ` where ` other_extension_id ` is not
176
+ the ID of the extension to handle the request, ` https://www.google.com/chrome ` , and other sensitive
177
+ requests core to browser functionality. Also synchronous XMLHttpRequests from your extension are
178
+ hidden from blocking event handlers in order to prevent deadlocks. Note that for some of the
179
+ supported schemes the set of available events might be limited due to the nature of the
180
+ corresponding protocol. For example, for the file: scheme, only ` onBeforeRequest ` ,
181
+ ` onResponseStarted ` , ` onCompleted ` , and ` onErrorOccurred ` may be dispatched.
203
182
204
183
Starting from Chrome 58, the webRequest API supports intercepting the WebSocket handshake request.
205
184
Since the handshake is done by means of an HTTP upgrade request, its flow fits into HTTP-oriented
@@ -228,8 +207,8 @@ in case of HTTP redirection or HTTP authentication.
228
207
### Registering event listeners
229
208
230
209
To register an event listener for a web request, you use a variation on the [ usual ` addListener() `
231
- function] [ api-events ] . In addition to specifying a callback function, you have to specify a filter
232
- argument and you may specify an optional extra info argument.
210
+ function] [ 10 ] . In addition to specifying a callback function, you have to specify a filter argument
211
+ and you may specify an optional extra info argument.
233
212
234
213
The three arguments to the web request API's ` addListener() ` have the following definitions:
235
214
@@ -254,28 +233,27 @@ information in this dictionary depends on the specific event type as well as the
254
233
If the optional ` opt_extraInfoSpec ` array contains the string ` 'blocking' ` (only allowed for
255
234
specific events), the callback function is handled synchronously. That means that the request is
256
235
blocked until the callback function returns. In this case, the callback can return a
257
- [ ` webRequest.BlockingResponse ` ] [ blocking-response ] that determines the further life cycle of the
258
- request. Depending on the context, this response allows cancelling or redirecting a request
259
- (` onBeforeRequest ` ), cancelling a request or modifying headers (` onBeforeSendHeaders ` ,
260
- ` onHeadersReceived ` ), and cancelling a request or providing authentication credentials
261
- (` onAuthRequired ` ).
236
+ [ ` webRequest.BlockingResponse ` ] [ 11 ] that determines the further life cycle of the request. Depending
237
+ on the context, this response allows cancelling or redirecting a request (` onBeforeRequest ` ),
238
+ cancelling a request or modifying headers (` onBeforeSendHeaders ` , ` onHeadersReceived ` ), and
239
+ cancelling a request or providing authentication credentials (` onAuthRequired ` ).
262
240
263
241
If the optional ` opt_extraInfoSpec ` array contains the string ` 'asyncBlocking' ` instead (only
264
- allowed for ` onAuthRequired ` ), the extension can generate the
265
- [ ` webRequest.BlockingResponse ` ] [ blocking-response ] asynchronously.
242
+ allowed for ` onAuthRequired ` ), the extension can generate the [ ` webRequest.BlockingResponse ` ] [ 12 ]
243
+ asynchronously.
266
244
267
- The [ ` webRequest.RequestFilter ` ] [ request-filter ] ` filter ` allows limiting the requests for which
268
- events are triggered in various dimensions:
245
+ The [ ` webRequest.RequestFilter ` ] [ 13 ] ` filter ` allows limiting the requests for which events are
246
+ triggered in various dimensions:
269
247
270
248
URLs
271
249
272
- : [ URL patterns] [ match-patterns ] such as ` *://www.google.com/foo*bar ` .
250
+ : [ URL patterns] [ 14 ] such as ` *://www.google.com/foo*bar ` .
273
251
274
252
Types
275
253
276
- : Request types such as ` main_frame ` (a document that is loaded for a top-level frame), ` sub_frame `
277
- (a document that is loaded for an embedded frame), and ` image ` (an image on a web site). See
278
- [ ` webRequest.RequestFilter ` ] [ request-filter ] .
254
+ : Request types such as ` main_frame ` (a document that is loaded for a top-level frame), ` sub_frame ` (a
255
+ document that is loaded for an embedded frame), and ` image ` (an image on a web site). See
256
+ [ ` webRequest.RequestFilter ` ] [ 15 ] .
279
257
280
258
Tab ID
281
259
@@ -289,7 +267,7 @@ Depending on the event type, you can specify strings in `opt_extraInfoSpec` to a
289
267
information about the request. This is used to provide detailed information on request's data only
290
268
if explicitly requested.
291
269
292
- ## Implementation details {: #implementation-details }
270
+ ## Implementation details
293
271
294
272
Several implementation details can be important to understand when developing an extension that uses
295
273
the web request API:
@@ -374,14 +352,22 @@ chrome.webRequest.onBeforeSendHeaders.addListener(
374
352
);
375
353
```
376
354
377
- [ api-events ] : /docs/extensions/events
378
- [ manifest-json ] : /docs/extensions/mv3/manifest/
379
- [ blocking-response ] : #type-BlockingResponse
380
- [ corb ] : https://chromium.googlesource.com/chromium/src/+/master/services/network/cross_origin_read_blocking_explainer.md
381
- [ cors-checks ] : https://fetch.spec.whatwg.org/#cors-check
382
- [ declare-perms ] : /docs/extensions/mv3/declare_permissions
383
- [ footnote-lifecycle ] : #life-cycle-footnote
384
- [ impl-details ] : #implementation-details
385
- [ match-patterns ] : /docs/extensions/mv3/match_patterns
386
- [ origin-header ] : https://fetch.spec.whatwg.org/#origin-header
387
- [ request-filter ] : #type-RequestFilter
355
+ For more example code, see the [ web request samples] [ 16 ] .
356
+
357
+ [ 1 ] : /docs/extensions/mv2/tabs
358
+ [ 2 ] : /docs/extensions/mv2/declare_permissions
359
+ [ 3 ] : #life_cycle_footnote
360
+ [ 4 ] : #implementation-details
361
+ [ 5 ] : #life_cycle_footnote
362
+ [ 6 ] : https://fetch.spec.whatwg.org/#cors-check
363
+ [ 7 ] : https://fetch.spec.whatwg.org/#origin-header
364
+ [ 8] :
365
+ https://chromium.googlesource.com/chromium/src/+/master/services/network/cross_origin_read_blocking_explainer.md
366
+ [ 9 ] : /docs/extensions/mv2/declare_permissions
367
+ [ 10 ] : /docs/extensions/events
368
+ [ 11 ] : #type-BlockingResponse
369
+ [ 12 ] : #type-BlockingResponse
370
+ [ 13 ] : #type-RequestFilter
371
+ [ 14 ] : /docs/extensions/mv2/match_patterns
372
+ [ 15 ] : #type-RequestFilter
373
+ [ 16 ] : /docs/extensions/mv2/samples#search:webrequest
0 commit comments