How to limit resources rendered with puppeter

Does any one know how to limit the requests sent by puppeteer? I'm rendering a webpage that sends request to a lot of places like google analytics and facebook. I would like to make puppeteer not render these pages so my algorithm improves a bit on speed.

Thanks :)

Upvotes: 2

Views: 1317

Answers (1)

Grant Miller
Grant Miller

Reputation: 29037

You can use page.setRequestInterception() to limit the resources rendered with Puppeteer.

Here's a sample program that will abort requests with URLs containing google-analytics or facebook:

'use strict';

const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();

  await page.setRequestInterception(true);

  page.on('request', intercepted_request => {
    if (intercepted_request.url().includes('google-analytics') || intercepted_request.url().includes('facebook')) {
      intercepted_request.abort();
    } else {
      intercepted_request.continue();
    }
  });

  await page.goto('https://example.com/');

  await browser.close();
})();

Upvotes: 2

Related Questions