Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

111
Views
Puppeteer dynamically add event listeners to page object

I'm writing some utility functions for puppeteer to speed up workflow. I want to assign callback functions to dynamically created event listeners. Here's my code so far --

const puppeteer = require('puppeteer');

const listeners = {
  'console': function(consoleObj) {
    console.log(consoleObj.text());
  },
  'dialog': function(dialog) {
    console.log(dialog.message());
  }
};

(async () => {
  const browser = await puppeteer.launch({headless: false});
  const page = await browser.newPage();
  for (var l in listeners) {
    page.on(l, obj => {
      listeners[l](obj)
    });
  }
  var el = await page.evaluate(_ => {
    console.log('page console operational!')
  });
})();

For some reason the page.on() function won't accept dynamically input listener types. Whereas if I input the listener type manually it works (obviously this is not useful because it will just continuously overwrite the 'console' listener) --

for (var l in listeners) {
  page.on('console', obj => {
    listeners[l](obj)
  });
}

How can I go about dynamically creating event listeners with Puppeteer?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!