jsoverson / puppeteer-interceptor Goto Github PK
View Code? Open in Web Editor NEWEasy request interception/response modification for puppeteer
Easy request interception/response modification for puppeteer
I saw it had collapsed non-ascii characters originally encoded in utf-8.
For example, for ブーツ
it produced ���
.
It'd be favorable to respect Content-Type
in the response header.
Also, it'd be nice to pass the original base64 encoded data to event handlers that will bring a chance to solve this kind of problem by them.
Failing using auth proxies
var context = await browser.createIncognitoBrowserContext({ proxyServer: proxy });
await page.authenticate({
username: user,
password: pass,
})
Result: ERR_INVALID_AUTH_CREDENTIALS
The credentials are valid
I want to modify response headers, but the codes below can't work.
intercept(page, patterns.Document('*'), {
onInterception: event => {
console.log(`${event.request.url} intercepted.`)
},
onResponseReceived: event => {
event.response.headers.push({
name: 'Link',
value: '<https://x.xxx.com>; rel="preconnect"'
});
console.log(`${event.request.url} intercepted, going to modify`)
return event.response;
}
});
Besides, I try to modify the source code, use 'Fetch.continueRequest' to modify response headers, can't work either
// in my code
intercept(page, patterns.Document('*'), {
onInterception: event => {
console.log(`${event.request.url} intercepted.`)
},
onResponseReceived: event => {
return null;
}
});
// in source code
if (newResponse) {
debug(`Fulfilling request ${requestId} with response returned from onResponseReceived`);
console.log('newResponse headers count:', newResponse.headers.length);
await client.send('Fetch.fulfillRequest', {
requestId,
responseCode: newResponse.statusCode,
responseHeaders: newResponse.headers,
body: newResponse.base64Body ? newResponse.base64Body : btoa_1.default(newResponse.body),
responsePhrase: newResponse.statusMessage,
});
}
else {
// here I try to modify response headers
await client.send('Fetch.continueRequest', { requestId, headers: [
{name: 'Link', value: '<https://x.xxx.com>; rel="preconnect"'},
] });
}
Simply logging the request object like so, it is clear that no field is exposed to modify a request POST body before it is sent out. This request here for example does have a POST body, but it is not indicated here, and cannot be mutated in any way.
console.log(event.request);
{
url: 'https://someurl',
method: 'GET',
headers: {
'sec-ch-ua': '" Not A;Brand";v="99", "Chromium";v="90"',
'sec-ch-ua-mobile': '?0',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4403.0 Safari/537.36',
Accept: '*/*'
},
initialPriority: 'Low',
referrerPolicy: 'strict-origin-when-cross-origin'
}
302 redirect not have response body,throw Error "Can only get response body on requests captured after headers received"
Maybe you need a ---> try catch
try {
const responseCdp = (await client.send('Fetch.getResponseBody', {
requestId,
}));
const response = {
body: responseCdp.base64Encoded ? atob_1.default(responseCdp.body) : responseCdp.body,
headers: event.responseHeaders,
errorReason: event.responseErrorReason,
statusCode: event.responseStatusCode,
};
newResponse = await this.eventHandlers.onResponseReceived({ response, request });
} catch (e) {
console.log(e);
newResponse = await this.eventHandlers.onResponseReceived({ test: 'test' });
}
Does not allow proxies with authentication to be authenticated.
const proxyUser = `o`;
const proxyPass = 'f';
const options = {
headless: false,
// devtools: true,
args: [
'--no-sandbox',
`--proxy-server=proxy:22225`,
],
};
let browser = await puppeteer.launch(options);
const page = await browser.newPage();
await page.authenticate({ username: proxyUser, password: proxyPass });
await page.goto('chrome://about')
Rather than authenticate the page it will ask for authentication.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.