I am writing a Chrome extension that modifies the user-agent header using declarativeNetRequest API. Interesting enough, the user-agent is only modified for requests within iframes as shown in the devtools. I must have missed/misunderstood something.
manifest.json
{
"name": "My extension",
"version": "1.0.0",
"manifest_version": 3,
"description": "My extension",
"homepage_url": "http://example.com",
"content_scripts": [
{
"matches": ["http://*/*", "https://*/*"],
"css": ["style.css"],
"js": ["script.js"]
}
],
"declarative_net_request": {
"rule_resources": [{
"id": "modify-user-agent",
"enabled": true,
"path": "rules.json"
}]
},
"icons": {
"16": "icons/icon16.png",
"48": "icons/icon48.png",
"128": "icons/icon128.png"
},
"web_accessible_resources": [{
"resources": [
"redirect.html"
],
"matches": [
"*://*/*"
]
}],
"action":
{
"default_icon": "icons/icon48.png",
"default_popup": "./src/index.html"
},
"default_locale": "en",
"permissions": [
"contentSettings",
"tabs",
"storage",
"declarativeNetRequest",
"declarativeNetRequestWithHostAccess"
],
"host_permissions": [
"*://*/*"
]
}
rules.json
[{
"id": 1,
"priority": 1,
"action": {
"type": "modifyHeaders",
"requestHeaders": [{
"header": "User-Agent",
"operation": "set",
"value": "My User Agent"
}]
},
"condition": {
"urlFilter": "*://*/*"
}
}]
Update: I modified the rule and include the resourceType in the condition:
"resourceTypes": ["main_frame", "sub_frame"]
This is working in both the main frame and iframe. But I still do not understand why by default it only applies to the iframe. I could not find any documentation regarding the default value for resourceTypes if not specified.