Is there any way to run JavaScript for an open browser tab remotely? For example if there is a tab open in a browser and I’d like to run a script in its developer console by piping the script to it from the shell. The idea being to develop the script from the comfort of my preferred editor and run it in the browser to test.
(Preferably, each run would be run in isolation such that multiple evaluations of const someVar = 1
wouldn’t conflict (as a const
can’t be redefined).)
2
Answers
For Chromium based browsers, one can run JavaScript remotely in debugger mode:
chromium --remote-debugging-port=9222
curl -s -o - http://127.0.0.1:9222/json
Runtime.evaluate
:In the last step, the important thing is to send a correctly escaped JavaScript as string in JSON object, I'm using
jq
andwebsocat
to achieve that, but other tools will do as well.There are lots of ways to control computers remotely, such as through SSH, VNC, TeamViewer, NoMachine, etc. All of those, except SSH, allow graphical control, so you can click/type into a browser remotely with them.
If instead you want to allow users to expose their computer to you, you could always write a browser plug-in, and it could make
fetch
requests to your server to get code. However, browser plug-ins do have some security restrictions that may limit you, depending on what exactly you want to accomplish.In particular, there are limits to what a plug-in can do with the dev console. I know some manipulation is possible, as there are plug-ins like the React Developer Tools which interact with the browser dev tools … but you’d have to research more to see exactly what the limits are. Likewise, there are also limits to what plug-ins can do to the filesystem, but I believe plug-ins can affect it (with user permission).