Problem Description
- Issue: URLs are not being blocked as expected.
- Expected behavior: When a URL matches one in the blacklist, it should be blocked dynamically.
Current implementation
- I’m using
chrome.webRequest.onBeforeRequest
to intercept requests. - I fetch the blacklist using
fetchBlackList()
which retrieves URLs correctly. - I compare the request URL with entries in the blacklist and attempt to block using
chrome.declarativeNetRequest.updateDynamicRules
.
Observations
- I’ve verified that
fetchBlackList()
returns the expected URLs. - The comparison logic in
isBlocked
seems correct based on console logs. - However, URLs continue to load despite being in the blacklist.
What I’ve tried:
Implemented fetchBlackList()
:
Successfully retrieved the blacklist of URLs from my API using fetchBlackList()
. Confirmed that the URLs returned are correct and match those in the database.
Comparison logic (isBlocked
):
Implemented a comparison logic to check if the current request URL matches any URL in the fetched blacklist. Used RegExp to ensure accurate matching of domain and path.
Dynamic rule addition (chrome.declarativeNetRequest.updateDynamicRules):
Attempted to add dynamic rules to block URLs that match entries in the blacklist. Generated unique rule IDs and set up blocking actions accordingly.
Debugging with console logs:
Used extensive logging (console.log) throughout the code to trace the flow of data and confirm that URLs are being evaluated correctly in the onBeforeRequest listener.
Expected outcome:
When a user tries to access a URL that is listed in the fetched blacklist, the Chrome extension should dynamically block that request before it’s sent, using chrome.declarativeNetRequest.updateDynamicRules.
Octave Emmanuel Faye is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1