title: Puppeteer dynamically add event listeners to page object
body: <p>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 --</p>
  <pre><code>const puppeteer = require('puppeteer');
  
  const listeners = {
    'console': function(consoleObj) {
      console.log(consoleObj.text());
    },
    'dialog': function(dialog) {
      console.log(dialog.message());
    }
  };
  
  (async () =&gt; {
    const browser = await puppeteer.launch({headless: false});
    const page = await browser.newPage();
    for (var l in listeners) {
      page.on(l, obj =&gt; {
        listeners[l](obj)
      });
    }
    var el = await page.evaluate(_ =&gt; {
      console.log('page console operational!')
    });
  })();
  </code></pre>
  <p>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) --</p>
  <pre><code>for (var l in listeners) {
    page.on('console', obj =&gt; {
      listeners[l](obj)
    });
  }
  </code></pre>
  <p>How can I go about dynamically creating event listeners with Puppeteer?</p>