admin管理员组

文章数量:1287961

Sometimes, one needs to add special headers to each request or specific requests made from a browser. The mon 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.

Sometimes, one needs to add special headers to each request or specific requests made from a browser. The mon 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.

Share Improve this question asked Dec 28, 2017 at 1:12 Erran MoradErran Morad 4,75311 gold badges45 silver badges76 bronze badges 2
  • is using a proxy an acceptable solution to you? – user10864482 Commented Jan 15, 2019 at 20:09
  • @User23332 - sounds acceptable. – Erran Morad Commented Jan 21, 2019 at 6:07
Add a ment  | 

3 Answers 3

Reset to default 9

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.

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.

Theres no possibility to edit requests in existing DOM without using any external tools. The most suitable is Browser Extension which is editing the existing DOM and HTTP requests (XMLHttpRequest and fetch) done by JavaScript code.

Theres millions of possibilities to add headers to requests if the owner of website is you. And the solutions are different consider on what lib are you using for doing requests.

But in general it's not remended to modify website data that is not yours.

The Browser Extension is the exact thing that you found for your problem. Hope my ment will help you.

本文标签: javascriptHow to add headers to a browser request without any browser extensionsStack Overflow