-
Notifications
You must be signed in to change notification settings - Fork 0
/
croco4mturk.js
96 lines (81 loc) · 3.47 KB
/
croco4mturk.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
//GET url pars
function gup(name) {
var regexS = "[\\?&]" + name + "=([^&#]*)";
var regex = new RegExp(regexS);
var tmpURL = window.location.href;
var results = regex.exec(tmpURL);
if (results==null)
return null;
return results[1];
}
//
// This method decodes the query parameters that were URL-encoded
//
function decode(strToDecode) {
var encoded = strToDecode;
return unescape(encoded.replace(/\+/g, " "));
}
$(document).ready(function() {
//if we are not in Mturk don't to anythin
if (gup('hitId')==null) {
console.log("not in MTURK");
return;
}
else {
$("form input[type=submit]").attr("value", "Submit to MTurk");
}
//
// Check if the worker is PREVIEWING the HIT or if they've ACCEPTED the HIT
//
if (gup('assignmentId') == "ASSIGNMENT_ID_NOT_AVAILABLE") {
// If we're previewing, disable the button and give it a helpful message
$("form").find(':input').prop("disabled",true);
var msg= "You must ACCEPT the HIT before you can submit the results."
$("form input[type=submit]").attr("value", msg);
$('body').prepend("<h1 style=\"color:red\">"+msg+"</h1>")
}
//this should automatically find the form.
$("form input[type=submit]").click(
function() {
$("form input[type=submit]").prop("disabled",true);
$("form input[type=submit]").attr("value", "Sending data, please wait");
var action = $("form").attr("action");
//add hitid and assignmentID to form data.
// hit id is not of mturk class, so it's not stored twice in MTURK results
var hitId = $('<input/>').attr({ type: 'hidden', id: 'hitID', name: 'hitID', value: gup('hitId')}) ;
$("form").append(hitId);
var wokerID = $('<input/>').attr({ type: 'hidden', id: 'workerID', name: 'assignmentId', value: gup('workerId')});
$("form").append(wokerID);
//assignmetID is of mturk class, so it's store. This is mandatory from MTurk
var assignmentId = $('<input/>').attr({ type: 'hidden', id: 'assignmentId', name: 'assignmentId', value: gup('assignmentId'),"class": "mturk" });
$("form").append(assignmentId);
//do an asyn post here with all the form data to the original URL.
$.ajax({
type: 'POST',
url: action,
data: $("form").serialize(),
success: function(data){
// if the post replies with some data we add them to the form.
//this is the form that will be sent to MTurk
$.each($.parseJSON(data), function(i,el) {
var input = $('<input/>').attr({ type: 'hidden', id: el.id, name: el.id, value: el.value, "class": "mturk" }) ;
$("form").append(input);
});
},
async:false
});
//this function just checks where to send the data
$("form").attr("action",decode(gup("turkSubmitTo"))+"/mturk/externalSubmit") ;
// if (document.referrer && (document.referrer.indexOf('workersandbox') != -1)) {
// $("form").attr("action", "http://workersandbox.mturk.com/mturk/externalSubmit");
// } else {
// $("form").attr("action", "http://www.mturk.com/mturk/externalSubmit");
// }
//disable all the fields that does not have to send to mturk.
var fields = $('form input:not(.mturk) ');
fields.prop("disabled", true);
//this disable also the send, so we have to submit the form to mturk
$("form").submit();
//here the form is sent to mturk.
});
});