-
Notifications
You must be signed in to change notification settings - Fork 1
/
popup.js
207 lines (190 loc) · 6.72 KB
/
popup.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
195
196
197
198
199
200
201
202
203
204
205
206
207
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-45267314-2']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = 'https://ssl.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
var config = new Config();
var gsites = new Sites(config);
function addIgnoredSite(new_site) {
return function() {
chrome.extension.sendRequest(
{action: "addIgnoredSite", site: new_site},
function(response) {
initialize();
});
};
}
function secondsToString(seconds) {
if (config.timeDisplayFormat == Config.timeDisplayFormatEnum.MINUTES) {
return (seconds/60).toFixed(2);
}
var years = Math.floor(seconds / 31536000);
var days = Math.floor((seconds % 31536000) / 86400);
var hours = Math.floor(((seconds % 31536000) % 86400) / 3600);
var mins = Math.floor((((seconds % 31536000) % 86400) % 3600) / 60);
var secs = (((seconds % 31536000) % 86400) % 3600) % 60;
var s = "";
if (years) {
s = s + " " + years + "y";
}
if (days) {
s = s + " " + days + "d";
}
if (hours) {
s = s + " " + hours + "h";
}
if (mins) {
s = s + " " + mins + "m";
}
if (secs) {
s = s + " " + secs.toFixed(0) + "s";
}
return s;
}
function addLocalDisplay() {
var old_tbody = document.getElementById("stats_tbody");
var tbody = document.createElement("tbody");
tbody.setAttribute("id", "stats_tbody");
old_tbody.parentNode.replaceChild(tbody, old_tbody);
/* Sort sites by time spent */
var sites = gsites.sites;
var sortedSites = new Array();
var totalTime = 0;
for (site in sites) {
sortedSites.push([site, sites[site]]);
totalTime += sites[site];
}
sortedSites.sort(function(a, b) {
return b[1] - a[1];
});
/* Show only the top 15 sites by default */
var max = 15;
if (document.location.href.indexOf("show=all") != -1) {
max = sortedSites.length;
}
/* Add total row. */
var row = document.createElement("tr");
var cell = document.createElement("td");
cell.innerHTML = "<b>Total</b>";
row.appendChild(cell);
cell = document.createElement("td");
cell.appendChild(document.createTextNode(secondsToString(totalTime)));
row.appendChild(cell);
cell = document.createElement("td");
cell.appendChild(document.createTextNode(("100")));
row.appendChild(cell);
row = setPercentageBG(row,0);
tbody.appendChild(row);
var maxTime = 0;
if (sortedSites.length) {
maxTime = sites[sortedSites[0][0]];
}
var relativePct = 0;
for (var index = 0; ((index < sortedSites.length) && (index < max));
index++ ){
var site = sortedSites[index][0];
row = document.createElement("tr");
cell = document.createElement("td");
var removeImage = document.createElement("img");
removeImage.src = chrome.extension.getURL("images/remove.png");
removeImage.title = "Remove and stop tracking.";
removeImage.width = 10;
removeImage.height = 10;
removeImage.onclick = addIgnoredSite(site);
cell.appendChild(removeImage);
var a = document.createElement('a');
var linkText = document.createTextNode(site);
a.appendChild(linkText);
a.title = "Open link in new tab";
a.href = site;
a.target = "_blank";
cell.appendChild(a);
row.appendChild(cell);
cell = document.createElement("td");
cell.appendChild(document.createTextNode(secondsToString(sites[site])));
row.appendChild(cell);
cell = document.createElement("td");
cell.appendChild(document.createTextNode(
(sites[site] / totalTime * 100).toFixed(2)));
relativePct = (sites[site]/maxTime*100).toFixed(2);
row = setPercentageBG(row,relativePct);
row.appendChild(cell);
tbody.appendChild(row);
}
/* Show the "Show All" link if there are some sites we didn't show. */
if (max < sortedSites.length && document.getElementById("show") == null) {
/* Add an option to show all stats */
var showAllLink = document.createElement("a");
showAllLink.onclick = function() {
chrome.tabs.create({url: "popup.html?show=all"});
}
showAllLink.setAttribute("id", "show");
showAllLink.setAttribute("href", "javascript:void(0)");
showAllLink.setAttribute("class", "pure-button");
showAllLink.appendChild(document.createTextNode("Show All"));
document.getElementById("button_row").appendChild(showAllLink);
} else if (document.getElementById("show") != null) {
var showLink = document.getElementById("show");
showLink.parentNode.removeChild(showLink);
}
}
function setPercentageBG(row,pct) {
var color = "#e8edff";
row.style.backgroundImage = "-webkit-linear-gradient(left, "+color+" "+pct+"%,#ffffff "+pct+"%)";
row.style.backgroundImage = " -moz-linear-gradient(left, "+color+" "+pct+"%, #ffffff "+pct+"%)";
row.style.backgroundImage = " -ms-linear-gradient(left, "+color+" "+pct+"%,#ffffff "+pct+"%)";
row.style.backgroundImage = " -o-linear-gradient(left, "+color+" "+pct+"%,#ffffff "+pct+"%)";
row.style.backgroundImage = " linear-gradient(to right, "+color+" "+pct+"%,#ffffff "+pct+"%)";
return row;
}
function sendStats() {
chrome.extension.sendRequest({action: "sendStats"}, function(response) {
/* Reload the iframe. */
var iframe = document.getElementById("stats_frame");
iframe.src = iframe.src;
});
}
function clearStats() {
chrome.extension.sendRequest({action: "clearStats"}, function(response) {
initialize();
});
}
function initialize() {
addLocalDisplay();
if (config.lastClearTime) {
var div = document.getElementById("lastClear");
if (div.childNodes.length == 1) {
div.removeChild(div.childNodes[0]);
}
div.appendChild(
document.createTextNode("Last Reset: " + new Date(
config.lastClearTime).toString()));
}
var nextClearStats = config.nextTimeToClear;
if (nextClearStats) {
nextClearStats = parseInt(nextClearStats, 10);
nextClearStats = new Date(nextClearStats);
var nextClearDiv = document.getElementById("nextClear");
if (nextClearDiv.childNodes.length == 1) {
nextClearDiv.removeChild(nextClearDiv.childNodes[0]);
}
nextClearDiv.appendChild(
document.createTextNode("Next Reset: " + nextClearStats.toString()));
}
}
document.addEventListener("DOMContentLoaded", function() {
document.getElementById("clear").addEventListener("click",
function() { if (confirm("Are you sure?")) { clearStats(); }});
document.getElementById("options").addEventListener("click",
function() { chrome.runtime.openOptionsPage(); });
var buttons = document.querySelectorAll("button");
for (var i = 0; i < buttons.length; i++) {
buttons[i].addEventListener("click", function(e) {
_gaq.push(["_trackEvent", e.target.id, "clicked"]);
});
}
initialize();
});