0

I would like to attach an id to each request going out via my extension. I have tried:

browser.webRequest.onBeforeSendHeaders.addListener(
    function (details) {
      const headers = details.requestHeaders ?? []

      headers.push({
        name: 'x-devqaly-request-id',
        value: uuidv4()
      })

      console.log(headers) // it shows that the header was added

      return { requestHeaders: headers }
    },
    { urls: ['https://api.codotto.com/*'] }, // The URL is correct as it works in other parts of the code with browser.webRequest.onBeforeRequest
    ['requestHeaders']
  )

But when I inspect the requests going out the header x-devqaly-request-id is not being attached to the request.

enter image description here

manifest.json

{
  "name": "devqaly",
  "description": "Record your screen while we record network requests, clicks, and events in your backend",
  "manifest_version":3,
  ...
  "permissions": [
    "storage",
    "webRequest",
    "scripting",
    "declarativeNetRequest"
  ],
  "host_permissions": [
    "<all_urls>"
  ]
}

I know that there is browser.declarativeNetRequest.updateDynamicRules but it is not possible to update the rules for each request. As you can see from the code example, I need to have a unique identifier for each request being sent in the header per request

How can I add a header using browser.webRequest.onBeforeSendHeaders? If not possible, is there a way to achieve sending a unique id per request in the header?

Bruno Francisco
  • 3,841
  • 4
  • 31
  • 61
  • You need to add `'blocking'` in the last parameter, see the documentation. – wOxxOm Jun 25 '23 at 19:29
  • @wOxxOm Thanks for the comment. The `'blocking'` requires the `webRequestBlocking` permission which is not available in manifest v3. I have updated my question to include `"manifest_version":3,` in the `manifest.json` – Bruno Francisco Jun 26 '23 at 06:58
  • Well of course in ManifestV3 you can't do it, at least in Chrome (I thought Firefox is more reasonable but I guess it's not). Additionally, Chrome makes an exception for policy-installed extensions, but I don't know about Firefox. – wOxxOm Jun 26 '23 at 07:38

0 Answers0