Skip to content

Instantly share code, notes, and snippets.

@RedstoneWizard08
Created October 12, 2022 03:44
Show Gist options
  • Save RedstoneWizard08/72e303d490fd8901b02adc9eff7a5925 to your computer and use it in GitHub Desktop.
Save RedstoneWizard08/72e303d490fd8901b02adc9eff7a5925 to your computer and use it in GitHub Desktop.
How to view JavaScript Console output in the linux (or anything that supports TypeScript) console:

I've needed to debug some projects from the CLI, so I created a handy little script to let me do just that. Here it is!

import { blue, cyan, green, magenta, red, yellow } from "colorette";
import puppeteer from "puppeteer";
const main = async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
page.on("console", (message) => {
const type = message.type().substr(0, 3).toUpperCase();
const colors: { [key: string]: (text: string) => string } = {
LOG: (text) => text,
ERR: red,
WAR: yellow,
INF: cyan,
};
const color = colors[type] || blue;
console.log(color(`${type} ${message.text()}`));
});
page.on("pageerror", ({ message }) => console.log(red(message)));
page.on(
"response",
(response) =>
!response.status().toString().startsWith("2") &&
!response.status().toString().startsWith("3") &&
console.log(green(`${response.status()} ${response.url()}`))
);
page.on("requestfailed", (request) =>
console.log(magenta(`${request.failure()?.errorText} ${request.url()}`))
);
await page.goto(process.argv[2]);
await new Promise((r) => setTimeout(r, 999999999));
await browser.close();
};
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment