For Saving All Images, changes were made to Puppeteer API that prevents the code from working correctly. The code below works for puppeteer@1.1.1. Changes made:
- We're now listening to the 'response' rather than 'requestfinished'. A line of code was removed as a result.
- The async await syntax has changed.
- The Promise returned by the response event is now handled correctly.
const fs = require('fs'); const puppeteer = require('puppeteer'); (async () => { const browser = await puppeteer.launch(); const page = await browser.newPage(); page.setViewport({ width: 1280, height: 926 }); let counter = 0; page.on('response', async response => { const matches = /.*\.(jpg|png|svg|gif)$/.exec(response.url()); if (matches && (matches.length === 2)) { const extension = matches[1]; const buffer = await response.buffer() .catch(err => { console.log(err); }); fs.writeFileSync(`images/image-${counter}.${extension}`, buffer, 'base64'); counter += 1; } }); await page.goto('https://intoli.com/blog/saving-images/'); await page.waitFor(10000); await browser.close(); })();
While evaluating JavaScript on the page is easy, we’ve seen that it comes with its own set of potentially undesirable limitations.
Can you explain what these limitations are?
I was mostly referring to limitations surrounding the Same-origin policy that I briefly mentioned. You may choose to count encoding images as data URLs as problematic, too!