What's new
  • Do not create multi-accounts, you will be blocked! For more information about rules, limits, and more, visit the Help Page Found a dead link? Use the report button!
  • If no files are available, please contact me or reply in the thread. I will update them immediately.

Chrome Extension: declarativeNetRequest regex does not work

The issue with the declarativeNetRequest regex not working in your Chrome extension is often caused by the following common pitfalls:

1. Regex Must Follow Google's Format

The regex used in declarativeNetRequest must comply with Google's supported syntax. For example:

Google's regex rules use RE2, which has limitations compared to full-featured regex engines like PCRE.

Lookaheads ((?=...)) and lookbehinds ((?<=...)) are not supported.

Certain special constructs, like backreferences (\1), are also unsupported.


2. Testing Your Regex

If you're unsure, test your regex in an environment that supports RE2 (e.g., regex101.com, set to RE2 mode).

Ensure the regex is escaped properly if it contains special characters.


3. Correct Configuration

Double-check the declarativeNetRequest configuration in the manifest.json. For example:

JavaScript:
{

  "name": "Example Extension",

  "version": "1.0",

  "manifest_version": 3,

  "permissions": ["declarativeNetRequest"],

  "host_permissions": ["<all_urls>"],

  "declarative_net_request": {

    "rule_resources": [

      {

        "id": "ruleset_1",

        "enabled": true,

        "path": "rules.json"

      }

    ]

  }

}

In the rules.json, make sure your regexFilter is correctly specified:

JavaScript:
[

  {

    "id": 1,

    "priority": 1,

    "action": { "type": "block" },

    "condition": {

      "regexFilter": "^https?:\\/\\/example\\.com\\/.*",

      "resourceTypes": ["main_frame"]

    }

  }

]

Here:

regexFilter is the regex pattern.

Ensure special characters like \ are properly escaped.


4. Error Debugging

Use the Extensions page in Chrome (chrome://extensions) to load your unpacked extension and inspect errors in the console or background.js.

Check for invalid regex errors.

Use chrome.declarativeNetRequest.getDynamicRules to inspect applied rules dynamically.


5. Ensure Rule Limits Are Met

Chrome limits the number of rules you can add:

Dynamic rules: 50,000.

Static rules: 150,000. If you exceed these, some rules may not apply.


6. Common Debugging Tips

Make sure the URL matches the pattern. Double-check if the regex is case-sensitive or not.

If the issue persists, simplify your regex to confirm whether the problem lies with the pattern or something else.
 
Back
Top