Run JavaScript in webpage from CLI with Node.js and Socket.io

Dale Parlement
Dale Parlement used Ask the Experts™
on
Hello, I am trying to automate something at work. I cannot use the actual code because the company is picky about privacy. The example I show would be similar and easy for me to use it the same way. I have begun understanding something about using Socket.io. The information I read showed how to connect to a web socket and emit data to connected web sockets. I now realize what web sockets can do. I am aware of how to set up a server and create an index.js file that sets up a connection. My question is how via a command line (Windows 10) could I make a webpage run JavaScript. In the code I show for the index.html page how could it run the sayHello function from a command-line command sent to the page? Thanks

<!doctype html>
<html>
	<head>
		<meta charset="utf-8">
			<title>Untitled Document</title>
			<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.3.0/socket.io.dev.js"></script>	
			<script type="text/javascript">
			
				var h = 'Hello';
				
				var socket = io.connect('http://localhost:4561');
				
				function sayHello() {
					alert(h);
				}
						
			</script>
	</head>

	<body>
	</body>
</html>

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
leakim971Multitechnician
Top Expert 2014

Commented:
I think your misunderstand something
opening web page mean you get data (packets) from a webserver (service listening on port 80,443)

now you can get that data(packet) from command line using :
wget command (or any similar command available on linux,unix,windows,osx,...)
when you run this sayhello in a browser, the browser display an alert saying 'Hello'
what is your final goal ?

maybe you're looking for :
https://phantomjs.org/ or http://casperjs.org/
both can run javascript after download a page in a command line (so they run the sayhello javascript after downloading the packets representing the webpage)

Author

Commented:
Hi, thanks for the reply. I have been meaning to thank you and say hi, but work has been a zoo.  I understand what you explain about data (packets) and web server listening. I had successfully created a process where a program we rely on could be automated. This program can only be automated via web panels. So I used Node.js and Soket.io to create a connection to the web panel in the program. It worked flawlessly. Now in a browser, I only need to go to http://localhost:4000/ and it runs. I can refresh the page and it works. Now to automate I just want to do the same action of refreshing the page and Viola. However, I never imagined that doing that process would be so hard outside of a browser. ???(Lol) I tried using Phantom JS, but it is way too slow. I tried Wget, no luck. I get what is going on the page needs to be opened in the browser to render the JavaScript and other things. I just cannot believe there is no command-line way that simple and fast to do this. I want the command line to do it because I can automate it from the other programs we use with it. I wound up having to rig a macro program to a browser and send F5. But this way is kinda clunky :(. If anybody has any ideas on how this could be done please shout. It is funny when you search for command line stuff you always wind up buried in Linux stuff. Windows command line ideas are dying :(. This forum is great I like so much already :). Thanks again for the great reply. Dale...
leakim971Multitechnician
Top Expert 2014

Commented:
I'm pretty sur PhantomJS are faster than a browser.
Do you need the page content or just running the script? Maybe you can create (isolate) the JS file and run it with Casper/Phantom JS?
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
OK, I saw the Casper JS program. Does it do something different than Phantom JS? With Phantom JS I followed the Quick Start and it worked. However, there was a hang-time before completing. When I refresh the browser it is blink of an eye fast. Does (isolate) mean run only the JavaScript? Isn't that what the Quick Start does? Thanks
leakim971Multitechnician
Top Expert 2014

Commented:
Does (isolate) mean run only the JavaScript?

yes

Isn't that what the Quick Start does? Thanks

I was thinking you tested it opening your web page not running the JS code directly, well done !

there was a hang-time before completing. When I refresh the browser it is blink of an eye fast.

If you add the time you took opening your browser, I'm sure phantomjs is quicker.
maybe run it twice to see the real delay

var t1 = new Date();
// run your js
var t2 = new Date();
// run your js
var t3 = new Date();
console.log("execution time script 1 : "+(t2-t1));
console.log("execution time script 2 : "+(t3-t2));

Open in new window

Author

Commented:
What I wound up going with was a FileMaker web viewer. This stays open and can be refreshed and scraped for the data. This seems the best option for now. This is much faster than using Phantom JS. However, I do like the Phantom JS stuff a useful thing to know about. Thanks
leakim971Multitechnician
Top Expert 2014

Commented:
FileMaker web viewer run from CLI ?
I don't like the idea of letting a window session open with a GUI program running task :)
I'm not sure why you've delay with PhantomJS as I have no idea of what you're doing.
No worries, have a nice week-end, prepare for the rush of next week ;-)

Author

Commented:
Thank you for all the super help enjoy your weekend as well. Dale :)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial