Execute browser page/javascript from a script/command-line - javascript

Hope this isnt a stupid question.
I have recently had an idea about something which I am very curious about.
I am a fan of Node.js (not really relevent here I think) and the V8 engine but I was wondering if its possible to run a browser (get it to execute JS) but INTERNALLY.
What I mean by that is to create a program (possibly using the V8 engine) which can open a page (as if in the browser) and execute its javascript.
For instance say I have the below file hosted on www.mysite.co.uk/home.php
<!DOCTYPE html>
<html>
<head>
<script>
function myFunction()
{
//javascript AJAX call to www.mysite.co.uk/ping.php
}
myFunction();
</script>
</head>
<body>
</body>
</html>
And ping.php looks something like:
<?php
//connect mysql, database ping and table ping
//it is a single column table with integer value starting on 0
//increment by 1 and update the table
Say I wanted to get the Javascript to execute by using some sort of script on my command line/linux box (essentially WITHOUT using a browser).
So something like:
./mybrowser http://www.mysite.co.uk/home.php
or even:
./mybrowser home.php
I feel like it should be possible as the V8 (or different JS engine) should technically be able to execute Javascript but I havnt the foggiest how it could do so out of a browser context (or even if its possible).
Any ideas?

You can use any js engine to run js scripts as long as they do not rely on the DOM.
You could start by looking at:
Running V8 Javascript Engine Standalone
Edit: as I understand you want a headless browser, here are some:
HTMLUnit (I use that one for unit testing)
PhantomJS
Zombie.js

Running JavaScript on the command line by using either Rhino for Java or Windows Script Host.
http://www.mozilla.org/rhino/
http://msdn.microsoft.com/en-us/library/9bbdkx3k%28VS.85%29.aspx

Related

ReferenceError: document is not defined (atom ide) [duplicate]

I am learning JavaScript and I am using Atom (Text Editor).
On my HTML file I got only this:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="style.css">
<script src="script.js"></script>
</head>
<body>
<h1>Hello Plunker!</h1>
<button id="displayTodosButton">Display Todos</button>
<button>Toggle Todos</button>
</body>
</html>
On my javascript file, I am simply trying to access the "Display todos" button using this:
var displayTodosButton = document.getElementById('displayTodosButton')
I was watching a video, and the instructor is using plnkr.co, and he accesses the button just fine, yet on Atom I get the "ReferenceError: document is not defined"
How can I fix this?
yet on Atom I get
If you really mean that Atom, your text editor, is highlighting it and showing you a warning that document is undefined, it's just that Atom doesn't realize you're running that code in a browser context where document will be defined.
It probably has a setting where you can tell it that you'll be running the code in a browser, so it can assume the default set of globals (window, document, etc.).
If the code in script.js is just what you've shown, although the error Atom is showing you won't be a problem (because in the browser, document will not be undefined), you'll get null back from getElementById because your code runs before the element exists. Again, this is assuming that code is on its own, not (say) inside a DOMContentLoaded handler or similar.
Unless you have a good reason to do it (and there aren't many), putting script elements in the head is an anti-pattern. Put them in body, right at the end, just prior to the closing </body> tag. That way, any elements defined above them will have been created by the browser before your code runs.
You have hit some menu option or key combination which is trying to execute the JS file using Node.js.
Your code, however, is designed to run, embedded in a web page, using the APIs supplied by web browsers.
Web browsers, under those circumstances, will provide a document object. Node.js will not.
You need to open the HTML document in a web browser. The open in browser extension might be useful.
You can see any error reports using the Developer Tools that every major browser supplies.
(NB: The first error you will then encounter is explained by this question and answer).
It looks like you are trying to run the JS code with the "script" package in atom (which is in a NodeJS context). What you actually want to do, is to run it in your web browser. So just open index.html in your favorite browser and see the magic :)

I keep getting a reference error saying the "document" is not defined [duplicate]

I am learning JavaScript and I am using Atom (Text Editor).
On my HTML file I got only this:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="style.css">
<script src="script.js"></script>
</head>
<body>
<h1>Hello Plunker!</h1>
<button id="displayTodosButton">Display Todos</button>
<button>Toggle Todos</button>
</body>
</html>
On my javascript file, I am simply trying to access the "Display todos" button using this:
var displayTodosButton = document.getElementById('displayTodosButton')
I was watching a video, and the instructor is using plnkr.co, and he accesses the button just fine, yet on Atom I get the "ReferenceError: document is not defined"
How can I fix this?
yet on Atom I get
If you really mean that Atom, your text editor, is highlighting it and showing you a warning that document is undefined, it's just that Atom doesn't realize you're running that code in a browser context where document will be defined.
It probably has a setting where you can tell it that you'll be running the code in a browser, so it can assume the default set of globals (window, document, etc.).
If the code in script.js is just what you've shown, although the error Atom is showing you won't be a problem (because in the browser, document will not be undefined), you'll get null back from getElementById because your code runs before the element exists. Again, this is assuming that code is on its own, not (say) inside a DOMContentLoaded handler or similar.
Unless you have a good reason to do it (and there aren't many), putting script elements in the head is an anti-pattern. Put them in body, right at the end, just prior to the closing </body> tag. That way, any elements defined above them will have been created by the browser before your code runs.
You have hit some menu option or key combination which is trying to execute the JS file using Node.js.
Your code, however, is designed to run, embedded in a web page, using the APIs supplied by web browsers.
Web browsers, under those circumstances, will provide a document object. Node.js will not.
You need to open the HTML document in a web browser. The open in browser extension might be useful.
You can see any error reports using the Developer Tools that every major browser supplies.
(NB: The first error you will then encounter is explained by this question and answer).
It looks like you are trying to run the JS code with the "script" package in atom (which is in a NodeJS context). What you actually want to do, is to run it in your web browser. So just open index.html in your favorite browser and see the magic :)

using command line in javascript

I know there are lots of questions about this already, but i cant seem to find one that works for me.
I am trying to launch a local file from a local html using cmd to pass command to launch file but it does not seem to work.
This is what i used so far:
<script type="text/javascript" language="javascript">
function RunFile() {
window.open('C:/Windows/System32/cmd.exe /c START %temp%/file.cpl');
}
</script>
someone pls help with this.
Lets just asume i can do this on IE window.open('C:/Windows/System32/cmd.exe); and it will open cmd.
My question is how do i pass some extra argument to make the cmd open my file from another location e.g. window.open('C:/Windows/System32/cmd.exe /c START %temp%/file.cpl');
You cannot run a program using a browser. You might be confused with Windows Scripting JScript, check: https://en.wikipedia.org/wiki/JScript
You may run apps using that (in windows shell). Check this: https://stackoverflow.com/a/15351708/1082061
You could do this using server-side binary execution on Nodejs using child_process.
Pro: Easy to use, just need a simple AJAX call to trigger execution from client to Node server.
Cons: Need to use a server instead of a single HTML page.

SSH with Javascript without GUI

What I want to make is a Operating System based on Ubuntu which will use the web
I want to make the items clickable. If you click on an app-icon, the application will open. I tried to use WebSockets, but they are'nt that easy to use.
I tried PHP, with exec(), popen(), system() and I tried ssh2 functions. Doesn't work or too slow.
I can't use a GUI like shellinabox, because I only want to connect to localhost and run some commands like 'firefox' or 'sensible-browser' or like 'gedit'. That's why I want help.
I googled the whole day and found nothing. I'm searching for a simple solution. Just a connection and just some commands. No extra GUI, just that simple things!
Thanks,
Amanush.
-------------------------------------------[SECOND QUESTION (EDIT)]------------
I made my own protocol and it's working well!
One last question. My html:
<html>
<body>
Open firefox
</body>
</html>
My .desktop file:
[Desktop Entry]
Encoding=UTF-8
Version=1.0
Type=Application
Terminal=false
Exec=/usr/bin/cloudjerun -c gedit
Name[en_US]=Gedit
Comment[en_US]=Small, easy-to-use program to access iTunesU media
Name=TunesViewer
Comment=Small, easy-to-use program to access iTunesU media
Icon=/usr/share/icons/hicolor/scalable/apps/tunesview.svg
Categories=Application;Network;
MimeType=x-scheme-handler/cloudje;
Comment[en_US.utf8]=Small, easy-to-use program to access iTunesU media
Tutorial: http://jarrpa.net/2011/10/28/creating-custom-url-handlers-in-ubuntu-11-04-11-10-gnome-3-0/
Ok, it's always executing gedit. The reason is the line Exec=/usr/bin/cloudjerun -c gedit.
That's cool, but I want to run firefox as well, with 'cloudje:firefox' in the HTML-file. How can I replace -'-c gedit' with '-c firefox', '-c skype' or '-c sensible-browser', automaticly?
I suggest you register a custom protocol handler for your OS on the machine.
ie.: mysweetos://launchapp/chromium
you can find info about this with a quick google of "registering a custom protocol handler linux"
You would have to write some software/script on the linux machine to receive this request and execute the required application.
This looks very similar, and could be a significant part of what you are trying to accomplish. Technical details here, looks like it requires GTK3.2+

Running a batch file using JavaScript in web browser

I'm having a nightmare here, please help.
Here is my JavaScript:
<script language="javascript">
function MyCmd();
var shell = new ActiveXObject("Shell.Application");
var appExe = #"D:/ping.bat";
shell.ShellExecute(appExe , "", "", "open", "1");
</script>
I call this function from inside a <td> in a table..
<button style="width:relative; height:65" onClick="MyCmd()"><b>Netstat</b></button>
All I want is to see the batch file running. Content of the batch file is: netstat > ping.bat, and it's located on d:\. Any ideas?
The code you supplied can only be made to run in Internet Explorer when set to lowest security as well as when UAC is turned off or an OS without UAC is used.
If you intended to get this running on the server, it's probably possible but a completly other question.
Echoing what others have said: this is a terrible idea. Even if you have created something harmless, how is the browser supposed to know that?
I cannot imagine why this is such an emergency. You need to take a step back and think about other ways to do what you want. If you're trying to create a program to execute a batch file, a webpage is a totally inappropriate place for that program.

Categories