-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathturkframe.js
94 lines (84 loc) · 2.99 KB
/
turkframe.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
/*
* Library for working with Turkframe, a PsiTurk wrapper for use around separately hosted experiments.
*/
var Turkframe =
{
// Check if the experiment is running inside an iframe.
isFramed: function()
{
return window.self !== window.parent;
},
// Utility function to grab the value of a given parameter from the URL query string.
// Returns the value if available or `false` if not.
// NOTE: This may not handle every edge case. URLSearchParams isn't widely supported
// yet, and this block isn't sophisticated enough to handle things like duplicate
// parameters (you'll just get the first match). So keep it simple; make sure you're
// passing a relatively clean query string. An example that works fine:
// `?customStuff=hello%20world&psiturkUid=debug123abc:debug789xyz`.
getQueryParam: function(param)
{
var queryString = window.location.search;
var pattern = RegExp(param + "=([^&]+)", "g"); // whatever param followed by an equals sign followed by anything but an ampersand
var matches = pattern.exec(queryString);
if (matches && matches.length >= 2)
{
return decodeURIComponent(matches[1]); // given duplicates in the query string, just returns the first
}
return false;
},
// Returns the experiment's PsiTurk UID if it has one, or `false` if it doesn't.
getUid: function()
{
var uid = this.getQueryParam("psiturkUid");
if (uid) return uid;
return false;
},
// Returns the experiment's PsiTurk wokerId if it has one, of `false` if it doesn't.
getWorkerId: function()
{
var workerId = this.getQueryParam("psiturkWorkerId");
if (workerId) return workerId;
return false;
},
// Returns the experiment's PsiTurk condition if it has one, or `false` if it doesn't.
getCondition: function()
{
var condition = this.getQueryParam("psiturkCondition");
if (condition) return condition;
return false;
},
// Returns the experiment's PsiTurk counterbalance if it has one, or `false` if it doesn't.
getCounterbalance: function()
{
var counter = this.getQueryParam("psiturkCounter");
if (counter) return counter;
return false;
},
// Check if the experiment is running through Turkframe (i.e. is in an iframe and has a UID).
inTurkframeMode: function()
{
return this.isFramed() && this.getUid();
},
// If we're running through Turkframe, send a message to the parent.
// Messages have three parts: the `turkframe: true` flag so that Turkframe knows we sent
// the message, the `message` property so that Turkframe knows what happened, and the
// optional `data` object for passing additional information to Turkframe.
messageUp: function(message, data)
{
if (this.inTurkframeMode())
{
message = {
turkframe: true,
message: message
};
if (data) message["data"] = data;
window.parent.postMessage(message, "*");
}
},
// If we're running through Turkframe, send a message to the parent that
// the experiment is over, optionally passing along a data object.
messageFinished: function(data)
{
this.messageUp("finished", data);
},
};