<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://msmvps.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>the blog =&gt; anything goes : IRCTC AJAX Javascript</title><link>http://msmvps.com/blogs/senthil/archive/tags/IRCTC+AJAX+Javascript/default.aspx</link><description>Tags: IRCTC AJAX Javascript</description><dc:language>en</dc:language><generator>CommunityServer 2008.5 SP2 (Build: 40407.4157)</generator><item><title>More greasemonkeying with IRCTC</title><link>http://msmvps.com/blogs/senthil/archive/2007/12/26/more-greasemonkeying-with-irctc.aspx</link><pubDate>Wed, 26 Dec 2007 08:09:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1420043</guid><dc:creator>Senthil</dc:creator><slash:comments>7</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/senthil/rsscomments.aspx?PostID=1420043</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/senthil/commentapi.aspx?PostID=1420043</wfw:comment><comments>http://msmvps.com/blogs/senthil/archive/2007/12/26/more-greasemonkeying-with-irctc.aspx#comments</comments><description>&lt;p&gt;I&amp;#39;m glad that a lot of people found &lt;a href="http://msmvps.com/blogs/senthil/archive/2007/11/10/greasemonkeying-around-with-irctc.aspx" target="_blank"&gt;AJAXAvailability&lt;/a&gt; useful. For people who haven&amp;#39;t heard about it, AJAXAvailability is a Greasemonkey script for IRCTC&amp;#39;s website that loads availability information asynchronously and shows it in the availability table, right alongside the list of trains (more information &lt;a href="http://msmvps.com/blogs/senthil/archive/2007/11/10/greasemonkeying-around-with-irctc.aspx"&gt;here&lt;/a&gt;).&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;a href="http://msmvps.com/blogs/senthil/archive/2007/11/10/greasemonkeying-around-with-irctc.aspx#1417733" target="_blank"&gt;Mani&lt;/a&gt; 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 - &lt;a href="http://senthil.thecoder.googlepages.com/sortitout.user.js"&gt;SortItOut&lt;/a&gt;, a Greasemonkey script
that modifies the availability table and makes the header texts clickable.
Clicking on one of the headers, say &amp;quot;Departure time&amp;quot;, sorts the table by the
values in that column, in ascending order.Greasemonkey script to do that.&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;img src="http://msmvps.com/blogs/senthil/ascendingsort.png" alt="Ascending sort screenshot" align="absmiddle" border="0" height="342" hspace="" width="518" /&gt;&lt;/p&gt;&lt;p&gt;Clicking on the same header again will sort the table in descending order.&amp;nbsp;&lt;/p&gt;&lt;p&gt;&lt;img src="http://msmvps.com/blogs/senthil/descendingsort.png" alt="" align="absmiddle" border="" height="344" hspace="" width="518" /&gt; &lt;br /&gt;&lt;/p&gt;&lt;p&gt;You can sort by any column, except the one with radio buttons (&amp;quot;Select&amp;quot;). 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).&amp;nbsp;&lt;/p&gt;&lt;p&gt;To install the script, &lt;a href="http://www.google.co.in/url?sa=t&amp;amp;ct=res&amp;amp;cd=1&amp;amp;url=https%3A%2F%2Faddons.mozilla.org%2Fen-US%2Ffirefox%2Faddon%2F748&amp;amp;ei=OL41R4LENaa6swLDtrijCw&amp;amp;usg=AFQjCNGwTfL-iNiyO_EdM51X1x7LgP1rEg&amp;amp;sig2=ks4n6Am8DHeoJTVzTfEFFQ" title="Install Greasemonkey"&gt;install Greasemonkey&lt;/a&gt; first (if you haven&amp;#39;t already), navigate to &lt;a href="http://senthil.thecoder.googlepages.com/sortitout.user.js"&gt;SortItOut&lt;/a&gt;&lt;a href="http://senthil.thecoder.googlepages.com/ajaxavailability.user.js" title="AJAXAvailability.user.js" target="_blank"&gt;&lt;/a&gt;
and click Install. The next time you visit IRCTC&amp;#39;s website and go to
the &amp;quot;Plan My Travel&amp;quot; page, the script gets loaded and runs
automatically. &lt;br /&gt;&lt;/p&gt;&lt;p&gt;That&amp;#39;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&amp;#39;re not interested.&lt;br /&gt;
&lt;/p&gt;&lt;hr /&gt;
&lt;p&gt;The script works by getting the availability table&amp;#39;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&amp;#39;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&amp;#39;s row and a reference to the value of the column in that row, on which the sort must be done.&lt;/p&gt;&lt;p&gt;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 -&lt;/p&gt;&lt;p&gt;1. Replace the existing HTML in each row with the HTML of the row to-be&lt;/p&gt;&lt;p&gt;2. Remove all rows in the table and create new rows and cells, based on the sorted output.&lt;/p&gt;&lt;p&gt;3. Use Greasemonkey&amp;#39;s insertBefore to switch around &amp;quot;TR&amp;quot; DOM objects.&lt;/p&gt;&lt;p&gt;The first approach wouldn&amp;#39;t work with AJAXAvailability. That script stores references to &amp;quot;TD&amp;quot; DOM elements, for updating availability information in the AJAX callback. Replacing the innerHTML of the table&amp;#39;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. &lt;/p&gt;&lt;p&gt;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.&lt;br /&gt;&lt;/p&gt;&lt;p&gt;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&amp;#39;t it feel great, when the best solution is the simplest and cleanest one as well?&lt;br /&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1420043" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/senthil/archive/tags/IRCTC+AJAX+Javascript/default.aspx">IRCTC AJAX Javascript</category></item><item><title>Greasemonkeying around with IRCTC</title><link>http://msmvps.com/blogs/senthil/archive/2007/11/10/greasemonkeying-around-with-irctc.aspx</link><pubDate>Sat, 10 Nov 2007 13:26:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1293793</guid><dc:creator>Senthil</dc:creator><slash:comments>62</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/senthil/rsscomments.aspx?PostID=1293793</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/senthil/commentapi.aspx?PostID=1293793</wfw:comment><comments>http://msmvps.com/blogs/senthil/archive/2007/11/10/greasemonkeying-around-with-irctc.aspx#comments</comments><description>&lt;p&gt;[&lt;b&gt;Update&lt;/b&gt; 12/19/2008: Updated the script to handle availability URL change by IRCTC developers. If you&amp;#39;ve already installed the script, uninstall it (Tools-&amp;gt;Greasemonkey-&amp;gt;ManageUserScripts, select AJAXAvailability and click Uninstall), and install the updated script from the same location (&lt;a target="_blank" href="http://senthilthecoder.com/software/irctcscripts/ajaxavailability.user.js" title="AJAXAvailability.user.js"&gt;http://senthilthecoder.com/software/irctcscripts/ajaxavailability.user.js&lt;/a&gt;)] &lt;/p&gt;
&lt;p&gt;For those of you who have not heard about Greasemonkey, it is a Firefox extension that &amp;quot;allows you to customize the way a webpage displays using small bits of JavaScript.&amp;quot;.&amp;nbsp; IRCTC (http://www.irctc.co.in/) is Indian Railways&amp;#39;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&amp;#39;t stand up to heavy user load, and the interface is not very user friendly. For one, the HTML is absolutely nasty, with multiple &amp;lt;html&amp;gt; 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&amp;#39;t even use gzip encoding. &lt;/p&gt;
&lt;p&gt;Enough ranting about that - Greasemonkey can&amp;#39;t help there. What it can do is help manipulate the UI. My first target was the &amp;quot;Plan My Travel&amp;quot; page. After entering the source and destination stations, date of travel etc. and hitting &amp;quot;Find Trains&amp;quot;, 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 &amp;quot;Book Ticket&amp;quot;. 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 &lt;b&gt;a&lt;/b&gt; 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. &lt;/p&gt;
&lt;p&gt;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&amp;#39;s how the page looks after enabling the script.&lt;/p&gt;
&lt;p&gt;&lt;img height="329" width="520" src="http://msmvps.com/blogs/senthil/AJAXAvailability.PNG" align="middle" alt="Screenshot after AJAXAvailability" /&gt; &lt;/p&gt;
&lt;p&gt;Notice how the new column blends in seamlessly with the rest of the page.&lt;/p&gt;
&lt;p&gt;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 &amp;quot;Show Availability&amp;quot; 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. &lt;/p&gt;
&lt;p&gt;From here, it&amp;#39;s fairly simple - another XPath expression &amp;quot;screen scrapes&amp;quot; the availability string from the HTML content returned by the server and there is some code to update the cell&amp;#39;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 &amp;quot;Query Failed&amp;quot; link, which, when clicked, shows the actual error in a messagebox (another XPath expression takes care of scraping the error text). &lt;/p&gt;
&lt;p&gt;To install the script, &lt;a href="http://www.google.co.in/url?sa=t&amp;amp;ct=res&amp;amp;cd=1&amp;amp;url=https%3A%2F%2Faddons.mozilla.org%2Fen-US%2Ffirefox%2Faddon%2F748&amp;amp;ei=OL41R4LENaa6swLDtrijCw&amp;amp;usg=AFQjCNGwTfL-iNiyO_EdM51X1x7LgP1rEg&amp;amp;sig2=ks4n6Am8DHeoJTVzTfEFFQ" title="Install Greasemonkey"&gt;install Greasemonkey&lt;/a&gt; first (if you haven&amp;#39;t already), navigate to &lt;a target="_blank" href="http://senthil.thecoder.googlepages.com/ajaxavailability.user.js" title="AJAXAvailability.user.js"&gt;http://senthil.thecoder.googlepages.com/ajaxavailability.user.js&lt;/a&gt; and click Install. The next time you visit IRCTC&amp;#39;s website and go to the &amp;quot;Plan My Travel&amp;quot; page, the script gets loaded and runs automatically.&lt;/p&gt;
&lt;p&gt;While you&amp;#39;re at it, you might also find &lt;a target="_blank" href="http://msmvps.com/blogs/senthil/archive/2007/12/26/more-greasemonkeying-with-irctc.aspx"&gt;SortItOut&lt;/a&gt;, a script for sorting the availability table (shown above), useful. &lt;/p&gt;
&lt;p&gt;Hope you guys find the scripts useful.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1293793" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/senthil/archive/tags/IRCTC+AJAX+Javascript/default.aspx">IRCTC AJAX Javascript</category></item></channel></rss>