How to Intercept HTTP requests in web extension ?
Last Updated :
07 Apr, 2025
In this article, we will understand to intercept the HTTP requests made by the browser using web extension API.
What is Intercepting HTTP Request?
Intercepting HTTP Requests means manipulating the HTTP requests made by the browser through the user or through asynchronous event handlers. With the help of web extension API, you can intercept any kind of web requests which are done in your browser. For example, you can change the request structure or log the request details on the console.
To intercept HTTP requests, use the webRequest API. This API enables you to add listeners for various stages of making an HTTP request.
With these listeners you can do the following:
- Get access to request headers and bodies and response headers.
- Cancel and redirect requests.
- Modify request and response headers.
Steps to follow to use this API:
- To use this API you need to provide special permission in the manifest.json file.
- This API can only be accessed using the background script so specify the background field in the manifest.json file.
Example manifest.json:
{
"name": "webRequest",
"version": "1.0.0",
"description": "webRequests",
"manifest_version": 2,
"permissions": [
"webRequest",
"<all_urls>"
],
"background": {
"scripts": ["background.js"]
}
}
Permissions for webRequest API:
- webRequest: To get access to the request object
- webRequestBlocking: To block the ongoing request
About the event and listener:
webRequest.onBeforeRequest is the listener used to get access to the HTTP requests.
This listener is triggered when a request is about to be made, and before headers are available.
onBeforeRequest has three functions :
1. addListener: Through this, we can add a listener to this event.
Syntax:
browser.webRequest.onBeforeRequest.addListener(
listener,
filter,
extraInfoSpec
)
Where:
- listener: Callback that returns the details object
- filter: A filter that restricts the events that will be sent to this listener.
- extraInfoSpec: array of string. Extra options for the event. You can pass any of the following values:
- "blocking": make the request synchronous, so you can cancel or redirect the request
- "requestBody": include requestBody in the details object passed to the listener
2. removeListener: Through this method,we can stop listening to this event.
Syntax:
browser.webRequest.onBeforeRequest.removeListener(listener)
3. hasListener: Using this method we can check whether the listener is registered for this event. Returns true if it is listening, false otherwise.
Syntax:
browser.webRequest.onBeforeRequest.hasListener(listener)
Let's understand with the help of examples.
Example 1: In this example, we will log the request messages using the webRequest API.
steps:
- load the extension
- open the background script and inspect the page
In the below file, you are providing access to the web extension API and helping your browser to load the extension.
manifest.json
{
"description": "webRequests",
"manifest_version": 2,
"name": "webRequest",
"version": "1.0",
"permissions": [
"webRequest",
"<all_urls>"
],
"background": {
"scripts": ["app.js"]
}
}
In the below app.js file you are logging the request URL on before the request.
JavaScript
// app.js
function logURL(requestDetails) {
console.log(`Loading: ${requestDetails.url}`);
}
browser.webRequest.onBeforeRequest.addListener(
logURL,
{ urls: ["<all_urls>"] }
);
Output:
Example 2: In this example, we will replace all the images from https://media.geeksforgeeks.org/ with https://media.geeksforgeeks.org/wp-content/uploads/20220830173608/Screenshot20220830173552.png
In the below file, you are providing access to the web extension API and helping your browser to load the extension.
manifest.json
{
"description": "webRequests",
"manifest_version": 2,
"name": "webRequest",
"version": "1.0",
"permissions": [
"webRequest",
"webRequestBlocking",
"https://www.geeksforgeeks.org/","https://media.geeksforgeeks.org/"
],
"background": {
"scripts": ["app.js"]
}
}
In the below file, you are changing all the images to https://media.geeksforgeeks.org/wp-content/uploads/20220830173608/Screenshot20220830173552.png
app.js
JavaScript
let pattern = "https://media.geeksforgeeks.org/*";
const targetUrl =
"https://media.geeksforgeeks.org/wp-content/uploads/20220830173608/Screenshot20220830173552.png";
function redirect(requestDetails) {
console.log(`Redirecting: ${requestDetails.url}`);
if (requestDetails.url === targetUrl) {
return;
}
return {
redirectUrl: targetUrl
};
}
browser.webRequest.onBeforeRequest.addListener(
redirect,
{ urls: [pattern], types: ["image", "imageset", "media"] },
["blocking"]
);
Output:
Similar Reads
How to make HTTP requests in Node ?
In the world of REST API, making HTTP requests is the core functionality of modern technology. Many developers learn it when they land in a new environment. Various open-source libraries including NodeJS built-in HTTP and HTTPS modules can be used to make network requests from NodeJS.There are many
4 min read
How To Use Axios NPM to Generate HTTP Requests ?
In this article, we are going to learn about Axios and HTTP requests and using Axios to generate HTTP requests. Axios is a promise-based HTTP library that is used by developers to make requests to their APIs or third-party API endpoints to fetch data. It is a popular JavaScript library used for maki
8 min read
How Are Parameters Sent In An HTTP POST Request?
HTTP POST requests are widely used in web development to send data from a client to a server. Whether you're submitting a form, uploading a file, or sending JSON data via an API, understanding how parameters are sent in an HTTP POST request is important. In this article, weâll explore how are parame
4 min read
How to create a new request in Postman?
Postman is a development tool that is used for testing web APIs i.e. Application Programming Interfaces. It allows you to test the functionality of any application's APIs. Almost every developer uses Postman for testing purposes. We can create any type of HTTP request in it such as GET, POST, PUT, D
2 min read
How to find the total number of HTTP requests ?
In this article, we will find the total number of HTTP requests through various approaches and methods like implementing server-side tracking by using PHP session variables or by analyzing the server logs to count and aggregate the incoming requests to the web application. By finding the total count
3 min read
How to send a POST Request with PHP ?
In web development, sending POST requests is a common practice for interacting with servers and exchanging data. PHP, a versatile server-side scripting language, provides various approaches to accomplish this task. This article will explore different methods to send POST requests using PHP. Table of
3 min read
How to set header request in Postman?
Postman is a powerful API development tool that offers a feature known as environment variables. These variables are used for efficient testing and development of APIs by allowing users to manage dynamic values across requests easily. In this article, we will learn How you can set header requests in
2 min read
How to Use Handle Get Request in Express.js ?
Express.js is a popular web application framework for Node.js, known for its simplicity and flexibility. One of the fundamental tasks in web development is handling HTTP requests, and GET requests are among the most common. This article will guide you through the process of handling GET requests in
2 min read
How to intercept response.send() / response.json() in Express JS
In the context of Express , "intercept" usually refers to the process of capturing or modifying a request or response in a middleware function before it reaches its final destination (e.g., a route handler) or before it is sent back to the client.In Express, intercepting response.send() or response.
3 min read
HTTP Interceptor use-cases in Angular
In Angular, HTTP interceptors are a powerful feature that allows you to intercept and modify HTTP requests and responses at a centralized location. They act as middleware, sitting between the applicationâs HTTP client (typically the built-in HttpClient module) and the server.In this article, we will
6 min read