-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbackground.js
110 lines (91 loc) · 3.06 KB
/
background.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
(function (window, undefined) {
var usingFB = false, totalTime = 0, startTime, personID, timeZoneOffset = (new Date()).getTimezoneOffset() * 1000, fbOpen = true, prevPersonID;
var startTime = function(tabId, changeInfo, tab) {
var currTime = Date.now();
if (usingFB) {
totalTime += currTime - startTime;
$.post('http://fbtimer.herokuapp.com/logsession', {
personID: prevPersonID,
sessionStart: startTime,
sessionEnd: currTime,
timeZoneOffset: timeZoneOffset
});
usingFB = false;
}
if (tab.url.indexOf('facebook.com') > -1) {
startTime = Date.now();
usingFB = true;
fbOpen = true;
if (prevPersonID != personID) {
totalTime = 0;
prevPersonID = personID;
}
chrome.tabs.sendMessage(tabId, {totalTime: totalTime});
}
}
var checkTime = function(activeinfo, changeInfo) {
var currTime = Date.now();
if (usingFB) {
totalTime += currTime - startTime;
$.post('http://fbtimer.herokuapp.com/logsession', {
personID: prevPersonID,
sessionStart: startTime,
sessionEnd: currTime,
timeZoneOffset: timeZoneOffset
});
usingFB = false;
}
chrome.tabs.query({url: "*://www.facebook.com/*"}, function(tabs) {
for (var i = 0; i < tabs.length; i++) {
if(tabs[i].id == activeinfo.tabId) {
startTime = Date.now();
usingFB = true;
fbOpen = true;
if (prevPersonID != personID) {
totalTime = 0;
prevPersonID = personID;
}
chrome.tabs.sendMessage(tabs[i].id, {totalTime: totalTime});
return;
}
}
});
}
var closingFB = function(tabId, removeInfo) {
chrome.tabs.query({url: "*://www.facebook.com/*"}, function(tabs) {
if(tabs.length == 0 && fbOpen) {
totalTime = 0;
fbOpen = false;
if (usingFB) {
totalTime += currTime - startTime;
$.post('http://fbtimer.herokuapp.com/logsession', {
personID: prevPersonID,
sessionStart: startTime,
sessionEnd: currTime,
timeZoneOffset: timeZoneOffset
});
usingFB = false;
}
$.post('http://fbtimer.herokuapp.com/endsession', {
personID: personID
});
}
});
}
// Add a listener so background knows when a tab has been reloaded or the URL is changed.
chrome.tabs.onUpdated.addListener(startTime);
//Also check time when tabs are switched
chrome.tabs.onActivated.addListener(checkTime);
//Check if all tabs are closed to know a session
chrome.tabs.onRemoved.addListener(closingFB);
//Check for messages from Contentscripts
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
if (request.sendData == "time")
sendResponse({totalTime: totalTime});
if(request.personID)
personID = request.personID;
$.get('http://fbtimer.herokuapp.com/showtimer', function(response) {
chrome.tabs.sendMessage(sender.tab.id, response);
});
});
})(window);