@@ -4,11 +4,11 @@ api: webRequest
4
4
5
5
## Manifest
6
6
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:
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:
12
12
13
13
``` json
14
14
{
@@ -34,37 +34,40 @@ 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 can
38
- 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
38
+ can 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 [ (\* )] [ 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.
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.
47
48
48
49
` onSendHeaders `
49
50
50
51
: Fires after all extensions have had a chance to modify the request headers, and presents the final
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.
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.
53
55
54
56
` onHeadersReceived ` (optionally synchronous)
55
57
56
58
: Fires each time that an HTTP(S) response header is received. Due to redirects and authentication
57
59
requests this can happen multiple times per request. This event is intended to allow extensions to
58
60
add, modify, and delete response headers, such as incoming Content-Type headers. The caching
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.
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.
61
64
62
65
` onAuthRequired ` (optionally synchronous)
63
66
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.
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.
68
71
69
72
` onBeforeRedirect `
70
73
@@ -90,7 +93,7 @@ The web request API guarantees that for each request either `onCompleted` or `on
90
93
fired as the final event with one exception: If a request is redirected to a ` data:// ` URL,
91
94
` onBeforeRedirect ` is the last reported event.
92
95
93
- <a id =" #life_cycle_footnote " >* </a >
96
+ <a id =" life-cycle-footnote " >* </a >
94
97
Note that the web request API presents an abstraction of the network stack to the extension.
95
98
Internally, one URL request can be split into several HTTP requests (for example to fetch individual
96
99
byte ranges from a large file) or can be handled by the network stack without communicating with the
@@ -135,32 +138,50 @@ removed without specifying `'extraHeaders'` in `opt_extraInfoSpec`:
135
138
{% Aside %}
136
139
137
140
** Note:** Modifying the ` Origin ` request header might not work as intended and may result in
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.
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.
144
147
145
148
{% endAside %}
146
149
147
150
Starting from Chrome 72, if you need to modify responses before [ Cross Origin Read Blocking
148
- (CORB)] [ 8 ] can block the response, you need to specify ` 'extraHeaders' ` in ` opt_extraInfpSpec ` .
151
+ (CORB)] [ corb ] can block the response, you need to specify ` 'extraHeaders' ` in ` opt_extraInfpSpec ` .
149
152
150
153
Starting from Chrome 72, the following request headers are ** not provided** and cannot be modified
151
154
or removed without specifying ` 'extraHeaders' ` in ` opt_extraInfoSpec ` :
152
155
153
- - Accept-Language
154
- - Accept-Encoding
155
- - Referer
156
- - Cookie
156
+ - ` Accept-Language `
157
+ - ` Accept-Encoding `
158
+ - ` Referer `
159
+ - ` Cookie `
157
160
158
161
Starting from Chrome 72, the ` Set-Cookie ` response header is ** not provided** and cannot be modified
159
162
or removed without specifying ` 'extraHeaders' ` in ` opt_extraInfoSpec ` .
160
163
161
- Starting from Chrome 89 , the ` X-Frame-Options ` response header cannot be effectively modified
164
+ Starting from Chrome 84 , the ` Accept-CH ` response header is ** not provided ** and cannot be modified
162
165
or removed without specifying ` 'extraHeaders' ` in ` opt_extraInfoSpec ` .
163
166
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
+
164
185
{% Aside %}
165
186
166
187
** Note:** Specifying ` 'extraHeaders' ` in ` opt_extraInfoSpec ` may have a negative impact on
@@ -169,16 +190,16 @@ performance, hence it should only be used when really necessary.
169
190
{% endAside %}
170
191
171
192
The webRequest API only exposes requests that the extension has permission to see, given its [ host
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.
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.
182
203
183
204
Starting from Chrome 58, the webRequest API supports intercepting the WebSocket handshake request.
184
205
Since the handshake is done by means of an HTTP upgrade request, its flow fits into HTTP-oriented
@@ -207,8 +228,8 @@ in case of HTTP redirection or HTTP authentication.
207
228
### Registering event listeners
208
229
209
230
To register an event listener for a web request, you use a variation on the [ usual ` addListener() `
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.
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.
212
233
213
234
The three arguments to the web request API's ` addListener() ` have the following definitions:
214
235
@@ -233,27 +254,28 @@ information in this dictionary depends on the specific event type as well as the
233
254
If the optional ` opt_extraInfoSpec ` array contains the string ` 'blocking' ` (only allowed for
234
255
specific events), the callback function is handled synchronously. That means that the request is
235
256
blocked until the callback function returns. In this case, the callback can return a
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 ` ).
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 ` ).
240
262
241
263
If the optional ` opt_extraInfoSpec ` array contains the string ` 'asyncBlocking' ` instead (only
242
- allowed for ` onAuthRequired ` ), the extension can generate the [ ` webRequest.BlockingResponse ` ] [ 12 ]
243
- asynchronously.
264
+ allowed for ` onAuthRequired ` ), the extension can generate the
265
+ [ ` webRequest.BlockingResponse ` ] [ blocking-response ] asynchronously.
244
266
245
- The [ ` webRequest.RequestFilter ` ] [ 13 ] ` filter ` allows limiting the requests for which events are
246
- triggered in various dimensions:
267
+ The [ ` webRequest.RequestFilter ` ] [ request-filter ] ` filter ` allows limiting the requests for which
268
+ events are triggered in various dimensions:
247
269
248
270
URLs
249
271
250
- : [ URL patterns] [ 14 ] such as ` *://www.google.com/foo*bar ` .
272
+ : [ URL patterns] [ match-patterns ] such as ` *://www.google.com/foo*bar ` .
251
273
252
274
Types
253
275
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 ] .
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 ] .
257
279
258
280
Tab ID
259
281
@@ -267,7 +289,7 @@ Depending on the event type, you can specify strings in `opt_extraInfoSpec` to a
267
289
information about the request. This is used to provide detailed information on request's data only
268
290
if explicitly requested.
269
291
270
- ## Implementation details
292
+ ## Implementation details {: #implementation-details }
271
293
272
294
Several implementation details can be important to understand when developing an extension that uses
273
295
the web request API:
@@ -352,22 +374,14 @@ chrome.webRequest.onBeforeSendHeaders.addListener(
352
374
);
353
375
```
354
376
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
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
0 commit comments