Add-ons

webRequest.onBeforeRequest

This event is triggered when a request is about to be made, and before headers are available. This is a good place to listen if you want to cancel or redirect the request.

To cancel or redirect the request, first include "blocking" in the extraInfoSpec array argument to addListener(). Then, in the listener function, return a BlockingResponse object, setting the appropriate property:

  • to cancel the request, include a property cancel with the value true.
  • to redirect the request, include a property redirectUrl with the value set to the URL to which you want to redirect.

If an extension wants to redirect a public (e.g. HTTPS) URL to an extension page, the extension's manifest.json file must contain a web_accessible_resources key that lists the URL for the extension page.

When multiple blocking handlers modify a request, only one set of modifications take effect. Redirects and cancellations have the same precedence. So if you canceled a request, you might see another request with the same  requestId again if another blocking handler redirected the request.

From Firefox 52 onwards, instead of returning BlockingResponse, the listener can return a Promise which is resolved with a BlockingResponse. This enables the listener to process the request asynchronously.

If you use "blocking", you must have the "webRequestBlocking" API permission in your manifest.json.

Document Tags and Contributors

 Contributors to this page: stoically, durbin, wbamberg, snoack, Makyen, andymckay-github
 Last updated by: stoically,