3

We have an extension with only 2 files: manifest.json and background.js

The browser (chrome version 112) doesn't report any error, but the user agent is not getting set to `my-custom-user-agent. Here is the complete extension code:

manifest.json

{
    "name": "ua-customizer",
    "version": "1",
    "manifest_version": 3,
    "permissions": [
      "declarativeNetRequest"
    ],
    "host_permissions": [
      "<all_urls>"
    ], 
    "background": {
      "service_worker": "background.js"
    }
  }

background.js

const rules = {
  removeRuleIds: [1],
  addRules: [
    {
      id: 1,
      priority: 1,
      action: {
        type: 'modifyHeaders',
        requestHeaders: [
          {
            header: 'user-agent',
            operation: 'set',
            value: `my-custom-user-agent`,
          },
        ],
      },
      condition: {
        urlFilter: '<all_urls>'
      },
    },
  ],
}
chrome.declarativeNetRequest.updateDynamicRules(rules);

What is it missing?

user3330840
  • 6,143
  • 7
  • 26
  • 39
  • @ThomasMueller Placing the code inside that event handler didn't create any visible difference. If you have access to a chrome or edge browser, would you please test it on your side to verify that this issue is not related to just one local machine or browser? – user3330840 May 06 '23 at 20:41
  • Adding `resourceTypes: [ "main_frame" ]` to the RuleCondition fixed it for me. Tested at https://www.whatismybrowser.com/detect/what-is-my-user-agent/ - The code works even when `chrome.declarativeNetRequest.updateDynamicRules()` is called at the top level of background.js, but it's cleaner to put it in a `chrome.runtime.onInstalled`handler. – Thomas Mueller May 06 '23 at 20:43
  • @ThomasMueller Did you make any additional changes? Because on my browser I'm seeing no effect with `resourceTypes` added. – user3330840 May 06 '23 at 20:48
  • You also need to change `urlFilter: ''` to `urlFilter: '*'`, because "all_urls" is not a supported construct in urlFilter, see [chrome.declarativeNetRequest > RuleCondition](https://developer.chrome.com/docs/extensions/reference/declarativeNetRequest/#type-RuleCondition) > urlFilter – Thomas Mueller May 06 '23 at 20:48
  • Yes, `urlFilter: '*'` fixed it. If you want to just place that as an answer I can accept that. – user3330840 May 06 '23 at 20:53

1 Answers1

2

Changes:

Changed code:

condition: {
    resourceTypes: [ 'main_frame' ],
    urlFilter: '*'
},

Both changes are necessary.
If you make only one of the changes, the extension will not modify the user-agent string.

Also, chrome.declarativeNetRequest.updateDynamicRules(rules); only needs to be executed once.
See chrome.declarativeNetRequest > updateDynamicRules: "These rules are persisted across browser sessions and across extension updates."
I recommend putting it in a chrome.runtime.onInstalled handler:

chrome.runtime.onInstalled.addListener(function(details) {
    chrome.declarativeNetRequest.updateDynamicRules(rules);
});
Thomas Mueller
  • 514
  • 1
  • 4
  • 10