Monday, November 14, 2005

Just in case this gets too popular, I have hosted it on a blogger account.

Unfortunately, this adds one step to adding the script to Greasemonkey.

Anyway, this is a script I created to help out anyway clicking away on the Amazon Mechanical Turk. Right now they only have one type of task where you choose an image that hopefully is the business front for some random business. The way they have it set up makes this quite tedious, especially since you need to be fast to make it worthwhile.

This script automates the process - it will accept the HIT automatically, then you just need to click on an image to select and submit it. I hope you find it useful.

To install:
- Download and install the Greasemonkey extension (remember to restart Firefox)
- First copy the code below into a text file named mturk.user.js
- Then open that file in Firefox
- Select "Install this user script" from the tools menu in Firefox
- Head on over to mturk.com to try it out!


// ==UserScript==


// @name Amazon Mechanical Turk


// @namespace http://abstract-science.com


// @description A script to ease the use of the Amazon Mechanical Turk


// @include http://www.mturk.com/mturk/accept*

// @include http://www.mturk.com/mturk/preview*

// @include http://www.mturk.com/mturk/submit


// ==/UserScript==


// function to create and return a new attribute
function MakeAttribute (attName, attValue) {
var objAttribute=document.createAttribute(attName);
objAttribute.value = attValue;
return objAttribute;
}

// get all the form tags to get the question form
forms = document.getElementsByTagName ("FORM");

// get the HIT form
for (var i in forms) {
if (forms[i].name == "hitForm") {
hitForm = forms[i];
}
}

// get all the inputs for the form
inputs = hitForm.getElementsByTagName ("INPUT");

// get all the radio buttons
var radioButtons = [], curRadio;
for (i = 0; i < inputs.length; i++) {
// find the question radio buttons
if (inputs[i].className == "question selection") {
curRadio = inputs[i];
radioButtons.push (curRadio);
// enable it
curRadio.disabled = false;
}
}


// get all the P tags, to find business name and address
questionTitleElements = document.getElementsByTagName ("P");
var questionTitle= "";

// loop through all the P elements
for (var i = 0; i < questionTitleElements.length; i++) {
// if this is a name or address
if (questionTitleElements[i].className == "question title") {
// add it to our title
questionTitle += questionTitleElements[i].innerHTML + "
";
}
}

// get all the images in the document

images = document.getElementsByTagName ("IMG");
// array of the answer images
answerImages = [];

// loop through all the images
var curImage, baseURI = false;
for (var i = 0; i < images.length; i++) {
curImage = images[i];
// if it is an accept image, get url
if (curImage.src == "http://www.mturk.com/images/accept_hit.gif")
baseURI = unescape(curImage.parentNode.href);

// if it is an answer images add it to the array
if (curImage.className == "answer binary image") {
answerImages.push (curImage);
}
}

// get the body of the document
body = document.getElementsByTagName ("body")[0];
if (!body) body = document.getElementsByTagName ("BODY")[0];

// get the parent of the body
bodyParent = body.parentNode;

// if this needs to be accepted, accept it
if (baseURI != false) {
window.location.href = baseURI;
bodyParent.removeChild (body);
} else {

// create the new body
newBody = document.createElement ("body");
// enclose everything in an h1 tag
var h1 = document.createElement ("h1");
// add the question title to the top
h1.innerHTML = questionTitle + "
";

// add all the images
var newImage;
for (i = 0; i < answerImages.length; i++) {
// make an image tag
newImage = document.createElement ("img");
// set attributes
newImage.attributes.setNamedItem (MakeAttribute ("src", answerImages[i].src));
newImage.attributes.setNamedItem (MakeAttribute ("width", 300));
newImage.attributes.setNamedItem (MakeAttribute ("height", 225));
newImage.attributes.setNamedItem (MakeAttribute ("class", "i" + i));
// add the new text and image
h1.appendChild (newImage);
}

// add the new body
newBody.appendChild (h1);
bodyParent.insertBefore (newBody, body);

// intercept clicks
document.addEventListener('click', function(event) {
if (event.target.className.charAt (0) == "i") {
var i = event.target.className.charAt (1);
radioButtons[i].checked = true;
hitForm.submit();
}
}, true);

}