Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Network.requestIntercepted with Puppeteer

I'm trying to get urls of navigation/domain redirects using the Chrome Dev tools Network.requestIntercepted event through Puppeteer, but I cant seem to access any of the events data.

The code below doesn't seem to trigger Network.requestIntercepted and I can't work out why.

Any help appreciated.

// console command
// node chrome-commands.js http://yahoo.com test

var url = process.argv[2];
const puppeteer = require('puppeteer');

puppeteer.launch().then(async browser => {
const page = await browser.newPage();
const client = await page.target().createCDPSession();
await client.send('Network.enable');

await client.on('Network.requestIntercepted', (e) => {
  console.log(e);
  console.log("EVENT INFO: ");
  console.log(e.interceptionId);
  console.log(e.resourceType);
  console.log(e.isNavigationRequest);
});

  await page.goto(url);
  await browser.close();
});
like image 239
turrican_34 Avatar asked Mar 10 '18 16:03

turrican_34


1 Answers

You should configure Network.setRequestInterception before Network.requestIntercepted. Here is working example:

const url = 'http://yahoo.com';
const puppeteer = require('puppeteer');

puppeteer.launch({ userDataDir: './data/' }).then(async browser => {
  const page = await browser.newPage();
  const client = await page.target().createCDPSession();
  await client.send('Network.enable');

  // added configuration
  await client.send('Network.setRequestInterception', {
    patterns: [{ urlPattern: '*' }],
  });

  await client.on('Network.requestIntercepted', async e => {
    console.log('EVENT INFO: ');
    console.log(e.interceptionId);
    console.log(e.resourceType);
    console.log(e.isNavigationRequest);

    // pass all network requests (not part of a question)
    await client.send('Network.continueInterceptedRequest', {
      interceptionId: e.interceptionId,
    });
  });

  await page.goto(url);
  await browser.close();
});
like image 133
Everettss Avatar answered Oct 09 '22 23:10

Everettss