Skip to content

Instantly share code, notes, and snippets.

@phaalonso
Forked from jsoverson/intercept.js
Created February 21, 2022 22:24
Show Gist options
  • Save phaalonso/46e3fbde5bbe501964f0e284615014c8 to your computer and use it in GitHub Desktop.
Save phaalonso/46e3fbde5bbe501964f0e284615014c8 to your computer and use it in GitHub Desktop.
Intercept responses with chrome and puppeteer
const puppeteer = require('puppeteer');
const prettier = require('prettier');
const atob = require('atob');
const btoa = require('btoa');
const scriptUrlPatterns = [
'*'
]
const requestCache = new Map();
async function interceptRequestsForPage(page) {
const client = await page.target().createCDPSession();
await client.send('Network.enable');
await client.send('Network.setRequestInterception', {
patterns: scriptUrlPatterns.map(pattern => ({
urlPattern: pattern, resourceType: 'Script', interceptionStage: 'HeadersReceived'
}))
});
client.on('Network.requestIntercepted', async ({ interceptionId, request, responseHeaders, resourceType }) => {
console.log(`Intercepted ${request.url} {interception id: ${interceptionId}}`);
const response = await client.send('Network.getResponseBodyForInterception',{ interceptionId });
const contentTypeHeader = Object.keys(responseHeaders).find(k => k.toLowerCase() === 'content-type');
let newBody, contentType = responseHeaders[contentTypeHeader];
if (requestCache.has(response.body)) {
newBody = requestCache.get(response.body);
} else {
const bodyData = response.base64Encoded ? atob(response.body) : response.body;
try {
if (resourceType === 'Script') newBody = prettier.format(bodyData, { parser: 'babel' })
else newBody === bodyData
} catch(e) {
console.log(`Failed to process ${request.url} {interception id: ${interceptionId}}: ${e}`);
newBody = bodyData
}
requestCache.set(response.body, newBody);
}
const newHeaders = [
'Date: ' + (new Date()).toUTCString(),
'Connection: closed',
'Content-Length: ' + newBody.length,
'Content-Type: ' + contentType
];
console.log(`Continuing interception ${interceptionId}`)
client.send('Network.continueInterceptedRequest', {
interceptionId,
rawResponse: btoa('HTTP/1.1 200 OK' + '\r\n' + newHeaders.join('\r\n') + '\r\n\r\n' + newBody)
});
});
}
(async function main(){
const browser = await puppeteer.launch({
headless:false,
defaultViewport:null,
devtools: true,
args: ['--window-size=1920,1170','--window-position=0,0']
});
const page = (await browser.pages())[0];
await interceptRequestsForPage(page);
browser.on('targetcreated', async (target) => {
const page = await target.page();
await interceptRequestsForPage(page);
})
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment