Windows Script Host as JSON shell
When hosted by your server, the web_send library turns your Firebug console into a command line for your server-side application. The same code can also be run by the Windows Script Host, a standard part of all Windows releases since Windows 98. Doing so has two big advantages:
- no files need to be uploaded to your web server,
- you are not restricted by the Same Origin Policy.
As a result, you have a command line for all JSON resources on the Web. The world is at your fingertips!
Installation
All of the required files are in the same subversion
repository used for web_send. Just double-click the jsh.lnk
file to launch the shell.
Using the shell
Launching the shell opens a rather plain looking DOS command shell with a
';
' prompt in the top left corner. At this prompt, you can enter
any JavaScript to be eval()
'd. The computed value is output as a
JSON string, prefixed by '//
'. Both the lib.Q
and lib.web
libraries have been loaded and are in the global scope, along with
ADSAFE
and JSON
; so you can enter commands just as
you do in the Firebug console. For example:
; 1 + 1 // 2 ; var factory = lib.web._ref(null, 'https://vsci.hpl.hp.com/-/7fuxka/#s=yuwsw2qt3bde7b') ; var drum = lib.Q.post(factory, 'makeDrum', []) ; var hits = lib.Q.get(drum, 'hits') ; hits // 0 ; lib.Q.post(drum, 'bang', [ 1 ]) ; hits = lib.Q.get(drum, 'hits') ; hits // 1
Multi-line expressions
To enter a multi-line JavaScript expression, just follow the JSLint coding style. In particular, the shell assumes that any line that ends with an open bracket, binary operator, comma, or semi-colon will be followed by another line. For example:
; lib.Q.post(drum, 'bang', [ 2 ]) ; hits = lib.Q.get(drum, 'hits') ; lib.Q.when(hits, function (value) { ADSAFE.log('hits = ' + value); }, function (reason) { ADSAFE.log('failed: ' + reason); }) ; // hits = 3
If the shell expects a continuation of the previous line, it outputs a two
space indent; otherwise, it outputs the ';
' prompt. Together with
the '//
' prefix for output, these conventions mean you can copy
and paste the entire screen to replay a scripting session.
Examples
Now that you're free from the Same Origin Policy, try out what the Web has to offer.
Google AJAX Search API
Google offers a JSON-enabled public search API. Using this service from a web_send shell is a breeze. For example:
; var search = lib.web._ref(null, 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0') ; var results = lib.Q.get(search, 'Paris Hilton') ; var feelingLucky = lib.Q.when(results, function (value) { return value.responseData.results[0].url; }) ; feelingLucky // "http://en.wikipedia.org/wiki/Paris_Hilton"
Yahoo! Query Language
Yahoo offers a JSON-enabled query engine for structured data on the Web. A wide array of data sources can be accessed via the YQL service. For example:
; var yql = lib.web._ref(null, 'http://query.yahooapis.com/v1/public/yql', { format: 'json' }) ; var sf = lib.Q.get(yql, 'select * from geo.places where text="san francisco, ca"') ; var x = lib.Q.when(sf, function (value) { return value.query.results.place.centroid; }) ; x // { // "latitude": "37.77916", // "longitude": "-122.420067" // }
These queries can sometimes take a little longer, so you may have to check
the value of 'x
' a few times before the promise resolves.
Twitter provides JSON-enabled access to user comments. For example:
; var twitter = lib.web._ref(null, 'http://search.twitter.com/search.json') ; var prez = lib.Q.get(twitter, 'from:BarackObama') ; var lastWord = lib.Q.when(prez, function (value) { return value.results[0].text; }) ; lastWord // "Join Organizing for America to fight for real health care reform. Host or at tend a Health Care Organizing Kickoff: http://bit.ly/cSMlF #OFA"
This one can take a while to resolve (when it comes back at all ;).
Flickr
Flickr offers a JSON-enabled services API. Unfortunately, an API key is required for even the simplest of queries. The key will be granted in real time though. For example:
; var flickrKey = // your flickr API key here ; var flickr = lib.web._ref(null, 'http://www.flickr.com/services/rest/') ; var lingLing = lib.web._ref(flickr, '', { api_key: flickrKey, format: 'json', nojsoncallback: 1, method: 'flickr.panda.getPhotos', panda_name: 'ling ling' }) ; var luckyTitle = lib.Q.when(lib.Q.get(lingLing), function (value) { return value.photos.photo[0].title; }) ; luckyTitle // "jam"