Is possible to establish a connection between a browser and a CMD/PowerShell process at browser launch?

I have been looking for a means of running external script/programs against events on a web page in a browser such as “On button Click” or “On Page change”.

I am able to easily do this with the Dev Tools found in browsers. For example, in the console, I can override the default behaviour of myImage with this:

myImage.onclick = function() {
	alert('Hello World')

This is exactly what I need, but I have since then learnt that its ‘officially’ not possible to run scripts/programs belonging to the host machine. This seems to a be core security feature of JavaScript/Web browsers.

I am now looking for alternative means of doing the above

Is it possible to start a ‘browsing session’ at the terminal, something like ‘test mode’ or ‘debugging mode’, so that it is connected to the shell that launched it, this way I can write my code in the Dev Tools console:

myImage.onclick = function() {
	logTerminal('Button has been clicked!')     //possibly a custom function

and when the event triggers, the browser outputs a message to the terminal its connected to, Button has been clicked!. and I am free to respond to it at the terminal.

Searching around, I came across Selenium but its feature set seems to be limited (I may be wrong) compared to what I can do at the Dev Tools console. Selenium seems to be based on polling, where the Dev Tools are event driven. I am looking for an event driven solution.

Note: I am NOT looking to deploy this as a solution for a live website, I am merely looking to, only on my local machine, “hook” into user events, like click, on sites such as the BBC/Nasa, so I can efficiently run my own tools at the right time, rather relying on polling.

Any help would be greatly appreciated!

Please explain your actual goal. What is it you are trying to do exactly?

I’m talking about the exact functionality needed, not theoretical.

Yes, this a core security feature, you absolutely cannot do this directly.

The normal way to do something similar to what you appear to want is to run a server, then use the HTTP APIs in the browser to send messages to/from that server.

This isn’t trivial to do from scratch, particularly if you have little experience.

Something like Selenium is doing the opposite to what I think you want: it is just using the browser JS API to control web page interactions.

As @lasjorg says, would help to know exactly what you want to do (I think it is possible that using a browser is not actually what you want)

1 Like

You are mixing local and remote here.

We really need to know what it is you are trying to do to give a proper answer.

  • A browser extension can interact with websites and add functionality. It should be possible to use it in concert with native applications but I don’t know enough about it (extension docs nativeMessaging).

  • Something like Selenium/Puppeteer/Playwright lets you interact with websites and perform actions based on conditions. They are mostly used for web scraping and site testing but it is possible to run external commands/applications as well.

  • A NodeJS server can respond to API calls from clients, run commands on the server, and send back a response to the clients.

  • NodeJS using something like Electron lets you create desktop applications written in JS.

That “Native Messaging” was exactly what I need and more. Using MDN extension example (hosted on Github) I was able to reproduce my own version.

Its really clever, using stdout and stdin I can even communicate back to the browser/extension.

Amazing. Thank you.

I was just looking for a solution that is akin to windows “shell hook” system but for the web browser/pages.
So, that like in Windows, if some event I subscribed to happens (on a webpage), my program will be informed and I can call my own function to do something that I want.

So far all the solutions I could manage was “polling” I needed an “event” based solution, which I found here. Cheers.

Great, good to hear you found something that works for you.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.