My favorites | Sign in
jsh
Project Home Downloads Wiki Issues Source
READ-ONLY: This project has been archived. For more information see this post.
Search
for
Tutorial  

Featured, Phase-Support
Updated May 17, 2012 by AncientA...@gmail.com

Getting Started

Download the jsh.7z or jsh.zip package and unpack, and you will see the following directory structure.

jsh
│ JSH.js
│ JSH.hta
│ index.htm
│ panel.htm
│ Blank_HTML.htm
│ Blank_HTML4.htm
├─bin
│   System.js
│   WebAutomation.js
├─cxx
│   main.cpp
│   main.exe
│   makefile
│   resource.js
│   resources.rc
└─lib
    AES.js
    Base64.js
    Intermezzo.js
    LZW.js
    Prelude.js
    Tartarus.js
    URI.js
    UTF16.js
    UTF8.js

Now you need to decide in which environment you would like to run jsh, the options are browser, console, HTA, as explained below.
After you launch jsh interactively, you can start by playing with the shell.

>>> 1
1
>>> _ + _
2
>>> _ + _
4
>>> Tartarus.print("Hello, JSH!")
Hello, JSH!
>>> dir(Tartarus)
['chainload', 'load', 'print']
>>> load('lib/URI.js')
>>> dir(URI)
['GetCurrentURI', 'NormalizeURI', 'ParseURI']
>>> load('lib/LZW.js')
>>> LZW.Compress('aaaaaaaaaaaaaaaaaaaaaaaaaaa').length
8
>>> add(1, 2, 3, 4, 5)
15
>>> add.apply(null, range(101))
5050
>>> comp(add)(range(101))
5050
>>> inc = curry(add, 1)
<lambda/0>
>>> inc(5)
6
>>> range(10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> map(inc, _)
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> exit()

Once you are familiar with jsh, you can start by reading advanced topics such as script injection, alternatively you may dive into the API Reference.

Run in browser

The simple way is to open index.htm locally in your favorite browser and play with it. However, in order to use script injection and many network related features, you may want to host the entire jsh folder on a web server.
Once you are done, you will see something similar as illustrated below.
Now there are three panels, click the Runtime Library link, which is the last item in the left panel, and you will see a prompt at the right panel as illustrated, now you can type in javascript expressions and statements and see the result.
Three meta commands are supported, .cls would clear the output, .history would show the command history, and .wrap would toggle the line wrapping mode.
You may also use the up and down arrow key to navigate through the command history.
If you are using a high DPI monitor, you may change the font size of the output using __builtins__.Environment.StdOut.style.fontSize = '18px', choose the appropriate typeface and size will make you happy and more effective :)


Needle (bookmark) can be saved as a bookmark and used on other web pages, this is done by injecting javascript code to current web page. Note that when index.htm is opened locally, Needle (bookmark) may not work due to browser security policy.
If you are running locally and using Internet Explorer, the browser may show an information bar asking if the active content should be executed depending on your browser security policy.

Run in Windows HTA

If your operating system is Windows based, you may start directly by double clicking the JSH.hta file icon, this would bring you an HTA based shell.

Run in Windows console

If your operating system is Windows based, you can run JSH.js in console using cscript.exe /nologo <JSH.js file path>, for example:

cscript.exe /nologo D:\WWW\JSH.js

In order to run JSH.js without having to type cscript.exe /nologo each time, you can change the default scripting host by the following command:

wscript.exe //H:CScript

This would allow you to run JSH.js directly either by double clicking the JSH.js file icon, or by typing JSH.js directly as illustrated below.
(if you are too lazy to type in the .js, you may add .js to the PATHEXT environment variable, then you just need to type the three letter JSH...)

Advanced Topic

Script injection

Script injection can be achieved in two ways.
The first way is to deploy jsh on a web server, bookmark the Needle (bookmark) and run it on targeted web pages.
The second way is only available for Trident on Windows platform, by using the main.exe application (C++ source available), this would give you the ability to inject script to Trident engine based applications, such like Windows SDK Help, MSN Live Messenger, Outlook Express, etc.

Chain load (browser and Windows HTA)

