Greasemonkeying around with IRCTC

[Update 3/6/2010: 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://senthilthecoder.com/software/irctcscripts/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. 

 

81 thoughts on “Greasemonkeying around with IRCTC”

  1. 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?

  2. 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.

  3. 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.

  4. 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

  5. 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?

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

  7. 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

  8. 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

  9. 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).

  10. 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

  11. 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.

  12. 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.

  13. 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

  14. TOO GOOD ! LEARNT ABOUT IT FROM A FRIEND.. SIMPLY GREAT FOR STUPID GUYS LIKE ME WHO WANT TO KNOW EVERYTHING ABOUT RAILWAYS AND IRCTC !!!

  15. Wonderful ! You are rocking !
    One more suggestion , It will be better if all availability of all the classes are displayed in one go.
    Any way the current stuff is very useful

  16. Since, there are genuine concerns of potential over-loading of IRCTC server, isn’t it possible to have some alternative solution?

    Like, running the query part on a different site (indianrail.gov.in) to get the status availability and then go ahead with booking on IRCTC.

    I’m no coding geek, so don’t know the feasibility.
    but is this can be done will be very useful.

  17. hi i am subhas i have a softwere whic can make fast irctc tatkal bookin and amke ur payment fast the cast is 5000 per system

    cantact on 9918576572

  18. 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
    raajticketers@yahoo.in

  19. 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

    jaintravels78@yahoo.in

Leave a Reply

Your email address will not be published. Required fields are marked *