Enable/disable Safari web extension content blocker using declarative_net_request in manifest

Is it possible to enable/disable the enabled flag before the extension is loaded? we want to have a button in our app which controls the availability of the content blocker ruleSet in declarative_net_request in manifest (version 2).

"declarative_net_request": { "rule_resources": [ { "id": "ruleset_1", "enabled": true, "path": "ruleset_1.json" } ] },

Answered by Frameworks Engineer in 804610022

It sounds like the error might be due to exceeding the rule limit because the previous dynamic rules haven’t been removed before adding new ones. To address this, ensure that you’re clearing out the old dynamic rules first. You can do this by calling browser.declarativeNetRequest.getDynamicRules() to inspect the current rules and then removing any unneeded ones with updateDynamicRules().

If you’re looking to toggle the enabled state of a static rule set directly from the manifest, you can use browser.declarativeNetRequest.updateEnabledRulesets(). This will allow you to enable or disable static rule sets without dealing with the limits imposed on dynamic rules.

Since you mentioned working with thousands of rules, using static rules might be a more stable approach if the rule set won’t change dynamically too often. This way, you can toggle their availability via a button, ensuring you remain within the limits.

We have more than 22,000 rules in the ruleset_1 json file.

We are getting the below error with 22342 rule sets. We are trying this for Safari iOS. As per the documentation, we see that dynamic rules have a limit of 30,000 - https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/declarativeNetRequest/MAX_NUMBER_OF_DYNAMIC_AND_SESSION_RULES

Accepted Answer

It sounds like the error might be due to exceeding the rule limit because the previous dynamic rules haven’t been removed before adding new ones. To address this, ensure that you’re clearing out the old dynamic rules first. You can do this by calling browser.declarativeNetRequest.getDynamicRules() to inspect the current rules and then removing any unneeded ones with updateDynamicRules().

If you’re looking to toggle the enabled state of a static rule set directly from the manifest, you can use browser.declarativeNetRequest.updateEnabledRulesets(). This will allow you to enable or disable static rule sets without dealing with the limits imposed on dynamic rules.

Since you mentioned working with thousands of rules, using static rules might be a more stable approach if the rule set won’t change dynamically too often. This way, you can toggle their availability via a button, ensuring you remain within the limits.

Enable/disable Safari web extension content blocker using declarative_net_request in manifest
 
 
Q