Greasemonkey Saves US Consulate Site From Sucking

I wrote my first GreaseMonkey script today.

Following Andrew Pollock's E-3 Visa application advice I tried to book a visit to the US Consulate today. Unfortunately the date selection popup has some truly horrible javascript in it.

I doubt anyone would own up to this gumpf but if someone at the US Consulate feels they want to protect this, just contact me.

if (window.CalendarSelect)
function CalDay(year,month,day) {
  CalendarDay = day;
  CalendarMonth = month;
  CalendarYear = year;
  changeDay(CalendarYear,CalendarMonth,CalendarDay);  
}

function changeDay(year,month,day) {
  opener.year = CalendarYear + '';
  opener.month = CalendarMonth + '';
  opener.day = CalendarDay + '';
  opener.restart();
  self.close();
}

Not sure what window.CalendarSelect is supposed to do or why they test for it as it's not used anywhere. The HTML calls CalDay, passing the year, month and day you selected from the calendar. All fair enough.

Then the fun starts. CalDay uses the passed values to set three global variables and then calls changeDay.

changeDay ignores the passed values and instead uses the global variables...

<cue music>
Enter GreaseMonkey

I've been meaning to migrate to Firefox from Mozilla Real Soon Now®. This proved to be the last straw. I knew vaguely of GreaseMonkey's dark and powerful voodoo. I suspected it's chaotic powers could save my poor soul from IE's unholy taint.

yeah, yeah, enough melodrama. One aptitude install firefox later and then I had GreaseMonkey installed in no time. After tinkering a bit to get an initial user script installed and outputting some debugging code, I caved and read some documentation. Eventually I'd worked out enough to get something useful happening and voila!

// ==UserScript==
// @name          Fix US Consulate booking calendar
// @namespace     http://chaosengine.net/userscripts
// @description   Works around some IE specific javascript
// @include       http://210.177.22.41/*/calendar.asp
// ==/UserScript==
// Notes:
//  The code on that page is just weird, not to mention pointless.
//  This user script will probably break whenever they update their site

unsafeWindow['CalDay'] = function CalDay(year,month,day) {
  unsafeWindow['CalendarDay'] = day;
  unsafeWindow['CalendarMonth'] = month;
  unsafeWindow['CalendarYear'] = year;
  unsafeWindow['changeDay'](year,month,day);
};

That's it. I'm just defining CalDay in the scope of the window.

Huzzah for GreaseMonkey!

Now I just have to prepare all the paperwork for the visa....