Archive for the ‘Apple’ Category

Firefox Extensions: Greasemonkey

Tuesday, February 7th, 2006

Apple is publicizing another iTunes contest. Like before, they are giving away a prize for every 100,000 songs download or entries submitted. The latter is the typical alternative that all such contests require to claim that they aren’t a lottery or another form of gambling.

In this case, the free entry form allows up to 25 entries per day. Apple makes it a little tedious by clearing the form when you go Back to it, so you have to re-enter a ton of personal information each time. They also include a captcha image to prevent automated submissions. That’s all cool, but I’d like it to be just a little easier for me to enter for the free iPod goodness.

Among other things, I’m an experienced Javascript programmer, so I know there should be way to automate the form filling a little bit. Enter Greasemonkey. Greasemonkey is a Firefox extension which allows you to customize webpages on the fly. You create a short text file which gives Greasemonkey some URLs to edit and a bit of Javascript to execute on each webpage.

After you install Greasemonkey, you can find some user script you want to install somewhere on the web and right click to “Install User Script…”. Be careful, of course, because user scripts are NOT SAFE! You need to either understand the code you are installing or trust the person who wrote the script.

I created a Greasemonkey user script that fills in some personal information in the iTunes contest form and sets the cursor in the captcha text field. Here’s the code (note that you will have to edit it to insert your own info before using it):

// ==UserScript==
// @name           Apple's iTunes 1 billion contest helper
// @namespace      http://www.chrisdolan.net
// @description    Prefill some data in a contest entry form
// @include        https://phobos.apple.com/WebObjects/MZFinance.woa/wa/billionSongAlternateEntryForm*
// @include        http://www.apple.com/itunes/1billion/entryform/
// ==/UserScript==

var focus_field = "7.1.49";
var data = {};
data["7.1.5.3"] = "Chris";
data["7.1.5.7"] = "Dolan";
data["7.1.9"] = "1234 Main St";
data["7.1.17"] = "Madison";
data["7.1.19.0.3"] = "WI";
data["7.1.21.0.3"] = "537XX";
data["7.1.25"] = "United States";
data["7.1.29"] = "608";
data["7.1.31"] = "555-1212";
data["7.1.35"] = "January";
data["7.1.36"] = "1";
data["7.1.37"] = "1970";
data["7.1.41"] = "chris@example.com";
data["7.1.45"] = "chris@example.com";

var form = document.forms[0];
for (var i=0; i<form.elements.length; i++) {
   var e = form.elements[i];
   if (e.name == focus_field)
      e.focus();
   var val = data[e.name];
   if (val == null)
      continue;
   if (e.type == "text") {
      e.value = val;
   } else if (e.type.indexOf("select") == 0) {
      for (var j=0; j<e.options.length; j++)
         if (e.options[j].text == val) {
            e.selectedIndex = j;
            break;
         }
   } else
      alert("failed to set "+e.name+" = "+val);
}

Save that as a text file called something like foo.user.js and edit it. In Firefox, drag the folder containing that script into the browser pane. You should see a directory listing for a file:// url. Right click on the user.js file and select “Install User Script…”. Then browse to the contest form. Your data should be prefilled.

If you need to edit the script, use “Tools > Manage User Scripts…” to uninstall the current version and install the edited version by the same steps as above.

Disclaimer: I do not feel that this is cheating on Apple’s contest at all. This script is just a helper similar to form auto-fill. You still have to fill in the captcha manually. This just takes a little of the tedium out of form filling.