-
Notifications
You must be signed in to change notification settings - Fork 0
/
tracker.js
101 lines (91 loc) · 2.77 KB
/
tracker.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
var tracker = (function() {
var timeSpan = 0;
var shouldSendRequest = false;
var url = "";
var interval = -1;
// selectors for main chat's name and status
var selectors = {
name: "div#main>header>div:nth-child(2)>div:nth-child(1)>div>span",
status: "div#main>header>div:nth-child(2)>div:nth-child(2)>span"
};
var pollThread = function() {
var person = getPersonInfo();
if (isOnline(person)) {
console.log(person.name + " is online");
timeSpan += 1;
// a flag to tell if we tracked something or not
shouldSendRequest = true;
} else if (shouldSendRequest) {
sendRequest({
name: person.name,
timeSpan: timeSpan
});
timeSpan = 0;
shouldSendRequest = false;
}
};
/**
* Tells if the chat's header says "typing"
* @param person object
*/
var isTyping = function(person) {
return person && person.status.indexOf("typing") != -1;
};
/**
* Tells if the chat's header says "online"
* @param person object
*/
var isJustOnline = function(person) {
return person && person.status.indexOf("online") != -1;
};
/**
* Return true if a person is either typing or just online.
* @param person the person object
*/
var isOnline = function(person) {
return person && (isJustOnline(person) || isTyping(person));
};
var getNodesBy = function(css) {
var matches = document.querySelectorAll(css);
return matches.length > 0 ? matches[0].innerHTML : null;
};
var getPersonInfo = function() {
var name = getNodesBy(selectors.name) || '';
var status = getNodesBy(selectors.status) || 'offline';
return {
name: name,
status: status
};
};
var init = function(config) {
var freq = config.freq || 1000;
var serverUrl = config.serverUrl || 'localhost:5000';
console.log("Initialized the polling thread");
interval = window.setInterval(pollThread, freq);
};
var sendRequest = function(data) {
fetch('http://localhost:5000/track/' + data['name'], {
method: 'POST',
body: JSON.stringify({
person: data['name'],
timeSpan: data['timeSpan']
}),
headers: {
'Content-type': 'application/json; charset=UTF-8'
}
}).then(res=>res.json()).then(console.log);
};
var stop = function() {
clearInterval(interval);
console.log("Tracking stopped.");
};
return {
init: init,
stop: stop
};
}
)();
tracker.init({
serverUrl: 'localhost:5000',
freq: 1000
});