I am building a chrome extenstion using manifest V3. It should listen for webRequests against certain urls and react on these.
In the service-worker.js:
- rules are set for supported domains (in this example -> example.com) to block a webrequest (replacing manifests V2 webRequestBlocking).
- a listener configured to listen to these requests and should then react and log the url.
When I install the extension and open the popup, it reacts to a request in the currently opened tab. When I dont open the extension popup first, it doesnt recognize the request at all. Also, everytime I open a new tab, I need to click on the extension icon first, before it reacts to a request.
Why is that and can I somehow make sure that it also works out of the box, if i just install the extension?
I also tried what was recommended here without any success: Persistent Service Worker in Chrome Extension (There was a "persistent": true parameter in Manifest V2 keeping the background script alive, but it seems that there is no equivalent in V3)
The service-worker.js:
let rules = [];
const supportedDomainPatterns = ['*://*example.com/*'];
supportedDomainPatterns.forEach((domainPattern, index) => {
rules.push({
id: index + 1001,
priority: 1,
action: {
type: 'block',
},
condition: {
urlFilter: domainPattern,
},
});
});
// Collect all the rule IDs to remove.
const ruleIdsToRemove = rules.map((rule) => rule.id);
chrome.runtime.onInstalled.addListener(() => {
chrome.declarativeNetRequest.updateDynamicRules({
removeRuleIds: ruleIdsToRemove,
addRules: rules,
});
});
chrome.webRequest.onBeforeRequest.addListener(
(details) => {
const url = new URL(details.url);
console.log('caught request' + url);
},
{
urls: supportedDomainPatterns,
types: ['main_frame'],
}
);
My manifest.json:
{
"manifest_version": 3,
"name": "example extension",
"description": "example",
"version": "1.0",
"background": {
"service_worker": "service-worker.js"
},
"action": {
"default_popup": "popup.html",
"default_icon": "icon-34.png"
},
"icons": {
"128": "icon-128.png"
},
"web_accessible_resources": [
{
"resources": ["content.styles.css", "icon-128.png", "icon-34.png"],
"matches": []
}
],
"host_permissions": ["*://*example.com/*"],
"permissions": [
"declarativeNetRequest",
"activeTab",
"webRequest",
]
}