-
Notifications
You must be signed in to change notification settings - Fork 0
/
popup.js
279 lines (215 loc) · 7.94 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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
var background = chrome.extension.getBackgroundPage();
//Event Listeners
document.addEventListener("DOMContentLoaded", function() {
loadDomains();
document.getElementById("tab-button").addEventListener("click", function(e) {
switchPage(e.target.innerHTML);
});
document.getElementById("restart-button").addEventListener("click", function() {
background.restartTimer();
});
document.getElementById("add-website").addEventListener("click", function() {
addWebsite();
});
document.getElementById("download-link").addEventListener("click", function() {
createCSVFile();
});
chrome.runtime.getBackgroundPage(function(background) {
setInterval(function() {
document.getElementById("total-timer").textContent = getTimeString(background.data.totalTime);
for(var domain in background.data.domains) {
document.getElementById(domain + "-timer").textContent = getTimeString(background.data.domains[domain]);
document.getElementById(domain + "-percentage").textContent = getPercentage(background.data.domains[domain], background.data.totalTime);
}
}, 1000);
});
});
/**
* Loads the list of websites and time information when popup is opened.
*/
function loadDomains() {
var websitesList = document.getElementById("websites");
var emptyListElement = websitesList.querySelector("li");
if(Object.keys(background.data.domains).length > 0 && emptyListElement !== null) {
websitesList.removeChild(emptyListElement);
}
for(var domain in background.data.domains) {
addHTMLForWebsiteEntry(domain);
}
}
/**
* Switches page based on `buttonValue`. If `buttonValue` is "options",
* switches to options page. If `buttonValue` is "timely", switches to
* main page.
*/
function switchPage(buttonValue) {
var tabButton = document.getElementById("tab-button");
if(buttonValue === "options") {
document.getElementById("timely").style["display"] = "none";
document.getElementById("options").style["display"] = "block";
tabButton.textContent = "time.ly";
tabButton.style["color"] = "red";
} else {
document.getElementById("timely").style["display"] = "block";
document.getElementById("options").style["display"] = "none";
tabButton.textContent = "options";
tabButton.style["color"] = "grey";
}
}
/**
* Adds domain to list of websites to track.
*/
function addWebsite() {
chrome.tabs.query({currentWindow: true, active: true}, function(tabs){
let url = new URL(tabs[0].url);
let domain = url.hostname;
if(background.data.domains.hasOwnProperty(domain)) return;
if(Object.keys(background.data.domains).length === 0) {
var websitesList = document.getElementById("websites");
var emptyListElement = websitesList.querySelector("li");
if(emptyListElement !== null) {
websitesList.removeChild(emptyListElement);
}
}
background.addWebsite(domain);
addHTMLForWebsiteEntry(domain);
});
}
/**
* Deletes the specified domain from the list of websites to track.
* @param domain String, domain to delete
*/
function deleteWebsite(domain) {
var timelyDiv = document.getElementById("timely");
background.deleteWebsite(domain);
var newList = document.createElement("ul");
newList.id = "websites";
timelyDiv.replaceChild(newList, document.getElementById("websites"));
loadDomains();
}
/**
* Creates a new `li` element with domain name, timer, and percentage of time spent on the website.
* @param domain String
*/
function addHTMLForWebsiteEntry(domain) {
var htmlString = "<li class = 'website-entry'>" +
"<button class=delete-button id=" + domain + ">x</button>" +
"<div class = 'domain'>" + domain + "</div>" +
"<div id=" + domain + "-timer>" + getTimeString(background.data.domains[domain]) + "</div>" +
"<div id=" + domain + "-percentage>" + getPercentage(background.data.domains[domain], background.data.totalTime) + "</div>"
"</li>";
document.getElementById("websites").insertAdjacentHTML("beforeend", htmlString);
document.getElementById(domain).addEventListener("click", function(e) {
deleteWebsite(e.target.id);
});
}
/**
* Creates and downloads a CSV file of timer data from chrome.storage.
*/
function createCSVFile() {
background.saveData(function() {
chrome.storage.sync.get("timelyData", function(result) {
let timelyData = result["timelyData"];
let csvString = createCSVString(timelyData);
let fileName = "timely_data.csv";
var data = encodeURI(csvString);
var downloadLink = document.createElement("a");
downloadLink.setAttribute("href", data);
downloadLink.setAttribute("download", fileName);
downloadLink.click();
//if user has selected to delete downloaded data, clear the data
chrome.storage.sync.get("saveDataOption", function(result) {
if(result["saveDataOption"]["deletePrevious"]) {
chrome.storage.sync.remove("timelyData", function() {
if(chrome.runtime.lastError)
console.log("Error deleting previous data.");
else
console.log("Previous data deleted.");
});
}
});
});
});
}
/**
* Creates a string for the downloadable CSV file using data from
* chrome.storage.
* @param timelyData Array of objects, each object represents a different timer
* @return String
*/
function createCSVString(timelyData) {
let columnDelimiter = ",";
let rowDelimiter = "\n";
var csvString = "Domains" + columnDelimiter;
let allDomainData = getAllDomainData(timelyData);
var i = 0;
for(var startTime in timelyData) {
csvString += startTime;
if(i++ < Object.keys(timelyData).length - 1) csvString += columnDelimiter;
}
csvString += rowDelimiter;
for(var domain in allDomainData)
csvString += domain + columnDelimiter + allDomainData[domain].join(columnDelimiter) + rowDelimiter;
csvString = "data:text/csv;charset=utf-8," + csvString;
return csvString;
}
/**
* Fetches time.ly data from chrome storage and creates an object to store
* all domains and their time spent percentages for each timer.
* @param result Object, chrome.storage object
* @return Object
*/
function getAllDomainData(timelyData) {
var allDomainData = {};
//add all domains seen in every timer to allDomainData
for(var startTime in timelyData) {
var domainDataForTimer = timelyData[startTime]["domains"];
for(var domain in domainDataForTimer) {
if(!allDomainData.hasOwnProperty(domain))
allDomainData[domain] = [];
}
}
//add time spent percentages for each domain
//if any domain is not present in a specific timer, "N.A." is used instead
for(var startTime in timelyData) {
var domainDataForTimer = timelyData[startTime]["domains"];
for(var domain in allDomainData) {
if(domainDataForTimer.hasOwnProperty(domain))
allDomainData[domain].push(getPercentage(domainDataForTimer[domain], timelyData[startTime]["totalTime"]));
else
allDomainData[domain].push("N.A.");
}
}
return allDomainData;
}
/**
* Creates a new timer string for the input number of seconds. For example, 68 seconds would
* output the timer string "00:01:08".
* @param numSeconds int
* @return String
*/
function getTimeString(numSeconds) {
let secondsPerUnit = [3600, 60, 1];
var timeString = "";
for(var i = 0; i < secondsPerUnit.length; i++) {
var numUnits = Math.floor(numSeconds / secondsPerUnit[i]);
timeString += stringify(numUnits);
if(i < secondsPerUnit.length - 1) timeString += ":";
numSeconds %= secondsPerUnit[i];
}
return timeString;
}
function getPercentage(numSeconds, totalTime) {
return Math.round((numSeconds/(totalTime === 0 ? 1 : totalTime)) * 100) + "%";
}
/**
* Returns a String for the input number including one leading zero (if necessary).
* @param num int
* @return String
*/
function stringify(num) {
if(num < 10)
return "0" + num.toString();
else
return num.toString();
}