Topic: https://intoli.com/blog/saving-images/
hide preview

What's next? verify your email address for reply notifications!

NqZY0bBX 6y, 211d ago

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:

  1. We're now listening to the 'response' rather than 'requestfinished'. A line of code was removed as a result.
  2. The async await syntax has changed.
  3. 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();
})();
remark link
hide preview

What's next? verify your email address for reply notifications!

andre 6y, 200d ago [edited]

Thanks. I've updated the article to use Puppeteer's new API.

hide preview

What's next? verify your email address for reply notifications!

unverified 6y, 69d ago

Thank you very much :)

hide preview

What's next? verify your email address for reply notifications!

25L8LaRZ 5y, 281d ago

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?

remark link
hide preview

What's next? verify your email address for reply notifications!

andre 5y, 281d ago

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!

hide preview

What's next? verify your email address for reply notifications!