When Bun v1.3.12 was released last week, they subtly included a powerful feature, almost under the radar: WebView. It's like Playwright/Puppeteer but more limited, especially in terms of documentation.
I built a demo app based on it called html-getter which you can use like this:
bun run src/html-getter.ts https://www.peterbe.com
That will open https://www.peterbe.com with Chrome or Chromium or WebKit and evaluate the following JavaScript expression: document.documentElement.outerHTML (which is the whole HTML source of the whole DOM) and print this to stdout.
or you can use it like this:
bun run src/html-getter.ts --body --screenshot /tmp/page.png https://www.peterbe.com
which means it prints everything inside the <body>...</body> instead and at the same time, it dumps a screenshot of what it looks like, which can be useful for debugging.
What's cool about this is that you no longer need to install playwright or puppeteer. Scraping HTML is just one of many applications you can use WebView for. I wouldn't use it for automated headless browser testing though because the documentation, tooling, and APIs for Playwright is still so much much better.
WARNING
At the time of writing this, I am unable, on macOS, to compile this Bun script into a binary. When I do, this happens:
$ bun run build
$ bun build src/html-getter.ts --target=bun --outfile out/html-getter --compile --bytecode
[14ms] bundle 1 modules
[77ms] compile out/html-getter
$ ./out/html-getter https://www.peterbe.com
[1] 70304 killed ./out/html-getter https://www.peterbe.com
The same worked on my x64 Linux server, but to get this to compile on macOS might need some more careful thought.
Comments