$ headless -c "
console.log('yolo');
window.close(1);
"
yolo
exit status 1
I want to run js in a headless browser from bash - e.g. to automate running my clientside js tests. There’s a lot of js test runners and libraries for controlling headless browsers out there already. But those are huge and the devtools protocol provides us with all we need:
- Page.navigate + Page.frameStoppedLoading to open a page containing the script to run as well as some setup code
- Runtime.ExceptionThrown notifies us of uncaught exceptions
- Runtime.addBinding + Runtime.bindingCalled + Runtime.evaluate with a sprinkle of reflection to bridge the gap between between go and js