using command line in javascript - 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.

Related

Why don't web workers work?

So, I'm trying to use a web worker in my project to run a long-running process that is currently tying up the UI. I've been to I don't know how many sites trying to get a worker to work, but to no avail.
All of my javascript is kept in separate files and referenced in the HTML file. As a test to get my feet wet, I created a test.js file and put the following code in it:
self.addEventListener('message', function(e) {
self.postMessage('return');},false);
Then, in the UI page's javascript file I placed this code in a function triggered by a button click event:
var w = new Worker('test.js');
w.addEventListener('message',function(e){
alert(e.data);},false);
w.postMessage('hi');
The code is derived from:
html5rocks.com/en/tutorials/workers/basics
Other websites I visited provided similar instructions on how to set up a worker.
For the life of me, I cannot get this to work. When I execute it does absolutely nothing and I seemingly get no errors. Stepping through the code, it appears to create the worker, but I don't see any evidence of the event listener being created and the 'postMessage' event doesn't do anything. I've tried IE11 and Chrome with the same results.
In my research, I came across a part of Chrome's developer tools that revealed the test.js file couldn't be found. Yet, the file is in the same folder as the page's js file. So, I tried adding in the relative directory information as I do in the page's HTML section. That didn't work either.
I then found claims that for security reasons you couldn't have one js file reference another js in the code. It's unclear whether this is a Chrome-only feature or part of some spec.
So, now I'm in a quandary. The worker requires a reference to a separate js file for the code to be executed, yet, the browser isn't allowed to reference another file? How is the worker supposed to work if you aren't allowed to do what it requires to work?
To now, I've successfully pissed away two days trying to get this one seemingly simple function to work. To say I'm mildly frustrated would be an understatement. Being a fairly novice programmer and not understanding every last little nuance about web programming I'm clearly missing a key part of this whole thing.
How the heck is one supposed to make web workers work?
Turns out browsers won't allow local files to be fetched via javascript. Because that means a website can read your personal files! So you need to develop and test your project using a web server. The easiest way to do this for me was to install:
docker-compose
and make sure it works. Then create a file named:
docker-compose.yml
inside root folder of my project with index.html file. Then put this inside the docker-compose.yml file:
version: '3'
services:
nginx:
image: nginx:alpine
volumes:
- .:/usr/share/nginx/html
ports:
- "80:80"
Then inside the root folder of my project run:
docker-compose up
And then in the browser go to:
http://localhost/
And it worked!
I appear to have found a solution, though it escapes me why.
If I use:
var w = new Worker('js\test.js');
the worker doesn't work.
But, if I use:
var w = new Worker('js/test.js');
the worker does work.
I characteristically use the back slash throughout the project to delineate paths without issue. Why the forward slash must be used to set the worker's file location is a mystery. I have seen nothing in any documentation that even remotely addresses that tiny, yet seemingly critical detail.
Thank you, Mr. Starke, for your help!

casperjs --web-security=no --cookies-file=/tmp/mycookies.txt myscript.js did not working

Hi for me the command did not working:
$ casperjs --web-security=no --cookies-file=/tmp/mycookies.txt myscript.js
(just copied from the documentation of casperjs: http://docs.casperjs.org/en/latest/cli.html#casperjs-native-options)
I have created the cookie file from another script. Now I want to implement the cookies into the second script. But the above command did not work.
What I want, is to implement the cookies before the first page invoke.
And yes the cookies are still alive, bcause if I use this in the second script:
var fs = require('fs');
phantom.cookies = JSON.parse(fs.read("pathToTheCookies"));
it works. But I want to do it with the command line, because I want to give the path with arguments.
Thanks.
OS: Windows 10
Best regards.
rikku47
I got it. My solution for Windows (my OS Windows 10) is a command line like this:
casperjs --cookies-file="""pathIncludeWhiteSpaces""" """scriptPathIncludeWhitspaces""" """argument1""" """argument2"""
In the arguments you can put paths too. Just wrape them with six quotes
"""AnyArgumentAndOrPath"""
and it should work. the official solution from the documentation did not work for me, if I use cookies.
With other word if I use this command:
casperjs --cookies-file=\"pathIncludeWhiteSpaces\" \"scriptPathIncludeWhitspaces\" \"argument1\" \"argument2\"
it work for the first time to write the cokkie file. But if I let it read, it do nothing (it looks like).
Here is the part of the documentation:
Hint
You may need to wrap an option containing a space with escaped double
quotes in Windows. –foo=\”space bar\”
http://docs.casperjs.org/en/latest/cli.html#casperjs-native-options
Best regards rikku47

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+

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

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

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