forked from proginosko/LeechBlockNG
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stats.js
119 lines (98 loc) · 3.54 KB
/
stats.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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
function log(message) { console.log("[LBNG] " + message); }
function warn(message) { console.warn("[LBNG] " + message); }
function getElement(id) { return document.getElementById(id); }
// Refresh table of statistics
//
function statsRefresh() {
//log("statsRefresh");
browser.storage.local.get("sync").then(onGotSync, onError);
function onGotSync(options) {
if (options["sync"]) {
browser.storage.sync.get().then(onGot, onError);
} else {
browser.storage.local.get().then(onGot, onError);
}
}
function onGot(options) {
setTheme(options["theme"]);
// Get current time in seconds
let now = Math.floor(Date.now() / 1000);
for (let set = 1; set <= NUM_SETS; set++) {
let setName = options[`setName${set}`];
let timedata = options[`timedata${set}`];
let limitMins = options[`limitMins${set}`];
let limitPeriod = options[`limitPeriod${set}`];
let limitOffset = options[`limitOffset${set}`];
let periodStart = getTimePeriodStart(now, limitPeriod, limitOffset);
if (setName) {
getElement(`blockSetName${set}`).innerText = setName;
}
if (Array.isArray(timedata) && timedata.length == 5) {
let fs = getFormattedStats(timedata);
getElement(`startTime${set}`).innerText = fs.startTime;
getElement(`totalTime${set}`).innerText = fs.totalTime;
getElement(`perWeekTime${set}`).innerText = fs.perWeekTime;
getElement(`perDayTime${set}`).innerText = fs.perDayTime;
if (limitMins && limitPeriod) {
// Calculate total seconds left in this time period
let secsLeft = (timedata[2] == periodStart)
? Math.max(0, (limitMins * 60) - timedata[3])
: (limitMins * 60);
let timeLeft = formatTime(secsLeft);
getElement(`timeLeft${set}`).innerText = timeLeft;
}
if (timedata[4] > now) {
let ldEndTime = new Date(timedata[4] * 1000).toLocaleString();
getElement(`ldEndTime${set}`).innerText = ldEndTime;
}
}
}
$("#form").show();
}
function onError(error) {
warn("Cannot get options: " + error);
}
}
// Return formatted times based on time data
//
function getFormattedStats(timedata) {
let days = 1
+ Math.floor(Date.now() / 86400000)
- Math.floor(timedata[0] / 86400);
let weeks = Math.floor((days + 6) / 7);
return {
startTime: new Date(timedata[0] * 1000).toLocaleString(),
totalTime: formatTime(timedata[1]),
perWeekTime: formatTime(timedata[1] / weeks),
perDayTime: formatTime(timedata[1] / days)
};
}
// Handle button click
//
function handleClick(e) {
let id = e.target.id;
if (id == "restartAll") {
// Request restart time data for all sets
let message = { type: "restart", set: 0 };
browser.runtime.sendMessage(message).then(statsRefresh);
} else if (/restart\d+/.test(id)) {
// Request restart time data for specific set
let message = { type: "restart", set: +id.substr(7) };
browser.runtime.sendMessage(message).then(statsRefresh);
}
}
/*** STARTUP CODE BEGINS HERE ***/
// Use HTML for first row to create other rows
let rowHTML = $("#statsRow1").html();
for (let set = 2; set <= NUM_SETS; set++) {
let nextRowHTML = rowHTML
.replace(/Block Set 1/g, `Block Set ${set}`)
.replace(/id="(\w+)1"/g, `id="$1${set}"`);
$("#statsTable").append(`<tr id="statsRow${set}">${nextRowHTML}</tr>`);
}
$(":button").click(handleClick);
document.addEventListener("DOMContentLoaded", statsRefresh);
document.addEventListener("focus", statsRefresh);