Skip to content

Instantly share code, notes, and snippets.

@longouyang
Created February 26, 2011 19:30
Show Gist options
  • Save longouyang/845528 to your computer and use it in GitHub Desktop.
Save longouyang/845528 to your computer and use it in GitHub Desktop.
Small helper file for doing external HITs on Mechanical Turk
/*
-----------------------------------------------------------------
-----------------------------------------------------------------
NOTE: this file has been superseded by mmturkey:
https://github.com/longouyang/mmturkey
-----------------------------------------------------------------
-----------------------------------------------------------------
*/
var turk = {};
(function() {
var param = function(url, name ) {
name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
var regexS = "[\\?&]"+name+"=([^&#]*)";
var regex = new RegExp( regexS );
var results = regex.exec( url );
return ( results == null ) ? "" : results[1];
}
var src = param(window.location.href, "assignmentId") ? window.location.href : document.referrer;
var keys = ["assignmentId","hitId","workerId","turkSubmitTo"];
keys.map(
function(key) {
turk[key] = unescape(param(src, key));
}
);
turk.previewMode = (turk.assignmentId == "ASSIGNMENT_ID_NOT_AVAILABLE");
turk.submit = function(data) {
var assignmentId = turk.assignmentId;
var turkSubmitTo = turk.turkSubmitTo;
if (!assignmentId || !turkSubmitTo) return;
var dataString = [];
for(var key in data) {
if (data.hasOwnProperty(key)) {
dataString.push(key+"="+escape(data[key]));
}
}
dataString.push("assignmentId="+assignmentId);
var url = turkSubmitTo + "/mturk/externalSubmit?" + dataString.join("&");
window.location.href = url;
}
})();
@longouyang
Copy link
Author

Note: this file has been superseded by mmturkey

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment