Greasemonkeying around with IRCTC

[Update 1/21/2008: Updated the script to handle availability URL change by IRCTC developers. If you've already installed the script, uninstall it (Tools->Greasemonkey->ManageUserScripts, select AJAXAvailability and click Uninstall), and install the updated script from the same location (http://senthil.thecoder.googlepages.com/ajaxavailability.user.js)]

For those of you who have not heard about Greasemonkey, it is a Firefox extension that "allows you to customize the way a webpage displays using small bits of JavaScript.".  IRCTC (http://www.irctc.co.in/) is Indian Railways's website for booking tickets. While a huge improvement over standing in queue at railway stations, I would not consider it to be a well designed site - it doesn't stand up to heavy user load, and the interface is not very user friendly. For one, the HTML is absolutely nasty, with multiple <html> tags, multiple elements with the same id, etc.. For what I think is a bandwidth strapped site, the HTML has lots and lots of unnecessary whitespace and commented out HTML and it doesn't even use gzip encoding.

Enough ranting about that - Greasemonkey can't help there. What it can do is help manipulate the UI. My first target was the "Plan My Travel" page. After entering the source and destination stations, date of travel etc. and hitting "Find Trains", the page shows the list of trains that run between the stations on that day. So far, so good. Now, to book tickets on a train, the user selects a train and hits "Book Ticket". However, the user would typically have to check availability of tickets before proceeding to book them. The page makes it frustratingly difficult to do that - checking availability for a train takes two clicks, and worse, the availability information shows up on a new window, forcing the user to switch back and forth between the windows.

I decided to write a Greasemonkey script to show availability information right alongside the list of trains. I did not want to slow down loading of the page though, so I decided to do AJAX style dynamic loading of availability information. Here's how the page looks after enabling the script.

Screenshot after AJAXAvailability

Notice how the new column blends in seamlessly with the rest of the page.

For the technically inclined, the script works by first adding a column to the train details table. It uses a XPath expression to get the list of train numbers from the HTML content and then queries availability information from the server by generating the same query string that a click on the "Show Availability" button generates. The difference is that it does it automatically for all trains in the page and that it uses GM_xmlhttpRequest to do the query asynchronously - which means that the script queries availability for trains (almost) simultaneously.

From here, it's fairly simple - another XPath expression "screen scrapes" the availability string from the HTML content returned by the server and there is some code to update the cell's contents with the string. The script handles failures also - for HTTP failures, it retries the request and for other kind of failures, it shows a "Query Failed" link, which, when clicked, shows the actual error in a messagebox (another XPath expression takes care of scraping the error text).

To install the script, install Greasemonkey first (if you haven't already), navigate to http://senthil.thecoder.googlepages.com/ajaxavailability.user.js and click Install. The next time you visit IRCTC's website and go to the "Plan My Travel" page, the script gets loaded and runs automatically.

While you're at it, you might also find SortItOut, a script for sorting the availability table (shown above), useful.

Hope you guys find the scripts useful. 


Comments

# re: Greasemonkeying around with IRCTC

Tuesday, November 13, 2007 7:29 AM by Vivek Ragunathan

Excellent da. I have not used it yet. Will use it tomorrow. Good work.

# re: Greasemonkeying around with IRCTC

Tuesday, November 13, 2007 11:41 AM by Selva

Really an useful stuff dude...Keep up the good work

# re: Greasemonkeying around with IRCTC

Thursday, November 15, 2007 12:46 AM by Narasimha Rao Taduri

Excellent work!!

# re: Greasemonkeying around with IRCTC

Thursday, November 15, 2007 12:44 PM by prakash

cooooool dude...keep up the good work...:-)

# re: Greasemonkeying around with IRCTC

Friday, November 16, 2007 3:55 AM by Anu

this is simply great.. Long expected feature ! Thank you !

# re: Greasemonkeying around with IRCTC

Friday, November 16, 2007 4:00 AM by Aarbee

Awesome..Very Creative.....Good Work!

One thing, are we degrading the server performance by executing the query with many values which normally handles one? Any thoughts on this?

# re: Greasemonkeying around with IRCTC

Monday, November 19, 2007 12:13 AM by Ashok

Yes we are definitely. We do save a click but we end up overloading the site unnecesarily at times.

# re: Greasemonkeying around with IRCTC

Monday, November 19, 2007 12:26 AM by Mottai

This is a good one Sadhu...new advances in web technology are amazing Mainframe Dudes like me !!

# ?????????????????? - Gilli &raquo; Blog Archive &raquo; ??????&#8230;..

Friday, November 23, 2007 5:08 AM by ?????????????????? - Gilli » Blog Archive » ??????…..

Pingback from  ?????????????????? - Gilli  &raquo; Blog Archive   &raquo; ??????&#8230;..

# re: Greasemonkeying around with IRCTC

Friday, November 23, 2007 6:24 AM by Shankar Manickam

Gr8 work machi!

Cool one!

# re: Greasemonkeying around with IRCTC

Friday, November 23, 2007 8:37 AM by Gopinath Selvaraj

Hi, Could u please help me on how to activate the script in my firefox?

Thanks,

Gopinath Selvaraj.

# re: Greasemonkeying around with IRCTC

Tuesday, November 27, 2007 8:07 AM by Rahul

awesum addon!! v v useful...

# re: Greasemonkeying around with IRCTC

Tuesday, November 27, 2007 8:08 AM by Rahul

awesum addon!! v v useful...

# re: Greasemonkeying around with IRCTC

Wednesday, November 28, 2007 1:17 AM by Venkatramanan

Gopi

Install Greasemonkey  (addons.mozilla.org/.../16465) & just drag this js (after saving it as .js file locally) into the browser. It will automatically install

Hope this helps.

Regards

Venkatramanan

# re: Greasemonkeying around with IRCTC

Wednesday, November 28, 2007 4:55 AM by Subash S

Fantastic utility. Well done dude.

Hats-off to you

Continue your good work.

Thanks a ton.

Best Regards,

Subash S

# re: Greasemonkeying around with IRCTC

Wednesday, November 28, 2007 6:55 AM by Vijay

Hope the developers of IRCTC are looking at it.

Its a shame that a site like IRCTC (used all over India) has to be hacked to get the information out of it.

An excellent blog and a accessibilty awareness.

# re: Greasemonkeying around with IRCTC

Friday, December 07, 2007 5:11 AM by Vikram Ragunathan

Hi,

I tried to install the file but i got Microsoft JScript Runtime error. The error is "document is undefined" in line number 96. Help me out with this issue.

# re: Greasemonkeying around with IRCTC

Saturday, December 08, 2007 1:01 AM by Senthil

Did you try installing this in IE? This works only with Firefox.

# re: Greasemonkeying around with IRCTC

Saturday, December 08, 2007 8:13 AM by Saravanan

Super. As a author of few Greasemonkey  scripts, I am happy to see people develop scripts for Indian websites.

userscripts.org is the repository for Greasemonkey scripts. You can host your script in userscripts.org

# re: Greasemonkeying around with IRCTC

Thursday, December 13, 2007 11:06 PM by Muraly

Its really a Cool feature, mate.

# re: Greasemonkeying around with IRCTC

Thursday, December 13, 2007 11:33 PM by Manju

Hi ,

I unable to insatll your cool s/w , could help me how to do it.I click the link which your pointing but its sending soem error.

--Manju

# re: Greasemonkeying around with IRCTC

Thursday, December 13, 2007 11:46 PM by Narayana

Superb Macha...

# re: Greasemonkeying around with IRCTC

Friday, December 14, 2007 12:56 AM by vishwanath

thanks a ton senthil

really neat utility

# re: Greasemonkeying around with IRCTC

Friday, December 14, 2007 5:14 AM by Shriram

This is so cool !

Thanks a bunch for writing this.

# re: Greasemonkeying around with IRCTC

Friday, December 14, 2007 7:34 AM by S.Neemb

hey, this script is awesome!

i came across another gmonkey script for gmail (http://g-alert.blogspot.com/) and that was also nice.

but your script is the best!

# re: Greasemonkeying around with IRCTC

Friday, December 14, 2007 12:05 PM by Basu

Hi Sendhil ... amazing work !!! usability is of course the key differentiator .....

Hats off to ur idea !!!

# re: Greasemonkeying around with IRCTC

Sunday, December 16, 2007 11:37 PM by Vipul Saxena

Hats off to you dude its working absolutely fine.....

Please continue this good work.....

# re: Greasemonkeying around with IRCTC

Monday, December 24, 2007 2:59 AM by Gokul

Really useful stuff...Appreciate

# re: Greasemonkeying around with IRCTC

Tuesday, December 25, 2007 1:16 AM by Mani

This is really good. I like it very much.. Hats off to you..

Can we have something like this to sort the trains by departure times?

# More greasemonkeying with IRCTC

Wednesday, December 26, 2007 7:27 AM by the blog => anything goes

I&#39;m glad that a lot of people found AJAXAvailability useful. For people who haven&#39;t heard about

# re: Greasemonkeying around with IRCTC

Wednesday, December 26, 2007 7:44 AM by Senthil

Mani,

Nice idea, take a look at msmvps.com/.../more-greasemonkeying-with-irctc.aspx.

# re: Greasemonkeying around with IRCTC

Thursday, December 27, 2007 10:24 PM by Manoj Sharma

IRCTC should learn something from you, to implement these kinds of gr8 ideas.

Keep it Up !!

Manoj

# re: Greasemonkeying around with IRCTC

Friday, February 08, 2008 2:02 AM by Suirya

Very Good Effort. Keep up the good work

# re: Greasemonkeying around with IRCTC

Thursday, February 14, 2008 6:03 AM by Aravindakumar

Nice work

# re: Greasemonkeying around with IRCTC

Thursday, March 27, 2008 6:31 AM by Sreedhar

its very good

update and release new versions if you can

thanks

# re: Greasemonkeying around with IRCTC

Saturday, May 10, 2008 10:54 AM by Lalam

Excellent work done. Hats off. I would further like to see if all class & all quota availability on the same screen if possible.

# re: Greasemonkeying around with IRCTC

Tuesday, May 20, 2008 11:34 AM by Sunil

Senthil good work on the IRCTC stuff. Just a little help, pls excuse my ignorance. I did download the greasemonkey and the script, but could not install them. Basically the options were not available,I am using a Windows Vista with IE 7

# re: Greasemonkeying around with IRCTC

Tuesday, May 20, 2008 11:26 PM by Senthil

Sunil, the Greasemonkey is an extension to FireFox, an alternative web browser. I'm afraid it doesn't work with IE7.

# re: Greasemonkeying around with IRCTC

Monday, June 23, 2008 4:53 AM by Ram

Hi....

Someone else has copied your idea...Check this out....

viked.livejournal.com/28300.html

# re: Greasemonkeying around with IRCTC

Wednesday, August 20, 2008 1:05 AM by Jayanthe

Its Amazing effort. Let laloo knows about this...

# re: Greasemonkeying around with IRCTC

Tuesday, November 04, 2008 5:19 AM by toxic brain

awesome dude!!! u rock, thumbs up.

# re: Greasemonkeying around with IRCTC

Wednesday, November 26, 2008 6:07 AM by Dinesh Merani

You rock dude!!! awesome!!

Leave a Comment

(required) 
(required) 
(optional)
(required)