Take Firebug Lite as an example, from the official site we can get the bookmark for firebug lite injection. However, this bookmark does not work for FRAME and IFRAME, neither does it support HTA and things like Windows Live Messenger.
Once we have jsh injected, jsh will allow you to evaluate expressions and execute statements, and thus makes it possible to load libraries or any other third party scripts. For this case we will firstly check the bookmark for Firebug Lite, and extract the statement:

>>> /* copy & paste the firebug lite bookmark into the double quote as follows */
>>> firebug_lite_bookmark = "javascript:(function(...){...})(...);";
>>> /* decode and evaluate */
>>> eval(decodeURIComponent(firebug_lite_bookmark.slice(11)));
>>> /* now wait a few seconds for firebug lite to load */
>>> /* enjoy :) */

Firebug is relatively large and have more features than jsh, jsh can be used as a boot loader for none browser applications such like Live Messenger and Windows Help. With Prelude.infect, it's even possible to support chain loading Firebug Lite into nested FRAME and IFRAME.

Taking arguments (Windows HTA and console)

Create a file named hello.js as follows

var print = Tartarus.print;

print("Hello, world!");
print(Prelude.format('Arguments: %r', __builtins__.Environment.Arguments));
print(Prelude.format('LibraryPath: %s', __builtins__.Environment.LibraryPath));
print(Prelude.format('StartupDirectory: %s', __builtins__.Environment.StartupDirectory));

Then run JSH.js hello.js a b c under command prompt to see the result.
You may also run JSH.hta hello.js a b c to see the result in HTA window.

Returning error code (Windows console)

Create a file named exit.js as follows, and run JSH.js exit.js.

Prelude.infect();
infect(globals(), Tartarus);

print('check if exit code in %errorlevel% is 3');
exit(3);
print('unreachable code');

Windows console stdin/stdout redirection

If you are facing problems while trying to use JSH for stdin/stdout redirection, consult this link http://support.microsoft.com/kb/321788.
For example, if we want to create a script for automating HTTP request, we can create a script named HttpClient.js

Tartarus.load(
	__builtins__.Runtime('lib/Intermezzo.js'),
function(){
	var method = 'GET', URL, xmlHttpReq = Intermezzo.XMLHttpRequest();
	switch(__builtins__.Environment.Arguments.length){
	case 2:
		URL = __builtins__.Environment.Arguments[1];
		break;
	case 3:
		method = __builtins__.Environment.Arguments[1];
		URL = __builtins__.Environment.Arguments[2];
		break;
	default:
		Tartarus.print(Prelude.format('Usage: %s [GET | POST] <URL>' , __builtins__.Environment.Arguments[0]));
		exit();
	}
	xmlHttpReq.open(method, URL, false);
	xmlHttpReq.send(method === 'POST' ? __builtins__.Environment.StdIn.ReadAll() : undefined);
	Tartarus.print(xmlHttpReq.responseText);
});

Then it is possible to use command like:
JSH.js HttpClient.js POST http://google.com < postdata.txt
JSH.js HttpClient.js GET http://google.com > result.txt
echo hello | JSH.js HttpClient.js POST http://google.com

Internet Explorer Automation

You can create a script named JshWeb.js for automating Internet Explorer, and run JSH.hta JshWeb.js or JSH.js JshWeb.js.

if(this.__builtins__ && (__builtins__.Engine === 'CScript' || __builtins__.Engine === 'HTA')){
	Tartarus.load(
		__builtins__.Runtime('bin/WebAutomation.js'),
		__builtins__.Runtime('lib/Intermezzo.js'),
	function(){
		var browser = WebAutomation.InternetExplorer('about:');
		browser.navigate('about:blank');
		WebAutomation.Wait(browser);
		Prelude.map(function(module){
			browser.Document.parentWindow.execScript(module.OpenAsTextStream().ReadAll());
		}, [
			System.GetFile(__builtins__.Runtime('lib/Tartarus.js')),
			System.GetFile(__builtins__.Runtime('lib/Prelude.js')),
			System.GetFile(__builtins__.Runtime('lib/Intermezzo.js')),
			System.GetFile(__builtins__.Runtime('lib/URI.js')),
			System.GetFile(__builtins__.Runtime('JSH.js'))
		]);
	});
}
Comment by rubelr...@gmail.com, Jun 26, 2014

Tartarus.print("Hello, JSH!")

Powered by Google Project Hosting