https://developer.chrome.com/docs/extensions/mv3/mv3-migration-checklist/#api_checklist
https://developer.chrome.com/docs/extensions/reference/declarativeNetRequest/#type-ResourceType
I try to use declarativeNetRequest to instead of webRequestBlocking.
In manifest.json
"manifest_version": 3,
"permissions": [
"declarativeNetRequest",
]
In background.js or devtool.js
chrome.declarativeNetRequest.updateDynamicRules({
addRules: [{
'id': 1001,
'priority': 1,
'action': {
'type': 'block'
},
'condition': {
'urlFilter': 'twitter.com',
'resourceTypes': [
'csp_report', 'font', 'image', 'main_frame', 'media', 'object', 'other', 'ping', 'script',
'stylesheet', 'sub_frame', 'webbundle', 'websocket', 'webtransport', 'xmlhttprequest'
]
}
}],
removeRuleIds: [1001]
})
\--------------------------------------------------------------------------------------------------------------
Are you currently using the blocking version of chrome.webRequest
?
This API is replaced by declarativeNetRequest
in Manifest V3.
This only applies to user-installed extensions; force installed extensions (extensions distributed using ExtensionInstallForcelist). These extensions — typically used in an enterprise setting — can still use the blocking version of chrome.webRequest
.
chrome.declarativeNetRequest
rules.webRequestBlocking
permission with declarativeNetRequest
.webRequest
permission if you no longer need to observe network requests.declarativeNetRequest
.\--------------------------------------------------------------------------------------------------------------
urlFilter | Matches | Does not match |
---|---|---|
"abc" | https://abcd.com https://example.com/abcd | https://ab.com |
"abc*d" | https://abcd.com https://example.com/abcxyzd | https://abc.com |
"||a.example.com" | https://a.example.com/ https://b.a.example.com/xyz | https://example.com/ |
"|https*" | https://example.com | http://example.com/ http://https.com |
"example*^123|" | https://example.com/123 http://abc.com/example?123 | https://example.com/1234 https://abc.com/example0123 |
urlFilter
string optional
The pattern which is matched against the network request url. Supported constructs:
'*' : Wildcard: Matches any number of characters.
'|' : Left/right anchor: If used at either end of the pattern, specifies the beginning/end of the url respectively.
'||' : Domain name anchor: If used at the beginning of the pattern, specifies the start of a (sub-)domain of the URL.
'^' : Separator character: This matches anything except a letter, a digit or one of the following: _ - . %.
This can also match the end of the URL.
Therefore urlFilter is composed of the following parts: (optional Left/Domain name anchor) + pattern + (optional Right anchor).
If omitted, all urls are matched. An empty string is not allowed.
A pattern beginning with ||* is not allowed. Use * instead.
Note: Only one of urlFilter or regexFilter can be specified.
Note: The urlFilter must be composed of only ASCII characters. This is matched against a url where the host is encoded in the punycode format (in case of internationalized domains) and any other non-ascii characters are url encoded in utf-8. For example, when the request url is http://abc.рф?q=ф, the urlFilter will be matched against the url http://abc.xn--p1ai/?q=%D1%84.
regexFilter
string optional
Regular expression to match against the network request url. This follows the RE2 syntax.
Note: Only one of urlFilter
or regexFilter
can be specified.
Note: The regexFilter
must be composed of only ASCII characters. This is matched against a url where the host is encoded in the punycode format (in case of internationalized domains) and any other non-ascii characters are url encoded in utf-8.
Example:
{
"id": 7,
"priority": 1,
"action": {
"type": "redirect",
"redirect": {
"regexSubstitution": "https://\\1.xyz.com/"
}
},
"condition": {
"regexFilter": "^https://www\\.(abc|def)\\.xyz\\.com/",
"resourceTypes": [
"main_frame"
]
}
},