More greasemonkeying with IRCTC

I’m glad that a lot of people found AJAXAvailability useful. For people who haven’t heard about it, AJAXAvailability is a Greasemonkey script for IRCTC’s website that loads availability information asynchronously and shows it in the availability table, right alongside the list of trains (more information here).

Mani suggested that the ability to sort the availability table based on departure time would be useful. That seemed like a nice idea, so here it is – SortItOut, a Greasemonkey script
that modifies the availability table and makes the header texts clickable.
Clicking on one of the headers, say “Departure time”, sorts the table by the
values in that column, in ascending order.Greasemonkey script to do that.

 Ascending sort screenshot

Clicking on the same header again will sort the table in descending order. 

You can sort by any column, except the one with radio buttons (“Select”). This script also works nicely with AJAXAvailability, in that it allows you to sort the table even when availability information is loading (unless you are sorting the table by availability information, of course). 

To install the script, install Greasemonkey first (if you haven’t already), navigate to SortItOut
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

That’s all, folks – the rest of the post is about the technical details of how the script works, feel free to skip it if you’re not interested.

The script works by getting the availability table’s DOM object through an XPath expression and replacing the cells of the first row with dynamically created anchor elements. The click event of those elements are hooked up to a function that then sorts the table, based on the column’s values. The sort function is pretty simple – it creates an array of objects, one object per row. Each object holds a reference to it’s row and a reference to the value of the column in that row, on which the sort must be done.

The script then uses the Array.sort library function to do the sorting and then rearranges the rows in the table based on the sorted result. There are quite a few ways of rearranging the table –

1. Replace the existing HTML in each row with the HTML of the row to-be

2. Remove all rows in the table and create new rows and cells, based on the sorted output.

3. Use Greasemonkey’s insertBefore to switch around “TR” DOM objects.

The first approach wouldn’t work with AJAXAvailability. That script stores references to “TD” DOM elements, for updating availability information in the AJAX callback. Replacing the innerHTML of the table’s rows would create new TD objects, breaking that script. There was another subtle problem when I tried doing this – my sorted array and the actual table hold references to the same TR DOM objects, so replacing the HTML in the table would also affect the sorted output.

The second approach will not work for the same reason – new TD elements will be created, and the AJAXAvailability script will update the now obsolete TD elements.

The third approach works because no new objects are created, existing TR objects are shunted around, based on the sorted output. This was also the simplest to code as well. Doesn’t it feel great, when the best solution is the simplest and cleanest one as well?

13 thoughts on “More greasemonkeying with IRCTC

  1. It’s really gr8.

    I can show availibility directly to my client. and they can get direct decision.

    Also tell me if is there anybody know that i have install it in MOZILA FIREFOX, but will it be possible in Google Chrome, and Internet Exploer ? if yes, then how ?


    Dream Cyber Zone
    DSS Technologies.

  2. I had used this script in Firefox, I love this script of showing availabity in a glance. But now I had renewed my Digital certificate I am not able to load that certificate in Mozilla Firefox. So i am missing this script very much in Internet explorer. So tell me how can either I install Digital certificate in Firefox?

  3. hey senthil kumar u hv done great work for ours and i am thankful to u.but if u can make the script for quickbook along with payment in one click it will great helpful for all

  4. if u think u good work with irctc then find out how tatkal ticket book at exact time 08:00:00 whenever before this time u cannot entered in irctc for tatkal ticket booking,when he form fillup, when he send payment.then how is this possible? if u find any answer to this question then call me on 09377726794,kiran singh, from ahmedabad,gujrat. if u find this solution i will pay u monthly 20,000/- twenty thousand fixed salary. somebody have get succeed in this .

  5. Thanks, It’s really working & thanks for uploading this useful software………….

    It’s really good & I request you that pls try for TATKAL fast booking script on time.

    Hemal Shah – Ahmedabad

  6. sir, your script is very useful to me, but sir your fans will be delighted if you help them to synchronize irctc clock with pc clock

Leave a Reply

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