Javascript Console for Windows like JSC [closed] - javascript

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 6 years ago.
Improve this question
I have to work on the Windows platform for a project, and I happen to do that project with server-side javascript (yes, I am actually using server-side javascript). Most of my analysis and research I do by working with JSC on my Mac, which allows me to write code and get back a response from the interpreter and load .js files. Now I have been googling and I find most of the results about Firebug or online tools. However, I am looking for something more commandlinish.
Does anyone have a good recommendation for a JavaScript interpreter/console application for the Windows platform that does not require a browser and can be run from the commandline (and supports the loading of external .js files) ?
Many thanks,

node.js has a Windows version. The installer is completely non-invasive, all it does is copies files to Program Files and adds the path to your system $PATH env. variable. If you then start node.exe without parameters it works as a REPL Javascript console.
You can load a .js file like this:
> .load ./file/to/myscript.js
Check out REPL page in Node.js manual for more info.

If you don't want to install any external tools, you can use Windows built in cscript.exe and a short script to read/eval/print/loop as follows:
try {
throw {};
} catch(repl) {
while (repl.line != '.exit') {
if (repl.line) {
repl.err = null;
try {
repl.out = eval('(' + repl.line + ')');
} catch (e) {
if (e instanceof SyntaxError) {
try {
repl.out = eval(repl.line);
} catch (e) {
repl.err = e;
}
} else {
repl.err = e;
}
}
if (repl.err) {
WScript.stdout.writeLine('Error: ' + repl.err.message);
} else {
WScript.stdout.writeLine(repl.out == null ? String(repl.out) : (typeof repl.out.toString == 'function' ? repl.out.toString() : Object.prototype.toString.call(repl.out)));
}
}
WScript.stdout.write('> ');
repl.line = WScript.stdin.readLine();
}
}
Save that as repl.js and run cscript repl.js to get a console similar to jsc.

if your java runtime >=1.5 ,you can try jrunscript. It is based on java scriptengines, JavaScript is the default language supported.

Not exactly equivalent to JSC, but still..
I use the browser's development tools (F12), and there I enter the Console tab. That's it.
Works with modern browsers, where Firefox usually needs Firebug for this.
The main advantage is to have an interactive interpreter, without the need to load a real file.

http://www.phpied.com/javascript-shell-scripting/ -> this might help your cause.
Rhino Shell - https://developer.mozilla.org/en/Rhino_Shell

Apart from Node.js, JSDB is quite nice. I checked node.js again and I believe it is now available as a single executable, node.exe, which is even more compact than JSDB. So I might be using that instead.

