Greasemonkeying around with IRCTC

[Update 12/19/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://senthilthecoder.com/software/irctcscripts/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!!

# re: Greasemonkeying around with IRCTC

Friday, December 12, 2008 8:40 AM by Suresh

awesome! great job!

# re: Greasemonkeying around with IRCTC

Wednesday, December 17, 2008 9:14 AM by Viral Shah

Sir thanks for a excellent script

one suggestions

If possible can we have all class availability detail together like SL/3A/2A/CC/2S etc (if s/w not slow down)

when tatkal option selected source & destination station automatically show in small window on clicking of train no or name or like that

# re: Greasemonkeying around with IRCTC

Wednesday, December 17, 2008 9:23 AM by Viral Shah

Help me yaar, some trouble

availability for all train shows same figure start to end

# re: Greasemonkeying around with IRCTC

Wednesday, December 17, 2008 10:40 PM by jigar metha

hey buddy tre seems some problem, now a days its not showing availability properly. kindly check n mail if possible.

jigudamehat@yahoo.com

# re: Greasemonkeying around with IRCTC

Friday, December 19, 2008 1:28 AM by Ramanathan

it is not working for the past 3 days. it always says 'query failed' or shows the position of first listed train to all other trains.

here is a error screen shot:

e.imagehost.org/.../error.jpg

# re: Greasemonkeying around with IRCTC

Wednesday, January 07, 2009 6:00 AM by R. Raja

Sir,

Thank you very much for your wonderful efforts.  Sir, a small clarification or request, is there any more java script to list all the classes also like the availability column.  IF so please inform me (heberlink@yahoo.com).

# re: Greasemonkeying around with IRCTC

Friday, January 09, 2009 8:30 AM by dharmesh

amazing

one thing can also we see all quota in one line or not pls try for it

# re: Greasemonkeying around with IRCTC

Monday, March 23, 2009 11:04 PM by harmain

hi well grease monkey is working very good

but it have side effect it not working on internet explorer why if working so how i don't know please will you help me

# re: Greasemonkeying around with IRCTC

Sunday, April 12, 2009 10:10 PM by shani mahadeva

yo macha ... Senthil Rocks

# re: Greasemonkeying around with IRCTC

Wednesday, April 15, 2009 12:44 PM by Ashok

Great man! I wanted to do this for a long time! But never really sat down to do this! Thanks...

# re: Greasemonkeying around with IRCTC

Friday, April 17, 2009 11:51 AM by Vidhya shankar

I love you.

May you live forever!

# re: Greasemonkeying around with IRCTC

Thursday, April 30, 2009 5:00 AM by Bis

thomascook did this before. with indian rail

# re: Greasemonkeying around with IRCTC

Monday, May 11, 2009 12:08 PM by Harvinder Singh mp

Does Greasemonkey works with IE7

# re: Greasemonkeying around with IRCTC

Tuesday, May 12, 2009 12:21 AM by Balasubramaniam

Thanks. Very Useful stuff!!!

# re: Greasemonkeying around with IRCTC

Thursday, June 04, 2009 3:57 PM by Venkata

Hi Senthil,

I'm trying to write a script for Firefox using greasymonkey. I got some questions regarding editing a script, can you take a look at those.

1) Do we need to have a mapping between the data entered in greasemonkey and the .user.js 's userscript block.

2) Can I make changes to already available user script & make it run? (if yes, can you please provide me the steps to do it)

Thanks in Advance.

Venkata Jamithireddy.

# re: Greasemonkeying around with IRCTC

Friday, July 17, 2009 7:38 AM by Sreedhar

Thanks for the nice script

Keep posting

# re: Greasemonkeying around with IRCTC

Thursday, October 08, 2009 4:29 AM by Ramesh

This used to work, but not now. Can you verify if the script works currently in the current IRCTC portal?

Thanks for the awesome script, BTW.

# re: Greasemonkeying around with IRCTC

Saturday, October 10, 2009 12:26 AM by Ramesh

I used the updated script and now it works :-)

# re: Greasemonkeying around with IRCTC

Wednesday, October 14, 2009 2:26 AM by AQUIL

Hi Mr kumar,

this in regards some situation that i face while

in the peak season, common error is "service unavailable"

i normally make ticket using quick book  option ,which gives me option to feed detail before 8 am

i have some trick to maintain session, and making few changes hitting submit at 8 am 10 seconds and payment gateway takes 10 to 30 seconds to  open ,processing of payment takes 25 seconds .some time payment gateways are so much loaded  that takes more than 1 minute to open,than all of sudden i get "service unavailable"

so handle this error   right click and reload ..

hence again i lose 30 seconds here .eventually

i end up with waiting list ...

here i would like to know how i can speed up the payment process and how i can avoid error "service unavailable".I'm looking ahead to share detail and in process take

info_aaquil@yahoo.co.in

regards Aquil

# re: Greasemonkeying around with IRCTC

Saturday, November 14, 2009 8:41 AM by Karthick

Dear Senthil:

I am having some doubts in your greasemonkeying-around-with-irctc blog. If you don't mine, can you give me your emailid or mobile no please.

my emailid: karthickreddyk@gmail.com

Leave a Comment

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