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

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?

Comments

# Greasemonkeying around with IRCTC - the blog => anything goes

Wednesday, December 26, 2007 8:08 AM by Greasemonkeying around with IRCTC - the blog => anything goes

Pingback from  Greasemonkeying around with IRCTC - the blog => anything goes

# re: More greasemonkeying with IRCTC

Saturday, July 12, 2008 11:09 AM by M K Batra, Dehradun

Really - Fantastic !

Keep it up ! Dude

# re: More greasemonkeying with IRCTC

Wednesday, September 10, 2008 12:16 PM by DHARMESH

ITS A GOOD SOFTWARE WHICH SAVES TIME BUT I AM UNABLE TO INSTALL IT PLEASE GIVE SOME IDEA TO INSTALL IT

# re: More greasemonkeying with IRCTC

Friday, December 12, 2008 3:06 AM by Jigar Shah

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 ?

Thanks,

Dream Cyber Zone

DSS Technologies.

# re: More greasemonkeying with IRCTC

Sunday, January 25, 2009 1:08 AM by Manoj Sharma

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?

# re: More greasemonkeying with IRCTC

Tuesday, February 24, 2009 3:28 AM by Subash

Cool add-on... Saved my time. Works seamless :) kudos

# re: More greasemonkeying with IRCTC

Thursday, March 12, 2009 11:41 AM by suri

Great work. Thanks for the application.

# re: More greasemonkeying with IRCTC

Tuesday, December 15, 2009 7:16 AM by jay shah

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

# fast TATKAL BOOKING with IRCTC

Tuesday, January 12, 2010 11:52 AM by kiran singh

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 .

kiran8642003@yahoo.co.in

# re: More greasemonkeying with IRCTC

Saturday, May 29, 2010 2:45 AM by Hemal shah

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

# re: More greasemonkeying with IRCTC

Friday, July 09, 2010 7:18 AM by Mani

vvvvvvvvvvvvvvery good

# re: More greasemonkeying with IRCTC

Sunday, July 11, 2010 10:22 PM by kunal desai

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

# re: More greasemonkeying with IRCTC

Monday, October 18, 2010 2:40 AM by selvakumraan

Hi Senthil,

          the IRCTC website is updated. pls update the Scirpt as well as. Thanks in advance

# re: More greasemonkeying with IRCTC

Tuesday, November 23, 2010 5:20 AM by aa

How To install and use it

Leave a Comment

(required) 
(required) 
(optional)
(required) 
If you can't read this number refresh your screen
Enter the numbers above: