-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.js
194 lines (187 loc) · 8.19 KB
/
logger.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
var EDA_LOGGER = EDA_LOGGER || (function() {
var settings = {
session: Math.floor(Math.random() * 1000000) + 1,
activity_interval: 2000,
debug: true,
firebase: {
bucket: "crowdworker-logger"
}
}
// A log function which can be easily turned of using debug variable
var log = function(message) {
if (settings.debug) console.log("[EDA_LOGGER] LOG: " + message);
};
function fullPath(el){
var names = [];
while (el.parentNode){
if (el.id){
names.unshift('#'+el.id);
break;
}else{
if (el==el.ownerDocument.documentElement)
names.unshift(el.tagName);
else{
for (var c=1,e=el;e.previousElementSibling;e=e.previousElementSibling,c++);
names.unshift(el.tagName+":nth-child("+c+")");
}
el=el.parentNode;
}
}
return names.join(" > ");
}
function isPageHidden() {
return document.hidden || document.msHidden || document.webkitHidden || document.mozHidden;
}
function getSelectedText() {
var text = "";
if (typeof window.getSelection != "undefined") {
text = window.getSelection().toString();
} else if (typeof document.selection != "undefined" && document.selection.type == "Text") {
text = document.selection.createRange().text;
}
return text;
}
var _args = {
key_name: "test_name",
key_value: "test_value"
}; // private
return {
init: function(Args) {
_args = Args;
_args['task_id'] = parseInt(document.getElementById("assignment-job-id").innerHTML);
_args['worker_id'] = parseInt(document.getElementById("assignment-worker-id").innerHTML);
// Include Firebase library
var logger = this;
logger.init_firebase(function() {
logger.init_events_capturing();
logger.init_activity_capturing();
});
},
init_firebase: function(callback) {
var firebase_script = document.createElement('script');
firebase_script.src = "https://cdn.firebase.com/js/client/2.2.9/firebase.js";
log(firebase_script.src);
document.getElementsByTagName('head')[0].appendChild(firebase_script);
var logger = this;
firebase_script.onload = function() {
// get the assignment code from the url
var assignment_code = document.location.pathname.substring(document.location.pathname.lastIndexOf("/"), document.location.pathname.length);
log(assignment_code);
// get the platform code from the url
var platform_code = document.location.hostname.replace(/\./g, '');
// form the firebase endpoint url
var firebase_endpoint_url = "https://" + settings.firebase.bucket + ".firebaseio.com/" + platform_code + "/" + _args["task_id"] + "/units/" + _args['key_value'] + "/assignments" + assignment_code;
log(firebase_endpoint_url);
_args["firebase_assignment"] = new Firebase(firebase_endpoint_url);
/*_args["firebase_assignment"].update({
key_name: _args.key_name,
key_value: _args.key_value,
unit_id: _args.key_value
});*/
_args["firebase_logs"] = _args["firebase_assignment"].child('sessions/' + settings.session + "/tab_visibilty");
_args["firebase_activity"] = _args["firebase_assignment"].child('sessions/' + settings.session + "/page_activity");
_args["firebase_keys"] = _args["firebase_assignment"].child('sessions/' + settings.session + "/key_pressed");
_args["firebase_clicks"] = _args["firebase_assignment"].child('sessions/' + settings.session + "/clicks");
callback();
};
},
init_activity_capturing: function() {
var logger = this;
var activity_statuses = {
"keyboard": 0,
"mouse": 0,
"scroll": 0,
"scroll_top":0,
"text_selected":0
};
setInterval(function() {
logger.log_event(_args["firebase_activity"], (function(a) {
activity_statuses = {
"keyboard": 0,
"mouse": 0,
"scroll": 0,
"scroll_top":0,
"text_selected":0
};
return a;
}(activity_statuses)));
}, settings.activity_interval);
document.onkeydown = function(evt) {
activity_statuses["keyboard"] = 1;
};
document.onmouseup = function(evt){
if (getSelectedText() != "")
activity_statuses["text_selected"] = 1;
}
document.onmousemove = function(evt) {
activity_statuses["mouse"] = 1;
};
window.onscroll = function(evt) {
activity_statuses["scroll"] = 1;
activity_statuses["scroll_top"] = document.body.scrollTop;
};
document.onkeydown = function(evt) {
var key = evt.keyCode || evt.charCode;
logger.log_event(_args["firebase_keys"],{"key":key});
};
document.onmousedown = function(evt) {
var element_path = fullPath(evt.path[0]);
logger.log_event(_args["firebase_clicks"],{"element":element_path});
};
},
init_events_capturing: function() {
var logger = this;
// Log the task was opened by the worker
logger.log_event(_args["firebase_logs"], {
status: "opened"
});
// Log the task page was closed by the worker
window.onbeforeunload = function() {
logger.log_event(_args["firebase_logs"], {
status: "closed"
});
};
logger.init_visibility_changes();
},
init_visibility_changes: function() {
var hidden, visibilityChange;
if (typeof document.hidden !== "undefined") { // Opera 12.10 and Firefox 18 and later support
hidden = "hidden";
visibilityChange = "visibilitychange";
} else if (typeof document.mozHidden !== "undefined") {
hidden = "mozHidden";
visibilityChange = "mozvisibilitychange";
} else if (typeof document.msHidden !== "undefined") {
hidden = "msHidden";
visibilityChange = "msvisibilitychange";
} else if (typeof document.webkitHidden !== "undefined") {
hidden = "webkitHidden";
visibilityChange = "webkitvisibilitychange";
}
var logger = this;
function handleVisibilityChange() {
if (document[hidden]) {
logger.log_event(_args["firebase_logs"], {
status: "hidden"
});
} else {
logger.log_event(_args["firebase_logs"], {
status: "active"
});
}
}
// Warn if the browser doesn't support addEventListener or the Page Visibility API
if (typeof document.addEventListener === "undefined" ||
typeof document[hidden] === "undefined") {
//alert("This demo requires a browser, such as Google Chrome or Firefox, that supports the Page Visibility API.");
} else {
// Handle page visibility change
document.addEventListener(visibilityChange, handleVisibilityChange, false);
}
},
log_event: function(firebase_reference, data) {
data['dt'] = Firebase.ServerValue.TIMESTAMP;
firebase_reference.push(data);
}
};
}());