Brilliant
Staff member
- Joined
- Dec 31, 2024
- Messages
- 373
- Reaction score
- 7
- Points
- 18
- User icon
- <svg xmlns="http://www.w3.org/2000/svg" height="14" width="15.75" viewBox="0 0 576 512"><!--!Font Awesome Free 6.7.2 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free Copyright 2025 Fonticons, Inc.--><path fill="#63E6BE" d="M309 106c11.4-7 19-19.7 19-34c0-22.1-17.9-40-40-40s-40 17.9-40 40c0 14.4 7.6 27 19 34L209.7 220.6c-9.1 18.2-32.7 23.4-48.6 10.7L72 160c5-6.7 8-15 8-24c0-22.1-17.9-40-40-40S0 113.9 0 136s17.9 40 40 40c.2 0 .5 0 .7 0L86.4 427.4c5.5 30.4 32 52.6 63 52.6l277.2 0c30.9 0 57.4-22.1 63-52.6L535.3 176c.2 0 .5 0 .7 0c22.1 0 40-17.9 40-40s-17.9-40-40-40s-40 17.9-40 40c0 9 3 17.3 8 24l-89.1 71.3c-15.9 12.7-39.5 7.5-48.6-10.7L309 106z"/></svg>
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:
In the rules.json, make sure your regexFilter is correctly specified:
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.
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.