This API enables extensions to obtain information about and modify declarative rules that block or modify network requests. The use of declarative rules means that extensions don't intercept and view the content of requests, providing more privacy.
This API enables extensions to obtain information about and modify declarative rules that block or modify network requests. The use of declarative rules means that extensions don't intercept and view the content of requests, providing more privacy.
To use this API, an extension must request the "declarativeNetRequest" or "declarativeNetRequestWithHostAccess" permissions in its manifest.json file.
The "declarativeNetRequest" permission allows extensions to block and upgrade requests without any host permissions. Host permissions are required if the extension wants to redirect a request or modify headers on a request. The "declarativeNetRequestWithHostAccess" permission requires host permissions to the request URL and initiator to act on a request.
The "declarativeNetRequestFeedback" permission is required to use getMatchedRules and onRuleMatchedDebug as they return information on declarative rules matched. See Testing for more information.
The declarative rules are defined by four fields:
id – An ID that uniquely identifies a rule within a ruleset. Mandatory and should be >= 1.priority – The rule priority. When specified, it should be >= 1. Defaults to 1. See Matching precedents for details on how priority affects which rules are applied.condition – The condition under which this rule is triggered.action – The action to take when the rule is matched. Rules can do one of these things: This is an example rule that blocks all script requests originating from "foo.com" to any URL with "abc" as a substring:
{ "id" : 1, "priority": 1, "action" : { "type" : "block" }, "condition" : { "urlFilter" : "abc", "domains" : ["foo.com"], "resourceTypes" : ["script"] } }
The urlFilter field of a rule condition is used to specify the pattern matched against the request URL. See RuleCondition for details. Some examples of URL filters are:
urlFilter | Matches | Does not match |
|---|---|---|
"abc" | https://abcd.com https://example.com/abcd | https://ab.com |
"abc*d" | https://abcd.com https://example.com/abcxyzd | https://abc.com |
"||a.example.com" | https://a.example.com/ https://b.a.example.com/xyz | https://example.com/ |
"|https*" | https://example.com | http://example.com/ http://https.com |
Rules are organized into rulesets:
"declarative_net_request" manifest key and stored in the extension. An extension can enable and disable static rulesets using updateEnabledRulesets. The set of enabled static rulesets is persisted across sessions but not across extension updates. The static rulesets enabled on extension installation and update are determined by the content of the "declarative_net_request" manifest key.updateDynamicRules. These rules persist across sessions and extension updates.updateSessionRules. These rules do not persist across browser sessions.Note: Errors and warnings about invalid static rules are only displayed during testing. Invalid static rules in permanently installed extensions are ignored. Therefore, it's important to verify that your static rulesets are valid by testing.
An extension can:
"declarative_net_request" manifest key up to the value of MAX_NUMBER_OF_STATIC_RULESETS.GUARANTEED_MINIMUM_STATIC_RULES, and the number of enabled static rulesets must not exceed the value of [MAX_NUMBER_OF_ENABLED_STATIC_RULESETS. In addition, the number of rules in enabled static rulesets for all extensions must not exceed the global limit. Extensions shouldn't depend on the global limit having a specific value and should instead use getAvailableStaticRuleCount to find the number of additional rules they can enable.The number of dynamic and session-scoped rules an extension can add is limited to the value of MAX_NUMBER_OF_DYNAMIC_AND_SESSION_RULES.
When the browser evaluates how to handle requests, it checks each extension's rules that have a condition that matches the request and chooses the one to consider applying as follows:
If only one extension provides a rule for the request, that rule is applied. However, where more than one extension has a matching rule, the browser chooses the one to apply in this order of precedence:
If the request was not blocked or redirected, the matching modifyHeaders actions are applied, as documented in declarativeNetRequest.ModifyHeaderInfo.
testMatchOutcome, getmatchedrules, and onRuleMatchedDebug are available to assist with testing rules and rulesets. These APIs require the "declarativeNetRequestFeedback" permissions. In addition:
extensions.dnr.feedback preference to true. Set this preference using about:config or the --pref flag of the web-ext CLI tool.declarativeNetRequest permission.declarativeNetRequest.MatchedRuleDetails of a matched rule.
declarativeNetRequest.ModifyHeaderInfoThe request or response headers to modify for the request.
declarativeNetRequest.RedirectDetails of how the redirect should be performed. Only valid for redirect rules.
declarativeNetRequest.ResourceTypeThe resource type of a request.
declarativeNetRequest.RuleAn object containing details of a rule.
declarativeNetRequest.RuleActionAn object defining the action to take if a rule is matched.
declarativeNetRequest.RuleConditionAn object defining the condition under which a rule is triggered.
declarativeNetRequest.URLTransformAn object containing details of a URL transformation to perform for a redirect action.
declarativeNetRequest.DYNAMIC_RULESET_IDRuleset ID for the dynamic rules added by the extension.
declarativeNetRequest.GETMATCHEDRULES_QUOTA_INTERVALThe time interval within which declarativeNetRequest.MAX_GETMATCHEDRULES_CALLS_PER_INTERVAL declarativeNetRequest.getMatchedRules calls can be made.
declarativeNetRequest.GUARANTEED_MINIMUM_STATIC_RULESThe minimum number of static rules guaranteed to an extension across its enabled static rulesets.
declarativeNetRequest.MAX_GETMATCHEDRULES_CALLS_PER_INTERVALThe number of times declarativeNetRequest.getMatchedRules can be called within a period of declarativeNetRequest.GETMATCHEDRULES_QUOTA_INTERVAL.
declarativeNetRequest.MAX_NUMBER_OF_DYNAMIC_AND_SESSION_RULESThe maximum number of combined dynamic and session scoped rules an extension can add.
declarativeNetRequest.MAX_NUMBER_OF_ENABLED_STATIC_RULESETSThe maximum number of static rulesets an extension can enable.
declarativeNetRequest.MAX_NUMBER_OF_REGEX_RULESThe maximum number of regular expression rules that an extension can add.
declarativeNetRequest.MAX_NUMBER_OF_STATIC_RULESETSThe maximum number of static rulesets an extension can specify as part of the declarative_net_request.rule_resources manifest key.
declarativeNetRequest.SESSION_RULESET_IDThe ruleset ID for the session-scoped rules added by the extension.
declarativeNetRequest.getAvailableStaticRuleCount()Returns the number of static rules an extension can enable before the global static rule limit is reached.
declarativeNetRequest.getDynamicRules()Returns the set of dynamic rules for the extension.
declarativeNetRequest.getEnabledRulesets()Returns the IDs for the set of enabled static rulesets.
declarativeNetRequest.getMatchedRules()Returns all the rules matched for the extension.
declarativeNetRequest.getSessionRules()Returns the set of session scoped rules for the extension.
declarativeNetRequest.isRegexSupported()Checks if a regular expression is supported as a declarativeNetRequest.RuleCondition.regexFilter rule condition.
declarativeNetRequest.setExtensionActionOptions()Configures how the action count for tabs are handled.
declarativeNetRequest.testMatchOutcome()Checks if any of the extension's declarativeNetRequest rules would match a hypothetical request.
declarativeNetRequest.updateDynamicRules()Modifies the active set of dynamic rules for the extension.
declarativeNetRequest.updateEnabledRulesets()Updates the set of active static rulesets for the extension.
declarativeNetRequest.updateSessionRules()Modifies the set of session scoped rules for the extension.
declarativeNetRequest.onRuleMatchedDebugFired when a rule is matched with a request when debugging an extension with the "declarativeNetRequestFeedback" permission.
| Desktop | Mobile | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | |
DYNAMIC_RULESET_ID |
84 | 84 | No | ? | 70 | No | ? | ? | No | ? | No | ? |
GETMATCHEDRULES_QUOTA_INTERVAL |
84 | 84 | No | ? | 70 | No | ? | ? | No | ? | No | ? |
GUARANTEED_MINIMUM_STATIC_RULES |
89 | 89 | No | ? | 75 | No | ? | ? | No | ? | No | ? |
MAX_GETMATCHEDRULES_CALLS_PER_INTERVAL |
84 | 84 | No | ? | 70 | No | ? | ? | No | ? | No | ? |
MAX_NUMBER_OF_DYNAMIC_AND_SESSION_RULES |
90 | 90 | No | ? | 76 | 16.4 | ? | ? | No | ? | 16.4 | ? |
MAX_NUMBER_OF_ENABLED_STATIC_RULESETS |
94 | 94 | No | ? | 80 | preview | ? | ? | No | ? | No | ? |
MAX_NUMBER_OF_REGEX_RULES |
84 | 84 | No | ? | 70 | No | ? | ? | No | ? | No | ? |
MAX_NUMBER_OF_STATIC_RULESETS |
84 | 84 | No | ? | 70 | 15 | ? | ? | No | ? | 15 | ? |
MatchedRule |
84 | 84 | No | ? | 70 | No | ? | ? | No | ? | No | ? |
Redirect |
84 | 84 | No | ? | 70 | 15.4 | ? | ? | No | ? | 15.4 | ? |
ResourceType |
84 | 84 | No | ? | 70 | 15 | ? | ? | No | ? | 15 | ? |
Rule |
84 | 84 | No | ? | 70 | 15 | ? | ? | No | ? | 15 | ? |
RuleAction |
84 | 84 | No | ? | 70 | 15 | ? | ? | No | ? | 15 | ? |
RuleCondition |
84 | 84 | No | ? | 70 | 15 | ? | ? | No | ? | 15 | ? |
SESSION_RULESET_ID |
90 | 90 | No | ? | 76 | No | ? | ? | No | ? | No | ? |
URLTransform |
84 | 84 | No | ? | 70 | 15.4 | ? | ? | No | ? | 15.4 | ? |
getAvailableStaticRuleCount |
89 | 89 | No | ? | 75 | No | ? | ? | No | ? | No | ? |
getDynamicRules |
84 | 84 | No | ? | 70 | 15.4 | ? | ? | No | ? | 15.4 | ? |
getEnabledRulesets |
84 | 84 | No | ? | 70 | 15 | ? | ? | No | ? | 15 | ? |
getMatchedRules |
84 | 84 | No | ? | 70 | 15.4 | ? | ? | No | ? | 15.4 | ? |
getSessionRules |
90 | 90 | No | ? | 76 | 15.4 | ? | ? | No | ? | 15.4 | ? |
isRegexSupported |
87 | 87 | No | ? | 73 | 15 | ? | ? | No | ? | 15 | ? |
onRuleMatchedDebug |
84Available only to unpacked extensions. |
84Available only to unpacked extensions. |
No | ? | 70Available only to unpacked extensions. |
No | ? | ? | No | ? | No | ? |
setExtensionActionOptions |
88 | 88 | No | ? | 74 | 16.4 | ? | ? | No | ? | 16.4 | ? |
testMatchOutcome |
103 | 103 | No | ? | 89 | No | ? | ? | No | ? | No | ? |
updateDynamicRules |
84 | 84 | No | ? | 70 | 15.4 | ? | ? | No | ? | 15.4 | ? |
updateEnabledRulesets |
84 | 84 | No | ? | 70 | 15 | ? | ? | No | ? | 15 | ? |
updateSessionRules |
90 | 90 | No | ? | 76 | 15.4 | ? | ? | No | ? | 15.4 | ? |
© 2005–2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/declarativeNetRequest