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

108
Vistas
Injecting Function with Puppeteer

I am trying to inject this function to webpage with Puppeteer to get the event listeners of the website.

My code is:

Array.from(document.querySelectorAll('*'))
  .reduce(function(pre, dom){
    var evtObj = getEventListeners(dom)
    Object.keys(evtObj).forEach(function (evt) {
      if (typeof pre[evt] === 'undefined') {
        pre[evt] = 0
      }
      pre[evt] += evtObj[evt].length
    })
    return pre
  }, {})

I have tried to run code below.

'use strict';

const puppeteer = require('puppeteer');

(async () => {
    const browser = await puppeteer.launch({ headless: false });
    const page = await browser.newPage();
    await page.goto('https://google.com')
    
    
    var FunctionToInject = function(input){
        Array.from(document.querySelectorAll('*'))
        .reduce(function(pre, dom){
          var evtObj = getEventListeners(dom)
          Object.keys(evtObj).forEach(function (evt) {
            if (typeof pre[evt] === 'undefined') {
              pre[evt] = 0
            }
            pre[evt] += evtObj[evt].length
          })
          return pre
        }, {})
      

    }
    
    await page.exposeFunction('functionToInject', functionToInject);

    
    var data = await page.evaluate(async function(){
        return  await FunctionToInject();
    });
    
    console.log(data);
    

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

I am getting UnhandledPromiseRejectionWarning: Error: Evaluation failed: ReferenceError: document is not defined error. How can I resolve this issue?

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