-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.html
76 lines (73 loc) · 2.25 KB
/
background.html
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
<html>
<head>
<script src="js/jquery-1.4.2.min.js"></script>
<script src="js/db.js"></script>
<script src="js/lib.js"></script>
</head>
<body>
<div id="data">
</div>
<script>
if (typeof(localStorage['enable']) == "undefined") { //first time running extention
localStorage['enable'] = 1;
}
if (localStorage['enable'] == 0) {
chrome.browserAction.setIcon({path:"icon-disable.png"})
}
DB.init();
function updateBadge(host) {
if(!host) {
chrome.browserAction.setBadgeText({'text':''});
chrome.browserAction.setTitle({'title':'AnalyseMe'});
return;
}
DB.fetchHostToday(host, function (data) {
if(data.length > 0) {
var text = String(Math.floor(data[0]['count']));
chrome.browserAction.setBadgeText({'text':text});
chrome.browserAction.setTitle({'title':text + " minutes spent on "+host + ' today'});
}
});
}
//listen for changes to detect activity
chrome.windows.onFocusChanged.addListener(
function(windowid) {
localStorage['lastActive'] = new Date().getTime();
});
chrome.tabs.onSelectionChanged.addListener(
function(tabid) {
localStorage['lastActive'] = new Date().getTime();
chrome.tabs.get(tabid, function(tab) {
var host = Lib.getHostname(tab.url);
updateBadge(host);
});
});
chrome.tabs.onUpdated.addListener(
function(tabId, changeInfo, tab) {
localStorage['lastActive'] = new Date().getTime();
if(tab.selected && changeInfo['url']) {
var host = Lib.getHostname(changeInfo['url']);
updateBadge(host);
}
});
function fetch_url() {
if(Lib.isActive() && localStorage['enable'] == 1) {//only update chrome was active and the extention is active
chrome.tabs.getSelected(null, function(tab) {
var d = new Date();
var rowObj = {};
var urlArray = tab.url.split(/\//);
//only count http and https pages
if(urlArray[0] == 'http:' || urlArray[0] == 'https:') {
rowObj['host'] = urlArray[2].replace(/^www\./, '');
var dateStr = DB.getDateStr(d);
rowObj['date'] = dateStr;
rowObj['hour'] = d.getHours();
DB.updateRow(rowObj);
}
});
}
}
window.setInterval('fetch_url()', 10000);
</script>
</body>
</html>