-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
62 lines (55 loc) · 1.88 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
"use strict";
let timeDataIdx;
$("#clear").click(() => {
chrome.storage.local.clear();
updateWithCurrentTab();
});
$("#back").click(() => {
timeDataIdx = Math.max(0, timeDataIdx - 1);
displayTimes();
});
$("#forward").click(async () => {
const timeData = (await safePromisify(chrome.storage.local.get,
"timeData")).timeData;
timeDataIdx = Math.min(timeDataIdx + 1, timeData.length - 1);
displayTimes();
});
init();
async function init() {
const timeData = (await safePromisify(chrome.storage.local.get,
"timeData")).timeData;
timeDataIdx = timeData.length - 1;
displayTimes();
}
async function displayTimes() {
try {
$("#tbody tr").remove();
const timeData = (await safePromisify(chrome.storage.local.get,
"timeData")).timeData;
const sitesDict = timeData[timeDataIdx];
let sites = Object.keys(sitesDict).map(key => [key, sitesDict[key]]);
const privateVars = ["startTime", "endTime"];
sites = sites.filter(row => privateVars.indexOf(row[0]) === -1);
sites.sort((x, y) => y[1] - x[1]); // Sort from highest to lowest times
let appendBody = ``;
for (let i = 0; i < sites.length; i++) {
let prettyTime = ``;
let seconds = sites[i][1];
if (seconds >= 3600) {
const hours = Math.floor(seconds/3600);
prettyTime += `${hours}h `;
seconds -= hours * 3600;
}
if (seconds >= 60) {
const mins = Math.floor(seconds/60);
prettyTime += `${mins}m `;
seconds -= mins * 60;
}
prettyTime += `${Math.round(seconds)}s`;
appendBody += `<tr><td>${sites[i][0]}</td><td>${prettyTime}</td></tr>`
}
$("#tbody").append(appendBody);
} catch (err) {
console.error(err);
}
}