Just check out DeskJS (https://deskjs.wordpress.com). It's exactly what you're looking for! It's a portable Windows console application that lets you run pure JavaScript code and even load any existing JS files. It supports even the basic JS popup boxes implemented in browsers. You can save your commands as JS files that can be run on startup or by dragging-and-dropping them on the app. Plus there's so much more to it like you can create a build system for Sublime Text that can run JS files via cmd, it supports themes and snippets e.t.c...

Related

Setting development environment flag and eliminating debug code when deploying to production

When working with web applications I often have code that is specifically designed for production/server vs local environments.
I also often need to deactivate specific functions (ie js, ruby, rails, java etc).
The problem I'm facing is that sometimes when releasing new version I miss to activate or inactivate these features.
The ideal thing (I think) would be to have like on/off function that you run before deploying to a server. So you don't miss anything.
Example.
I comment out a function when developing.
JS:
// foobar() FOR PRODUCTION
Ruby On Rails (production.rb):
# config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # FOR PRODUCTION
The latter, is when I need to test something local with production settings but because I don't have specific environment installed (nginx, appache) I can't use it's features.
I use Sublime Text 2,3 and it would be great if it had a feature defining specific functions/code blocks to be uncommented/commented.
How do you work so you won't miss anything?
This question is not specific to Sublime Text at all, or any text editor, but a generic question how to preprocess and debug (JavaScript) source code.
First, the easiest approach would be have some kind of global debug flag in your codebase.
window.DEBUG = true;
function log(a, b, c, d) {
if(DEBUG) {
console.log(a, b, c, d);
}
}
if(DEBUG) {
// Do dev stuff
} else {
// Do produection stuff
}
log("foobar") // Does not need to be wrapper, as log() itself is functional only in debug mode
You can set the debug flag depending on your URL:
if(window.location.href.indexOf("localhost") >= 0) {
window.DEBUG = true;
}
When you deploy your JavaScript to production, you usually run it through minimizing and bundling tool. One popular tool is UglifyJS 2 which features constant elimination. When running and deploying your JavaScript code with this tool, if(DEBUG) { } parts of your code gets eliminated.
You can connect any tool to Sublime Text using Project > Build scripts.
For the server-side code, you simply need to set debug flag depending on your environment. Each framework (Ruby on rails) have their own way of doing this, and you need to ask specific details in framework specific question.

Workflow/tools for frontend javascript development/test and production?

A Web frontend javascript application, in production, will have little to nothing "exposed", that is, declared as a global var or attached to window.
all code minimized and appended to one (or a few) js file(s);
little to nothing "exposed", that is, declared as a global var or attached to window.
During development, though, life is easier if the code is readable, split in several files, and also if prototypes and relevant instances are accessible from console.
To be more clear (leaving aside minimization, which is easy obtained with a number of different tools), in production I'll have something like:
(function() {
var Greeter = function() {
};
Greeter.prototype.msg = function() {
return 'Hello, world!';
};
Greeter.prototype.greet = function() {
console.log(this.msg());
};
new Greeter().greet();
}());
This way, my code will do its stuff without exposing anything: neither the Greeter object nor its instances are accessible to other code.
(Of course this is just one of many ways to accomplish this, but that's not the point of the question).
Debugging this code, though, is hard, and unit testing is impossible.
In order to allow both debugging and testing, I would usually attach both Greeter and its instance to the window object, or to some other object.
So, during development, I'll use something like:
(function() {
var Greeter = function() {
};
Greeter.prototype.msg = function() {
return 'Hello, world!';
};
Greeter.prototype.greet = function() {
console.log(this.msg());
};
window.Greeter = Greeter;
window.greeter = new Greeter();
window.greeter.greet();
}());
This way I'll be able to unit test Greeter, and also to interrogate it from the browser's console to check its status.
Is there a tool, or a set of tools, or some different way to organize my code, so that it's possible to pass from the development version to the production one (which will also be minimized)?
There is no single package or executable you can install that will get you 100% of the way there. You will need to combine an editor, command line tools and your browser to create an effective web application / javascript development environment.
3.18.13: Added a link for Sublime Web Inspector. Debug Javascript inside of Sublime Text! http://sokolovstas.github.com/SublimeWebInspector/
Editor
Things to look for: Plugin system, system highlighting, linting, autocomplete. If you are using an editor today that supports plugins your best bet is to stick with it and setup linting and syntax highlighting. If you are looking for some recommendations all of the following are solid choices.
Sublime Text 2 (free trial)
Textmate (commercial, 30 day trial)
VIM (free)
Webstorm (commercial, 30 day trial)
Workflow Tools:
I recommend starting with a high level toolset like Yeoman or lineman. They are somewhat opinionated but provide a complete workflow and will allow you to get stuff done quickly. Once you are comfortable with using it you can peek under the covers and pick and customize it to your needs.
Yeoman : Provides scaffolding, package management, dev server, concatenate & minify and will run specs
Lineman: Dev server, concatenate & minify, run specs
Grunt: More low level (used by both Yeoman and Lineman). Similar to ruby's rake
VCS: Not to be overlooked, a good command line based VCS is essential. I recommend Git, again use what you are comfortable with to start.
Browser:
The development tools in the browser will provide you with a console, and debugging tool. Spend some time researching and really getting to know how to use the development tools provided in the browser. They are extremely powerful.
Webkit browser (Chrome or Safari): Built in Developer Tools (Command option J).
Firefox + firebug
Browser testing: Highly recommend browserstack for cross browser testing.

WScript.Sleep() ~ WSScript is undefined

I'm trying to follow some example code from microsofts mdn site..
var WshShell = new ActiveXObject("WScript.Shell");
var oExec = WshShell.Exec("calc");
while (oExec.Status == 0)
{
WScript.Sleep(100);
}
WScript.Echo(oExec.Status);
I'm currently writing a javascript/JScript to run a few batch commands. Everything works fine if I don't use WScript.Sleep(). However, If i try to use it, to prevent locking up the browser, i'm getting an error that WScript is not defined.
I figured that I needed to define it myself. However, I have been searching mdn website all day with no luck. This is my first time using any "windows only" products I could be coompletly overlooking something.
If you want to see the documentation I'm looking through it is located here.
http://msdn.microsoft.com/en-us/library/ateytk4a(v=vs.85).aspx
Any help is greatly appreciated.
Thanks,
Freddy
WScript is an object that is defined when the Javascript is run within the Windows Script Host, aka WSH. The object is not available within the Javascript engine in a web browser.
If you are really trying to produce "batch like" files, then you don't need a browser, and I'd say you probably don't want a browser. You can write your code into a .js file and just run it from the cmd.exe prompt or Explorer window with a double-click.
Change the script language from JavaScript to JScript

console-like interface on a web page using javascript [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 5 years ago.
Improve this question
I very like MySQLs mysql cli tool and I don't like phpMyAdmin.
[IMHO]It's a nice thing for a Windows user, but its not so good when you've used to console.[/IMHO].
What I want is to build a web page containing element with console-like input (for example something like this) which should get input from user, send it to PHP script on back-end and show back-end response.
Back-end script is done (it was the easiest part), but I can't find any library for JavaScript implementing console-like input.
I've tried to examine and modify for my needs example I've provided, but it's too bloated (because doesn't use any libraries) and implements specific thing. Also I would like this element to provide some auto-completion for input.
Any ideas on such JS library?
I think you are looking for this: jQueryTerminal
there is shellinabox - javascript terminal.
EDIT:
There is also library xterm.js that's real terminal emulator.
EDIT 2:
My jQuery Terminal library is useful when you need custom behavior and you can write your code in JS or as backend code, but backend need to be simple input -> output, if you want to run for instance interactive backend commands, like vi or emacs, you need proper tty, for this use xterm.js (or implement that in JavaScript) for any other usage jQuery Terminal is better. It have lot of features and you don't need to run process on the server (listen on a port) which usually is forbidden on shared hostings or GitHub pages.
instead of using console.log() use document.write()
It will write text on the webpage just like console.log would in the console
I've made a console library called Simple Console (I'll probably rename it because simple-console is taken on npm)
It handles command history and such for you, and you can use it to implement any kind of console.
var handleCommand = (command)=> {
var req = new XMLHttpRequest();
req.addEventListener("load", ()=> {
con.log(req.responseText);
// TODO: use con.error for errors and con.warn for warnings
// TODO: maybe log a table element to display rows of data
});
// TODO: actually pass the command to the server
req.open("GET", "mysql.php");
req.send();
};
var con = new SimpleConsole({
handleCommand,
placeholder: "Enter MySQL queries",
storageID: "mysql-console"
});
document.body.appendChild(con.element);
Check out the documentation on GitHub for more information.
hmm firebug console ?
http://getfirebug.com/commandline

Tutorial for using JavaScript on a Desktop

I need to do some scripts in java script.
I am working on it but couldn't find a few solutions to a few problems.
First of all I need a GOOD tutorial, but not for an internet page but for a DESKTOP script.
Things couldn't find out like :
1) I wanted a simple message box in order to debug my program, I used:
var name = prompt("What is your name","Type Name Here");
When running it I get error of "Object expected"
2) Couldn't find how to open a file
Based on your comments, I guess that you are attempting to run a JavaScript file directly on Windows. Double-clicking on a .js file in windows will (probably) run it in Windows Script Host.
The prompt() function will not work this way, since WSH provides a completely different API than browser-embedded engines.
The following code should accomplish your intentions. However if you want anything more than a simple popup, HTAs are the only way to do complex GUIs with JScript on the desktop.
var fso, ws, ts;
fso = new ActiveXObject('Scripting.FileSystemObject');
ws = WScript.CreateObject('WScript.Shell');
var ForWriting= 2;
ts = fso.OpenTextFile('foo.txt', ForWriting, true);
ts.WriteLine(new Date().getTime());
ts.Close();
ws.Popup('Wrote to file!');
var ForReading= 1;
ts = fso.OpenTextFile('foo.txt', ForReading, false);
var fileContents = ts.ReadLine();
ts.Close();
ws.Popup('The file contained: ' + fileContents);
WScript.Quit();
I have to ask: why is JavaScript the right tool for the job? Why not use a scripting language intended to be used this way, such as Python, Ruby, Lua, ... etc?
If you are using Microsoft's JScript (and it sounds like you are), look to the MSDN web site for help. The page here looks fairly good. Google can also help with that.
Assuming you don't mind using Java, you could also use the Mozilla Rhino shell. But it doesn't look like there is a standard way of reading from the console in JavaScript. (presumably since this is not something typically required in a JavaScript application...) The built in JavaScript functions in the shell seem fairly basic, but you can read a file.
There area also examples of using Rhino, which may be helpful. You can interface with the Java API to do whatever else you need to do.
Edit: I wrote this answer a long time ago; today I would use node.js. See their learning page.
The latest prerelease of Opera acts as a runtime for JS applications.
They have tutorials describing how to use it.
I used: var name = prompt("What is your name","Type Name Here");
When running it I get error of "Object expected"
Presumably your runtime doesn't implement prompt that in a way that is compatible with those arguments.
2) Couldn't find how to open a file
This depends on the runtime you use. JS itself doesn't have anything built in to read files (or display a prompt). You need an environment that provides those objects.

Categories

Resources