Type in the console with headless mode (puppeteer)

  console, headless, javascript, node.js, puppeteer

I am trying to write a function in the console using node and puppeteer to extract some data. There is an easy way to get all console logs via puppeteer:

const puppeteer = require("puppeteer");

(async () => {
  const browser = await puppeteer.launch({
    devtools: true,
  const page = await browser.newPage();

  await page.goto("https://www.example.com/", {
    waitUntil: "load",
    timeout: 0,

  // getting console logs
  const data = await page.on("console", (msg) => {
    return msg;

  await browser.close();
  console.log("MY DATA", data);

what I need is to type in console my function before returning all logs. Any ideas how to do that?

Source: Ask Javascript Questions