alekzonder / docker-puppeteer

docker image with Google Puppeteer installed
https://hub.docker.com/r/alekzonder/puppeteer/
MIT License
485 stars 138 forks source link

Monitoring requested url returns "undefined" #8

Open githubyiti opened 6 years ago

githubyiti commented 6 years ago

Thank you for creating this easy to start docker image. While trying to use the following code to monitor the URL requested, the code returns:"undefined" How can I get the requested URLs? Thank you in advance.

//////////////////////////////
const puppeteer = require('puppeteer');
 (async() => {

    const browser = await puppeteer.launch({
        args: [
            '--no-sandbox',
            '--disable-setuid-sandbox',
            '--enable-logging', '--v=1'
        ]
    });

    const page = await browser.newPage();
    await page.setRequestInterception(true);
    page.on('request', request => {
        console.log(request.url);
            request.continue();
      });

    await page.goto('https://camo.githubusercontent.com/05492f5e135964801f6cbe748dc7668925a965e2/687474703a2f2f646f636b6572692e636f2f696d6167652f616c656b7a6f6e6465722f707570706574656572', {waitUntil: 'networkidle2'});

    browser.close();
})();
//////////////
Neoglyph commented 6 years ago

Hey,

Url is actually a function, so using it the way you did would return [Function: url]. The correct way would be:

console.log(request.url());

The above code works just fine for me otherwise.

What is the command you use to run your script?

githubyiti commented 6 years ago

Hi,

Thanks for not only giving the answer but also the reason!

So helpful for person just started learning javascript like me!

Thank you again for your time!