Skip to main content
Block ads and trackers with the declarativeNetRequest API. You declare matching rules, and the browser blocks requests natively before they leave the network stack.

What you build

Why MV3 replaced blocking webRequest

Manifest V2 ad blockers registered blocking webRequest listeners. Every request paused while extension JavaScript decided its fate. Manifest V3 removed that model for performance and privacy reasons. With declarativeNetRequest, the browser evaluates your rules itself. Your code never sits on the request path and never reads request contents to block them. For proxy-style use cases, such as routing all traffic through another server, use the proxy API instead of request rules.

Manifest

Block and allow rules need no host permissions. Redirect rules and header rules need host permissions for the affected sites.
manifest.json
Extension.js compiles the ruleset file that rule_resources references and emits it with the build output.

Static rules

Static rules live in rules.json and load when the extension loads.
rules.json
The ||domain^ filter syntax matches a domain and all of its subdomains.

Dynamic rules

Use dynamic rules for filters that change at runtime, such as user-added blocklist entries. Remove a rule id before you re-add it, so updates stay idempotent.
background.js
Chrome caps static and dynamic rule counts. Check the current limits in the declarativeNetRequest reference before you ship large filter lists.

Count blocked requests on the badge

One call turns the action badge into a per-tab counter of matched rules.
background.js
To read matched rules yourself with getMatchedRules, add the declarativeNetRequestFeedback permission.

Run it

Before you edit manifest.json, know that manifest changes need a dev-server restart. See Dev update behavior.
Open a page that requests a blocked domain. The badge count rises as rules match.

Firefox notes

  • Firefox supports declarativeNetRequest in Manifest V3.
  • Firefox does not support setExtensionActionOptions, so the badge counter is Chromium-only.
  • Firefox still allows blocking webRequest in Manifest V3, which Chrome reserves for policy installs.
  • Use browser-specific manifest fields when the two targets diverge.

Start from a template

The action template ships a background script plus a toolbar popup, a good base for a blocker UI.
Repository: extension-js/examples/action

Best practices

  • Keep static rules in rules.json and reserve dynamic rules for user choices.
  • Give every dynamic rule a stable id, so removals stay predictable.
  • Scope resourceTypes to what you block, not to every type.
  • Prefer block and allow rules, which need no host permissions.
  • Test rules against real pages before you publish filter updates.

Next steps