-
Notifications
You must be signed in to change notification settings - Fork 4
/
mmturkey-extension.js
176 lines (153 loc) · 8.28 KB
/
mmturkey-extension.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// Bundle in Crockford's JSON2.js
var JSON;JSON||(JSON={});
(function(){function k(a){return a<10?"0"+a:a}function o(a){p.lastIndex=0;return p.test(a)?'"'+a.replace(p,function(a){var c=r[a];return typeof c==="string"?c:"\\u"+("0000"+a.charCodeAt(0).toString(16)).slice(-4)})+'"':'"'+a+'"'}function l(a,j){var c,d,h,m,g=e,f,b=j[a];b&&typeof b==="object"&&typeof b.toJSON==="function"&&(b=b.toJSON(a));typeof i==="function"&&(b=i.call(j,a,b));switch(typeof b){case "string":return o(b);case "number":return isFinite(b)?String(b):"null";case "boolean":case "null":return String(b);case "object":if(!b)return"null";
e+=n;f=[];if(Object.prototype.toString.apply(b)==="[object Array]"){m=b.length;for(c=0;c<m;c+=1)f[c]=l(c,b)||"null";h=f.length===0?"[]":e?"[\n"+e+f.join(",\n"+e)+"\n"+g+"]":"["+f.join(",")+"]";e=g;return h}if(i&&typeof i==="object"){m=i.length;for(c=0;c<m;c+=1)typeof i[c]==="string"&&(d=i[c],(h=l(d,b))&&f.push(o(d)+(e?": ":":")+h))}else for(d in b)Object.prototype.hasOwnProperty.call(b,d)&&(h=l(d,b))&&f.push(o(d)+(e?": ":":")+h);h=f.length===0?"{}":e?"{\n"+e+f.join(",\n"+e)+"\n"+g+"}":"{"+f.join(",")+
"}";e=g;return h}}if(typeof Date.prototype.toJSON!=="function")Date.prototype.toJSON=function(){return isFinite(this.valueOf())?this.getUTCFullYear()+"-"+k(this.getUTCMonth()+1)+"-"+k(this.getUTCDate())+"T"+k(this.getUTCHours())+":"+k(this.getUTCMinutes())+":"+k(this.getUTCSeconds())+"Z":null},String.prototype.toJSON=Number.prototype.toJSON=Boolean.prototype.toJSON=function(){return this.valueOf()};var q=/[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,
p=/[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,e,n,r={"\u0008":"\\b","\t":"\\t","\n":"\\n","\u000c":"\\f","\r":"\\r",'"':'\\"',"\\":"\\\\"},i;if(typeof JSON.stringify!=="function")JSON.stringify=function(a,j,c){var d;n=e="";if(typeof c==="number")for(d=0;d<c;d+=1)n+=" ";else typeof c==="string"&&(n=c);if((i=j)&&typeof j!=="function"&&(typeof j!=="object"||typeof j.length!=="number"))throw Error("JSON.stringify");return l("",
{"":a})};if(typeof JSON.parse!=="function")JSON.parse=function(a,e){function c(a,d){var g,f,b=a[d];if(b&&typeof b==="object")for(g in b)Object.prototype.hasOwnProperty.call(b,g)&&(f=c(b,g),f!==void 0?b[g]=f:delete b[g]);return e.call(a,d,b)}var d,a=String(a);q.lastIndex=0;q.test(a)&&(a=a.replace(q,function(a){return"\\u"+("0000"+a.charCodeAt(0).toString(16)).slice(-4)}));if(/^[\],:{}\s]*$/.test(a.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g,"@").replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g,
"]").replace(/(?:^|:|,)(?:\s*\[)+/g,"")))return d=eval("("+a+")"),typeof e==="function"?c({"":d},""):d;throw new SyntaxError("JSON.parse");}})();
var turk;
turk = turk || {};
(function() {
if (!Array.prototype.map) {
Array.prototype.map = function(fun /*, thisp*/) {
var len = this.length >>> 0;
if (typeof fun != "function") { throw new TypeError(); }
var res = new Array(len);
var thisp = arguments[1];
for (var i = 0; i < len; i++) {
if (i in this)
res[i] = fun.call(thisp, this[i], i, this);
}
return res;
};
}
var hopUndefined = !Object.prototype.hasOwnProperty,
showPreviewWarning = true;
// We can disable the previewWarning by including this script with "nowarn" in the script url
// (i.e. mmturkey.js?nowarn). This doesn't work in FF 1.5, which doesn't define document.scripts
if (document.scripts) {
for(var i=0, ii = document.scripts.length, src = document.scripts[i].src; i < ii; i++ ) {
if ( /mmturkey/.test(src) && /\?nowarn/.test(src) ) {
showPreviewWarning = false;
break;
}
}
}
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];
}
// Give an HTML representation of an object
var htmlify = function(obj) {
if (obj instanceof Array) {
return "[" + obj.map(function(o) { return htmlify(o) } ).join(",") + "]";
} else if (typeof obj == "object") {
var strs = [];
for(var key in obj) {
if (obj.hasOwnProperty(key)) {
var str = "<li>" + htmlify(key) + ": " + htmlify(obj[key]) + "</li>";
strs.push(str);
}
}
return "{<ul>" + strs.join("") + "</ul>}";
} else if (typeof obj == "string") {
return '"' + obj + '"';
} else if (typeof obj == "undefined" ) {
return "[undefined]"
} else {
return obj.toString();
}
};
var addFormData = function(form,key,value) {
var input = document.createElement('input');
input.type = 'hidden';
input.name = key;
input.value = value;
form.appendChild(input);
}
var url = window.location.href,
src = param(url, "assignmentId") ? url : document.referrer,
keys = ["assignmentId","hitId","workerId","turkSubmitTo"];
keys.map(function(key) {
turk[key] = unescape(param(src, key));
});
turk.previewMode = (turk.assignmentId == "ASSIGNMENT_ID_NOT_AVAILABLE");
// TODO: what do you do if data is an array, rather than an object?
// Submit a POST request to Turk
turk.submit = function(object) {
var assignmentId = turk.assignmentId,
turkSubmitTo = turk.turkSubmitTo,
rawData = {},
form = document.createElement('form');
document.body.appendChild(form);
if (assignmentId) {
rawData.assignmentId = assignmentId;
addFormData(form,"assignmentId",assignmentId);
}
// Filter out non-own properties and things that are functions
for(var key in object) {
if ((hopUndefined || object.hasOwnProperty(key)) && (typeof object[key] != "function") ) {
rawData[key] = object[key];
addFormData(form, key, JSON.stringify(object[key]));
}
}
if (assignmentId && turkSubmitTo) {
// If you're hooked up to MTurk, submit the form
form.action = turk.turkSubmitTo + "/mturk/externalSubmit";
form.method = "POST";
form.submit();
} else if (submitAddress && experimentName) {
// else, if you want to submit to a server specified in your JavaScript file
// your experiment's JavaScript file should specify BOTH variables, else it won't post
var uniqueIdentifier = Math.floor((Math.random() * 100000000000000000) + 1);
addFormData(form, 'uniqueIdentifier', JSON.stringify(uniqueIdentifier));
addFormData(form, 'experimentName', JSON.stringify(experimentName));
addFormData(form, 'payment', JSON.stringify('none'));
form.action = submitAddress;
form.method = "POST";
form.submit();
} else {
// Else, you're in debug mode - emit the debug output and stop
var div = document.createElement('div');
div.style.fontFamily = '"HelveticaNeue-Light", "Helvetica Neue Light", "Helvetica Neue", sans-serif';
div.style.fontSize = "14px";
div.style.cssFloat = "right";
div.style.padding = "1em";
div.style.backgroundColor = "#dfdfdf";
div.innerHTML = "<p><b>Debug mode</b></p>Here is the data that would have been submitted to Turk: <ul>" + htmlify(rawData) + "</ul>"
document.body.appendChild(div);
return;
}
}
// simulate $(document).ready() to show the preview warning
if (showPreviewWarning && turk.previewMode) {
var intervalHandle = setInterval(function() {
try {
var div = document.createElement('div'), style = div.style;
style.backgroundColor = "gray";
style.color = "white";
style.position = "absolute";
style.margin = "0";
style.padding = "0";
style.paddingTop = "15px";
style.paddingBottom = "15px";
style.top = "0";
style.width = "98%";
style.textAlign = "center";
style.fontFamily = "arial";
style.fontSize = "24px";
style.fontWeight = "bold";
style.opacity = "0.5";
style.filter = "alpha(opacity = 50)";
div.innerHTML = "PREVIEW MODE: CLICK \"ACCEPT\" ABOVE TO START THIS HIT";
document.body.appendChild(div);
clearInterval(intervalHandle);
} catch(e) {
}
},20);
}
})();