1

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.

nkhin
  • 11
  • 3
  • You can take a look at an example here - https://github.com/requestly/modify-headers-manifest-v3/tree/master/src You have to specify the resourceTypes and [an empty resourceType list is not allowed](https://developer.chrome.com/docs/extensions/reference/declarativeNetRequest/) – Sachin Jain Apr 26 '23 at 03:32
  • @SachinJain "An empty list is not allowed." My understanding is "resourceTypes": [] is not allowed. This is not a mandatory field. – nkhin Apr 26 '23 at 03:56
  • It may be https://crbug.com/1247400. Use an external tool like Fiddler, Charles, WireShark to verify the header. – wOxxOm Apr 26 '23 at 04:53
  • @wOxxOm In my case I am trying to modify the outgoing request headers instead of the incoming response headers. I verified using httpbin.org/get endpoint and confirmed the user-agent was not changed. – nkhin Apr 26 '23 at 05:46
  • Then it's clearly a bug in the API implementation of the optional resourceTypes. The underlying reason may be the same as https://crbug.com/1432871. – wOxxOm Apr 26 '23 at 07:00

0 Answers0