Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
puppeteer-intercept-and-modify-requests
Advanced tools
The `puppeteer-intercept-and-modify-requests` TypeScript library allows you to intercept and modify network requests and responses using Puppeteer. It supports modifying all network requests and responses. You can also apply delays, stream responses, modi
The puppeteer-intercept-and-modify-requests
TypeScript library allows you to intercept and modify network requests and responses using Puppeteer. It supports modifying all network requests and responses. You can also apply delays, stream responses, modify error responses or apply network errors. It provides a powerful and flexible tool for controlling network behavior in your web automation tasks.
errorReason
instead of a body
.modifyRequest
function.undefined
.The puppeteer-intercept-and-modify-requests
library offers several advantages over using the rudimentary page.setRequestInterception
and page.on('request', interceptedRequestCallback)
built-in to Puppeteer:
In summary, the puppeteer-intercept-and-modify-requests
library offers a more powerful and flexible solution for controlling network behavior in your Puppeteer projects, making it a superior choice over the built-in page.setRequestInterception
and page.on('request', interceptedRequestCallback)
features.
npm install puppeteer-intercept-and-modify-requests
To modify intercepted requests:
import { RequestInterceptionManager } from 'puppeteer-intercept-and-modify-requests'
// assuming 'page' is your Puppeteer page object
const client = await page.target().createCDPSession()
// note: if you want to intercept requests on ALL tabs, instead use:
// const client = await browser.target().createCDPSession()
const interceptManager = new RequestInterceptionManager(client)
await interceptManager.intercept(
{
// specify the URL pattern to intercept:
urlPattern: `https://example.com/*`,
// optionally filter by resource type:
resourceType: 'Document',
// specify how you want to modify the response (may be async):
modifyResponse({ body }) {
return {
// replace break lines with horizontal lines:
body: body.replaceAll('<br/>', '<hr/>'),
}
},
},
{
urlPattern: '*/api/v4/user.json',
// specify how you want to modify the response (may be async):
modifyResponse({ body }) {
const parsed = JSON.parse(body)
// set role property to 'admin'
parsed.role = 'admin'
return {
body: JSON.stringify(parsed),
}
},
},
)
ModifiedResponse
: A type representing a modified response object.ModifiedRequest
: A type representing a modified request object.ModifyResponseChunkFn
: A function type for modifying response chunks.StreamResponseConfig
: A configuration object for streaming responses.Interception
: An interception configuration object.InterceptionWithUrlPatternRegExp
: An interception configuration object extended with a RegExp representation of the URL pattern.RequestInterceptionManager
: A class for managing request interceptions.
constructor(client: CDPSession, { onError } = {})
: Creates a new RequestInterceptionManager
instance.
async intercept(...interceptions: Interception[])
: Enables request interception and adds the provided interception configurations.
async removeIntercept(interceptUrlPattern: string)
: Removes an existing interception configuration for the specified URL pattern.
async enable()
: Enables request interception based on the current interception configurations.
async disable()
: Disables request interception.
async clear()
: Clears all interception configurations and disables request interception.
async onRequestPausedEvent(event: Protocol.Fetch.RequestPausedEvent)
: An async event handler for when a request is paused.
Here's an example of how to use the RequestInterceptionManager
to intercept and modify a request and a response:
import puppeteer from 'puppeteer'
import {
RequestInterceptionManager,
Interception,
} from 'puppeteer-intercept-and-modify-requests'
async function main() {
const browser = await puppeteer.launch()
const page = await browser.newPage()
const client = await page.target().createCDPSession()
const requestInterceptionManager = new RequestInterceptionManager(client)
const interceptionConfig: Interception = {
urlPattern: 'https://example.com/*',
modifyRequest: async ({ event }) => {
// Modify request headers
return {
headers: [{ name: 'X-Custom-Header', value: 'CustomValue' }],
}
},
modifyResponse: async ({ body }) => {
// Modify response body
const modifiedBody = body.replace(/example/gi, 'intercepted')
return { body: modifiedBody }
},
}
await requestInterceptionManager.intercept(interceptionConfig)
await page.goto('https://example.com')
await browser.close()
}
main()
This example modifies the request by adding a custom header and modifies the response by replacing all occurrences of the word "example" with "intercepted".
You can apply a delay to a request or response using the delay
property in the modifyRequest
or modifyResponse
functions. Here's an example of how to add a delay to a request:
import puppeteer from 'puppeteer'
import {
RequestInterceptionManager,
Interception,
} from 'puppeteer-intercept-and-modify-requests'
async function main() {
const browser = await puppeteer.launch()
const page = await browser.newPage()
const client = await page.target().createCDPSession()
const requestInterceptionManager = new RequestInterceptionManager(client)
const interceptionConfig: Interception = {
urlPattern: 'https://example.com/*',
modifyRequest: async ({ event }) => {
// Add a 500 ms delay to the request
return {
delay: 500,
}
},
}
await requestInterceptionManager.intercept(interceptionConfig)
await page.goto('https://example.com')
await browser.close()
}
main()
In this example, a 500 ms delay is added to the request for the specified URL pattern.
You can handle errors using the onError
option when creating a new RequestInterceptionManager
instance. Here's an example of how to handle errors:
import puppeteer from 'puppeteer'
import {
RequestInterceptionManager,
Interception,
} from 'puppeteer-intercept-and-modify-requests'
async function main() {
const browser = await puppeteer.launch()
const page = await browser.newPage()
const client = await page.target().createCDPSession()
const requestInterceptionManager = new RequestInterceptionManager(client, {
onError: (error) => {
console.error('Request interception error:', error)
},
})
const interceptionConfig: Interception = {
urlPattern: 'https://example.com/*',
modifyRequest: async ({ event }) => {
// Modify request headers
return {
headers: [{ name: 'X-Custom-Header', value: 'CustomValue' }],
}
},
}
await requestInterceptionManager.intercept(interceptionConfig)
await page.goto('https://example.com')
await browser.close()
}
main()
In this example, any errors that occur during request interception are logged to the console with the message "Request interception error:".
To fail a request, return an object containing an errorReason
property in the modifyRequest
function. Here's an example of how to fail a request:
import puppeteer from 'puppeteer'
import {
RequestInterceptionManager,
Interception,
} from 'puppeteer-intercept-and-modify-requests'
async function main() {
const browser = await puppeteer.launch()
const page = await browser.newPage()
const client = await page.target().createCDPSession()
const requestInterceptionManager = new RequestInterceptionManager(client)
const interceptionConfig: Interception = {
urlPattern: 'https://example.com/*',
modifyRequest: async ({ event }) => {
// Fail the request with the error reason "BlockedByClient"
return {
errorReason: 'BlockedByClient',
}
},
}
await requestInterceptionManager.intercept(interceptionConfig)
await page.goto('https://example.com')
await browser.close()
}
main()
In this example, the request for the specified URL pattern is blocked with the error reason "BlockedByClient".
When creating the RequestInterceptionManager
instance, you can pass in the client
object from the CDPSession
of the Browser
object. This will allow you to intercept requests from all the pages rather than just the one. Here's an example of how to do this:
// intercept requests on ALL tabs, instead use:
const client = await browser.target().createCDPSession()
const interceptManager = new RequestInterceptionManager(client)
// ...
You can also stream and modify response chunks using the streamResponse
and modifyResponseChunk
options. Here's an example of how to do this:
import puppeteer from 'puppeteer'
import {
RequestInterceptionManager,
Interception,
} from 'puppeteer-intercept-and-modify-requests'
async function main() {
const browser = await puppeteer.launch()
const page = await browser.newPage()
const client = await page.target().createCDPSession()
const requestInterceptionManager = new RequestInterceptionManager(client)
const interceptionConfig: Interception = {
urlPattern: 'https://example.com/*',
streamResponse: true,
modifyResponseChunk: async ({ event, data }) => {
// Modify response chunk
const modifiedData = data.replace(/example/gi, 'intercepted')
return { ...event, data: modifiedData }
},
}
await requestInterceptionManager.intercept(interceptionConfig)
await page.goto('https://example.com')
await browser.close()
}
main()
In this example, the response is streamed and each response chunk has all occurrences of the word "example" replaced with "intercepted".
FAQs
The `puppeteer-intercept-and-modify-requests` TypeScript library allows you to intercept and modify network requests and responses using Puppeteer. It supports modifying all network requests and responses. You can also apply delays, stream responses, modi
We found that puppeteer-intercept-and-modify-requests demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.