You are viewing extension docs in chrome via the 'file:' scheme: are you expecting to see local changes when you refresh? You'll need run chrome with --allow-file-access-from-files.
WARNING: This is the BETA documentation. It may not work with the stable release of Chrome.
WARNING: This is unofficial documentation. It may not work with the current release of Chrome.

Google Chrome Extensions (Labs)

chrome.declarativeWebRequest

Warning: This API is still under development. It is only available for Chrome users on the dev early release channel. Learn more.

Notes

Use the chrome.declarativeWebRequest module to intercept, block, or modify requests in-flight. It is significantly faster than the chrome.webRequest API because you can register rules that are evaluated in the browser rather than the JavaScript engine which reduces roundtrip latencies and allows for very high efficiency.

Manifest

You must declare the "declarative" and the "declarativeWebRequest" permission in the extension manifest to use this API, along with host permissions for any hosts whose network requests you want to access.

{
  "name": "My extension",
  ...
  "permissions": [
    "declarative",
    "declarativeWebRequest",
    "*://*.google.com"
  ],
  ...
}

Rules

The Declarative Web Request API follows the concepts of the Declarative API. You can register rules to the chrome.declarativeWebRequest.onRequest event object.

The Declarative Web Request API supports a single type of match criteria, the RequestMatcher. The RequestMatcher matches network requests if and only if all listed criteria are met. The following RequestMatcher would match a network request when the user enters "http://www.example.com" in the URL bar:

var matcher = new chrome.declarativeWebRequest.RequestMatcher({
  url: { hostSuffix: 'example.com', schemes: ['http'] },
  resourceType: 'main_frame'
  });

Requests to "https://www.example.com" would be rejected by the RequestMatcher due to the scheme. Also all requests for an embedded iframe would be rejected due to the resourceType.

Note: All conditions and actions are created via a constructor as shown in the example above.

In order to cancel all requests to "example.com", you can define a rule as follows:

var rule = {
  conditions: [
    new chrome.declarativeWebRequest.RequestMatcher({
      url: { hostSuffix: 'example.com' } })
  ],
  actions: [
    new chrome.declarativeWebRequest.CancelRequest()
  ]};

In order to cancel all requests to "example.com" and "foobar.com", you can add a second condition, as each condition is sufficient to trigger all specified actions:

var rule2 = {
  conditions: [
    new chrome.declarativeWebRequest.RequestMatcher({
      url: { hostSuffix: 'example.com' } }),
    new chrome.declarativeWebRequest.RequestMatcher({
      url: { hostSuffix: 'foobar.com' } })
  ],
  actions: [
    new chrome.declarativeWebRequest.CancelRequest()
  ]};

Register rules as follows:

chrome.declarativeWebRequest.onRequest.addRules([rule2]);

Note: You should always register or unregister rules in bulk rather than individually because each of these operations recreates internal data structures. This re-creation is computationally expensive but facilitates a very fast URL matching algorithm for hundreds of thousands of URLs.

Todo

  • Explain precedences, once we can ignore rules based on their priority (e.g. how can I cancel all requests except for a specific whitelist?)
  • Explain when conditions can be evaluated, when actions can be executed, and when rules can be executed (e.g. you cannot cancel a request when you have received the response already)

API reference: chrome.declarativeWebRequest

Types

declarativeWebRequest.RedirectByRegEx

( object )
Redirects a request by applying a regular expression on the URL. The regular expressions use the RE2 syntax.
from
( string )
A match pattern that may contain capture groups. Capture groups are referenced in the Perl syntax ($1, $2, ...) instead of the RE2 syntax (\1, \2, ...) in order to be closer to JavaScript Regular Expressions.
to
( string )
Destination pattern.

RequestMatcher

( object )
Matches network events by various criteria.
url
( optional events.UrlFilter )
Matches if the condition of the UrlFilter are fulfilled for the URL of the request.
resourceType
( optional array of string ["main_frame", "sub_frame", "stylesheet", "script", "image", "object", "xmlhttprequest", "other"] )
Matches if the request type of a request is contained in the list. Requests that cannot match any of the types will be filtered out.
instanceType
( enumerated string ["declarativeWebRequest.RequestMatcher"] )
Undocumented.

RedirectRequest

( object )
Declarative event action that redirects a network request.
instanceType
( enumerated string ["declarativeWebRequest.RedirectRequest"] )
Undocumented.
redirectUrl
( string )
Destination to where the request is redirected.

declarativeWebRequest.RedirectToTransparentImage

( object )
Declarative event action that redirects a network request to a transparent image.

declarativeWebRequest.RedirectToEmptyDocument

( object )
Declarative event action that redirects a network request to an empty document.

CancelRequest

( object )
Declarative event action that cancels a network request.
instanceType
( enumerated string ["declarativeWebRequest.CancelRequest"] )
Undocumented.

declarativeWebRequest.SetRequestHeader

( object )
Sets the request header of the specified name to the specified value. If a header with the specified name did not exist before, a new one is created. Header name comparison is always case-insensitive. Each request header name occurs only once in each request.
name
( string )
HTTP request header name.
value
( string )
HTTP request header value.

declarativeWebRequest.RemoveRequestHeader

( object )
Removes the request header of the specified name. Do not use SetRequestHeader and RemoveRequestHeader with the same header name on the same request. Each request header name occurs only once in each request.
name
( string )
HTTP request header name (case-insensitive).

declarativeWebRequest.AddResponseHeader

( object )
Adds the response header to the response of this web request. As multiple response headers may share the same name, you need to first remove and then add a new response header in order to replace one.
name
( string )
HTTP response header name.
value
( string )
HTTP response header value.

declarativeWebRequest.RemoveResponseHeader

( object )
Removes all response headers of the specified names and values.
name
( string )
HTTP request header name (case-insensitive).
value
( optional string )
HTTP request header value (case-insensitive).

declarativeWebRequest.IgnoreRules

( object )
Masks all rules that match the specified criteria.
lowerPriorityThan
( integer )
If set, rules with a lower priority than the specified value are ignored. This boundary is not persited, it affects only rules and their actions of the same network request stage. TODO(battre): Explain network request stages.