Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

122
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda