Created
September 15, 2023 11:03
-
-
Save guvener/da679ae41b4d4e8537e0a2b422cd6af2 to your computer and use it in GitHub Desktop.
intercept/convert image loading chrome puppeteer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
await page.setRequestInterception(true); | |
page.on('request', async (req) => { | |
if (req.resourceType() !== 'image') { | |
return req.continue(); | |
} | |
try { | |
const res = await axios.get(req.url(), { responseType: 'arraybuffer' }); | |
const jpeg = await sharp(res.data).webp().toBuffer(); | |
req.respond({ body: jpeg }); | |
} catch(e) { | |
console.log(e); | |
req.continue(); | |
} | |
}); | |
await page.goto(url); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment