Overriding request response for images

Hey, I want to override request response image, how could I do that with puppeteer? Playwright has a neat field called from which I can include files from my local machine but with Puppeteer I can't make it work.
3 Replies
fascinating-indigo
fascinating-indigo•3y ago
can you show your code? and the website @liutc
dependent-tan
dependent-tan•3y ago
Hey, if I understand you correctly, here is how you can override image data with Puppeteer:
import { PuppeteerCrawler } from "crawlee";
import { readFileSync } from "fs";

const startUrls = ["https://crawlee.dev/"];
const placeholderImageBuffer = readFileSync("./test_img.png");

const crawler = new PuppeteerCrawler({
preNavigationHooks: [
async ({ addInterceptRequestHandler }) => {
await addInterceptRequestHandler((request) => {
if (request.resourceType() === "image") {
return request.respond({
status: 200,
contentType: "image/png",
body: placeholderImageBuffer,
});
}
return request.continue();
});
},
],
});

await crawler.run(startUrls);
import { PuppeteerCrawler } from "crawlee";
import { readFileSync } from "fs";

const startUrls = ["https://crawlee.dev/"];
const placeholderImageBuffer = readFileSync("./test_img.png");

const crawler = new PuppeteerCrawler({
preNavigationHooks: [
async ({ addInterceptRequestHandler }) => {
await addInterceptRequestHandler((request) => {
if (request.resourceType() === "image") {
return request.respond({
status: 200,
contentType: "image/png",
body: placeholderImageBuffer,
});
}
return request.continue();
});
},
],
});

await crawler.run(startUrls);
conscious-sapphire
conscious-sapphireOP•3y ago
Thanks @yellott trying it now 🙂

Did you find this page helpful?