-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
311 lines (280 loc) · 9.87 KB
/
script.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
Watch: {
timestamp: Number
label: String
}
config: {
saved: Boolean,
watches: Array[Watch],
tracked: Number,
settings: {
autoclear: {
enabled: Boolean,
time: Number
},
reload_minutes: Number
},
hash: String
}
*/
function correctConfig() {
if (typeof config.saved === "undefined") config.saved = true;
if (typeof config.watches === "undefined") config.watches = [];
if (typeof config.tracked === "undefined") config.tracked = -1;
// if (typeof config.settings === "undefined") config.settings = {};
// if (typeof config.settings.reload_minutes === "undefined") config.settings.reload_minutes = 2/60;
// if (typeof config.settings.autoclear === "undefined") config.settings.autoclear = {};
// if (typeof config.settings.autoclear.enabled === "undefined") config.settings.autoclear.enabled = false;
// if (typeof config.settings.autoclear.time === "undefined") config.settings.autoclear.time = 18000;
ensureSettings();
if (typeof config.hash === "undefined") config.hash = "";
}
var default_config = {
saved: true,
watches: [],
tracked: -1,
settings: {
autoclear: {
enabled: true,
time: 18000
},
reload_minutes: 2/60
},
hash: ""
};
config = default_config;
function clearStorage() {
localStorage.clear();
}
var display;
var lastReload;
var lastHash = "";
async function main() {
display = document.getElementById("display");
if (localStorage.stConfig) {
config = JSON.parse(localStorage.stConfig);
renderStopwatches();
}
setInterval(updateStopwatches, 50);
await doAccessCheck();
config = await retrieveConfig();
correctConfig();
setSettingsFromConfig();
if (typeof config.tracked === "undefined") config.tracked = -1;
if (location.hash == "#start") {
location.hash = "";
startNewStopwatch();
} else {
renderStopwatches();
}
lastReload = Date.now();
lastHash = config.hash;
}
window.addEventListener("load", async function() {
switchTo(mainwindow);
await main();
});
// start a new stopwatch with no label
function startNewStopwatch() {
if (getSelection().toString() != '') { // check if any text on the screen is highlighted, which is the secret code to process an admin command instead
processAdminCommand();
return;
}
config.watches.push({
timestamp: Date.now(),
label: ""
});
if (config.watches.length == 1) config.tracked = 0;
saveConfig();
renderStopwatches();
window.scrollTo(0, document.body.scrollHeight)
}
// populate window with stopwatches according to config
function renderStopwatches() {
display.innerHTML = "";
for (var i = 0; i < config.watches.length; i++) {
display.innerHTML += `
<div class="stopwatch">
<h1 onclick='setTime(${i})'></h1>
<input type="radio" onclick="updateTracked()" name="tracked" class="trackbtn" value="${i}" /><span><button class='labelBtn' onclick='addLabel(${i})'>Add Label</button></span> <u class='deleteBtn' onclick='deleteWatch(${i})'>Delete</u>
</div>
`;
}
}
// update times on existing stopwatches
function updateStopwatches() {
doReloadCheck();
var els = Array.from(display.children);
els.forEach(function(el, i) {
if (!config.watches[i]) return;
el.children[0].innerText = formatTime(config.watches[i].timestamp);
if (config.watches[i].label != "") {
el.children[2].children[0].innerHTML = config.watches[i].label;
}
el.children[1].checked = (i == config.tracked);
});
}
// format time in h:mm:ss format
function formatTime(timestamp) {
var timeDiff = Date.now() - timestamp;
var hours = Math.floor(timeDiff/1000/60/60);
timeDiff -= hours*1000*60*60;
var minutes = Math.floor(timeDiff/1000/60);
timeDiff -= minutes*1000*60;
var seconds = Math.floor(timeDiff/1000);
return `${hours}:${minutes.toString().padStart(2, 0)}:${seconds.toString().padStart(2, 0)}`;
}
// set label for stopwatch by index
function addLabel(i) {
var input = prompt("Enter Label", config.watches[i].label || "");
config.watches[i].label = input;
saveConfig();
}
// delete stopwatch by index
function deleteWatch(i) {
if (!confirm(`Are you sure you want to delete stopwatch ${config.watches[i].label}?`)) return;
config.watches.splice(i, 1);
if (config.tracked == i) config.tracked = -1;
if (i < config.tracked) config.tracked--;
if (config.watches.length == 0) config.tracked = -1;
saveConfig();
renderStopwatches();
}
// set new time by index
function setTime(i) {
var input = prompt("Enter time in form h:mm:ss", formatTime(config.watches[i].timestamp));
if (!input) return;
var inputs = input.split(":");
var millis = (+inputs[0])*1000*60*60 + (+inputs[1])*1000*60 + (+inputs[2])*1000;
config.watches[i].timestamp = Date.now()-millis;
saveConfig();
}
// update index of selected stopwatch to track (display on my home screen)
function updateTracked() {
if (document.querySelector("input[name='tracked']:checked")) {
config.tracked = +document.querySelector("input[name='tracked']:checked").value
} else {
config.tracked = -1;
}
saveConfig();
}
/*
Admin commands are a series of letters and arguments, to achieve things that are not strictly
necessary for operation but still helpful tools to have.
Commands:
r: reorder
rm: move (example: rm1,3 => insert position 1 to position 3)
rs: swap (example: rs1,3 => swap positions 1 and 3)
d: deselect tracked stopwatch
l: logout (clears login data)
*/
function processAdminCommand() {
var cmd = prompt("Enter Command");
if (cmd[0] == "r") reorder(cmd.substring(1));
if (cmd[0] == "d") deselect();
if (cmd[0] == "l") logout();
}
// reorder two stopwatches (move or swap)
function reorder(reorderCode) {
/*
rm1,3 => moves 1 to 3 position (shift)
rs1,3 => swaps 1 and 3 position
*/
var reorderLetter = reorderCode[0];
var arg1 = +reorderCode.substring(1, reorderCode.indexOf(","));
var arg2 = +reorderCode.substring(reorderCode.indexOf(",")+1, reorderCode.length);
if (reorderLetter == "s") {
var w1 = config.watches[arg1];
var w2 = config.watches[arg2];
config.watches[arg1] = w2;
config.watches[arg2] = w1;
if (config.tracked == arg1) {
config.tracked = arg2;
} else if (config.tracked == arg2) {
config.tracked = arg1;
}
} else if (reorderLetter == "m") {
var watch = config.watches.splice(arg1, 1)[0];
config.watches.splice(arg2, 0, watch);
if (config.tracked <= arg2 && config.tracked > arg1) config.tracked--;
if (config.tracked == arg1) config.tracked = arg2;
} else {
alert("invalid code");
}
saveConfig();
}
// deselect tracked stopwatch
function deselect() {
document.querySelector("input[name='tracked']:checked").checked = false;
updateTracked();
}
// clear credentials
function logout() {
localStorage.removeItem("dkey");
location.reload();
}
HTMLElement.prototype.show = function() {
this.removeAttribute("hidden");
}
HTMLElement.prototype.hide = function() {
this.setAttribute("hidden", "true");
}
function hideAll() {
var elements = document.getElementsByClassName("hideable");
Array.from(elements).forEach(element=>element.hide());
}
function switchTo(element) {
hideAll();
element.show();
}
function settingsClick() {
switchTo(settings);
}
function setSettingsFromConfig() {
ensureSettings();
autoclear_check.checked = config.settings.autoclear.enabled;
autoclear_time.disabled = !autoclear_check.checked;
autoclear_time.value = convertSecondsToTimestring(config.settings.autoclear.time);
reload_minutes_input.value = config.settings.reload_minutes*60;
}
function ensureSettings() {
if (! (config.settings instanceof Object)) config.settings = {autoclear: {enabled: true, time: convertTimestringToSeconds("5:00")}, reload_minutes: 2/60};
if (! (config.settings.autoclear instanceof Object)) config.settings.autoclear = {enabled: true, time: convertTimestringToSeconds("5:00")};
if (typeof config.settings.autoclear.enabled === "undefined") config.settings.autoclear.enabled = true;
if (typeof config.settings.autoclear.time === "undefined") config.settings.autoclear.time = convertTimestringToSeconds("5:00");
if (typeof config.settings.reload_minutes === "undefined") config.settings.reload_minutes = 2/60;
}
async function doAutoclearChange() {
ensureSettings();
config.settings.autoclear.enabled = autoclear_check.checked;
autoclear_time.disabled = !autoclear_check.checked;
config.settings.autoclear.time = convertTimestringToSeconds(autoclear_time.value);
await saveConfig();
}
async function doReloadTimeChange() {
ensureSettings();
config.settings.reload_minutes = (+reload_minutes_input.value)/60;
await saveConfig();
}
function convertTimestringToSeconds(timestring) {
var parts = timestring.split(":");
var hours = +parts[0];
var minutes = +parts[1];
return (hours*60+minutes)*60;
}
function convertSecondsToTimestring(seconds) {
var hours = Math.floor(seconds/60/60);
var minutes = Math.floor((seconds-hours*60*60)/60);
return `${hours.toString().padStart(2, "0")}:${minutes.toString().padStart(2, "0")}`;
}
async function doReloadCheck() {
ensureSettings();
if (typeof config.settings.reload_minutes === "undefined") return;
var current = Date.now();
var delay = config.settings.reload_minutes*60*1000;
if (current > lastReload + delay) {
lastReload = current;
await retrieveConfig();
if (config.hash != lastHash) main();
}
}