Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add headers to a browser request without any browser extensions?

Sometimes, one needs to add special headers to each request or specific requests made from a browser. The common approach to do this is by using browser extensions which allow us to modify request headers. Is there another way to do this, without any browser extension ?

PS - I have searched SO and not found a single post which actually suggests or shows how to do what I need.

like image 832
Erran Morad Avatar asked Oct 29 '25 14:10

Erran Morad


2 Answers

Outside of APIs designed to make custom HTTP requests (XMLHttpRequest and fetch), it is impossible to add arbitrary HTTP headers to requests made by browsers using JS embedded in a page.

like image 99
Quentin Avatar answered Oct 31 '25 03:10

Quentin


If you control the websites that you want this functionality on, you could achieve this by setting each application to install a ServiceWorker. In a nutshell, service workers run as a proxy server within your browser. They can do things like notify you of updates even if you don't have the website open.

Within a ServiceWorker you are able to set up event listeners that can do some asynchronous task on behalf of the client app. This includes the fetch event which is fired every time the web page makes a request.

Here's a write up on someone implementing a ServiceWorker who also needed to intercept network requests. You could follow most of this and just alter the logic when inspecting the request type. At that point you could add any special headers before dispatching on the applications behalf.

like image 36
GenericUser Avatar answered Oct 31 '25 04:10

GenericUser