# Catch webRequests when browser was closed before

**URL:** <https://discourse.mozilla.org/t/catch-webrequests-when-browser-was-closed-before/31339>\
**Category:** Development\
**Created:** [August 29, 2018, 1:31pm UTC](https://discourse.mozilla.org/t/catch-webrequests-when-browser-was-closed-before/31339 "2018-08-29T13:31:20Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![airbenich](https://avatars.discourse-cdn.com/v4/letter/a/d07c76/32.png) [@airbenich](https://discourse.mozilla.org/u/airbenich)\
**Post date:** [August 29, 2018, 1:31pm UTC](https://discourse.mozilla.org/t/catch-webrequests-when-browser-was-closed-before/31339/1 "2018-08-29T13:31:20Z")

</div>

Hey there!

When I catch a browser webRequest with:  
`chrome.webRequest.onBeforeRequest`

It works well. But only when the browser was open already. If the browser was closed and an external application opens this link, the webRequest won’t be watched. It looks like the extension is at that time not active.

How can I handle this?  
My extension already has the `background` permission in the `manifest.json` file.

Thank you for your help and greetings,  
Vivian

---

<div class="post-metadata">

**Author:** ![NilkasG](https://avatars.discourse-cdn.com/v4/letter/n/0ea827/32.png) [@NilkasG](https://discourse.mozilla.org/u/NilkasG)\
**Post date:** [August 29, 2018, 2:03pm UTC](https://discourse.mozilla.org/t/catch-webrequests-when-browser-was-closed-before/31339/2 "2018-08-29T14:03:43Z")

</div>

> My extension already has the `background` permission in the `manifest.json` file.

While that may help in Chrome, Firefox [doesn’t support](https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/manifest.json/permissions) it. (And I don’t think it ever will, how could this ever work with multiple profiles?)

I thought Firefox already had a logic similar to the one Chrome uses to resume event pages: record the extensions event listeners at shutdown and at startup, stall those events until the extension is started, then fire them.

Either that doesn’t work, isn’t implemented yet, or you are not adding your listeners soon enough / remove them to early.

---

<div class="post-metadata">

**Author:** ![freaktechnik](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/freaktechnik/32/37766_2.png) [@freaktechnik](https://discourse.mozilla.org/u/freaktechnik)\
**Post date:** [August 29, 2018, 2:04pm UTC](https://discourse.mozilla.org/t/catch-webrequests-when-browser-was-closed-before/31339/3 "2018-08-29T14:04:29Z")

</div>

I _think_ you need the webRequest blocking permission to get these requests early, and I’m not sure from what Firefox version on that actually works.

---

<div class="post-metadata">

**Author:** ![airbenich](https://avatars.discourse-cdn.com/v4/letter/a/d07c76/32.png) [@airbenich](https://discourse.mozilla.org/u/airbenich)\
**Post date:** [August 29, 2018, 2:12pm UTC](https://discourse.mozilla.org/t/catch-webrequests-when-browser-was-closed-before/31339/4 "2018-08-29T14:12:34Z")

</div>

Thank you @NilkasG and @freaktechnik for your fast reply!

@NilkasG So is there no option for firefox to intercept webRequests made on startup of firefox? My listeners are in the background.js right on top. Is there an earlier way to register listeners?

@freaktechnik In my list of permissions `webRequest` & `webRequestBlocking` is already listed. My firefox version so this should work.

---

<div class="post-metadata">

**Author:** ![freaktechnik](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/freaktechnik/32/37766_2.png) [@freaktechnik](https://discourse.mozilla.org/u/freaktechnik)\
**Post date:** [August 29, 2018, 2:16pm UTC](https://discourse.mozilla.org/t/catch-webrequests-when-browser-was-closed-before/31339/5 "2018-08-29T14:16:39Z")

</div>

Yeah, this should be working as far as it can with Firefox 61: [https://bugzilla.mozilla.org/show\_bug.cgi?id=1447551](https://bugzilla.mozilla.org/show_bug.cgi?id=1447551) (though bug fixes to the system may possibly have been introduced in later versions, since it’s an improvement over the status quo)

---

<div class="post-metadata">

**Author:** ![NilkasG](https://avatars.discourse-cdn.com/v4/letter/n/0ea827/32.png) [@NilkasG](https://discourse.mozilla.org/u/NilkasG)\
**Post date:** [August 29, 2018, 2:18pm UTC](https://discourse.mozilla.org/t/catch-webrequests-when-browser-was-closed-before/31339/6 "2018-08-29T14:18:55Z")

</div>

> [@airbenich](#):
>
> My listeners are in the background.js right on top. Is there an earlier way to register listeners?

It doesn’t really matter where you add them from, but _when_ they are added, i.e. whether you allow the event loop to pass one or multiple times before adding them.  
If you add them in a callback of an asynchronous operation (retrieving data from `browser.storage`, fetching scripts/resources, `setTimeout`, or really anything that involves Promises, you let the event loop pass and your listeners are not added synchronously anymore, which may be required here (even though I think that is pretty poor API design (originally by the Chrome team), considering that all `browser` APIs are asynchronous).

---

<div class="post-metadata">

**Author:** ![airbenich](https://avatars.discourse-cdn.com/v4/letter/a/d07c76/32.png) [@airbenich](https://discourse.mozilla.org/u/airbenich)\
**Post date:** [August 29, 2018, 3:53pm UTC](https://discourse.mozilla.org/t/catch-webrequests-when-browser-was-closed-before/31339/7 "2018-08-29T15:53:31Z")

</div>

Thank you @freaktechnik

Okay so this bug is already fixed but still present in the current version as far as I can interpret this. Some commented this issue as verified and tested with version 61.x

Im working with version 62.x but still see this error.

---

<div class="post-metadata">

**Author:** ![airbenich](https://avatars.discourse-cdn.com/v4/letter/a/d07c76/32.png) [@airbenich](https://discourse.mozilla.org/u/airbenich)\
**Post date:** [August 29, 2018, 3:55pm UTC](https://discourse.mozilla.org/t/catch-webrequests-when-browser-was-closed-before/31339/8 "2018-08-29T15:55:16Z")

</div>

Okay @NilkasG Thats clear. My listener is on the “root-level” and not nested in an asynchronous function or promise.
