0

Using WebExtensions, I am trying to write a content script which would count the number of HTTP responses per web page.

I just started looking into WebExtensions, so I may be completely off the track, but so far my approach has been to modify the background script to listen to browser.webRequest.onHeadersReceived. Once fired, callback to that function would invoke browser.tabs.sendMessage to the tabId from where the request to this response originated.

In the content script I use browser.runtime.onMessage.addListener to listen to those messages from the background script.

With this approach I have two problems:

  1. The background script is trying to send messages to the content script even before the content script has started listening, resulting in errors from the sendMessage and thus lost information.
  2. On page reload, some of the messages from the background script are received before the content script is reloaded, some are lost (presumably during the period where one content script was unloaded and before another one started) and some are received after.

I've been looking whether browser.storage.local can help me with this situation. With this I can avoid the problem of messages being lost by simply keeping the count in that storage.

But I still have the problem in the background script that I don't know whether to increment the count for the web page that was displayed before the reload happened, or the one after (due to the problem #2 above).

I think instead of using tabId as a "pointer" to where the count is increased, having some kind of an ID for the web page to which the response belongs to would help. But I haven't been able to find anything like that in the documentation yet. But again, I'm novice to WebExtensions, so there may be a completely different approach I'm missing.

Peter Jankuliak
  • 3,464
  • 1
  • 29
  • 40
  • 1
    There could be several solutions depending on how you use that data in the end. So far it sounds like all you need is to add `"run_at": "document_start"` to your content script declaration. Alternatively you can accumulate the data in the background script's global variable e.g. `const data = {}` then in the webRequest listener you would update or add `data[details.tabId]`. As for intercepting requests in the content script it'll be more convoluted. – wOxxOm Mar 27 '20 at 15:28
  • @wOxxOm thanks, setting `"run_at": "document_start"` seems to be what I need (feel free to post it as answer, I'll do some more testing and likely accept it). I'm unsure about your second suggestion, the problem being that the background script doesn't know when the document is reloaded and so I don't know when to reset the accumulated data back to zero. – Peter Jankuliak Mar 30 '20 at 10:00
  • 1
    The background script can use browser.webNavigation API to detect page reloads. I think I've even seen an answer about that on SO. – wOxxOm Mar 30 '20 at 10:26
  • You're right, I am able to reset the accumulated data in a callback passed to `browser.webNavigation.onBeforeNavigate.addListener`. I now prefer the second solution, thanks :) – Peter Jankuliak Apr 01 '20 at 11:34

0 Answers0