-
Notifications
You must be signed in to change notification settings - Fork 65
/
lockdown.js
185 lines (146 loc) · 4.17 KB
/
lockdown.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
/* 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); }
var gStorage = browser.storage.local;
var gFormHTML;
var gNumSets;
var gClockOffset;
// Initialize form (with specified number of block sets)
//
function initForm(numSets) {
//log("initForm: " + numSets);
// Reset form to original HTML
$("#form").html(gFormHTML);
gNumSets = +numSets;
// Use HTML for first check box to create other check boxes
let blockSetHTML = $("#blockSets").html();
for (let set = 2; set <= gNumSets; set++) {
let nextSetHTML = blockSetHTML
.replace(/(Block Set) 1/g, `$1 ${set}`)
.replace(/(id|for)="(\w+)1"/g, `$1="$2${set}"`);
$("#blockSets").append(nextSetHTML);
}
// Set up JQuery UI widgets
$("#activate").button();
$("#activate").click(onActivate);
$("#cancel").button();
$("#cancel").click(onCancel);
}
// Refresh page
//
function refreshPage() {
//log("refreshPage");
$("#form").hide();
browser.storage.local.get("sync").then(onGotSync, onError);
function onGotSync(options) {
gStorage = options["sync"]
? browser.storage.sync
: browser.storage.local;
gStorage.get().then(onGot, onError);
}
function onGot(options) {
cleanOptions(options);
// Initialize form
initForm(options["numSets"]);
setTheme(options["theme"]);
gClockOffset = options["clockOffset"];
let lockdownHours = options["lockdownHours"];
if (lockdownHours > 0) {
getElement("hours").value = lockdownHours;
}
let lockdownMins = options["lockdownMins"];
if (lockdownMins > 0) {
getElement("mins").value = lockdownMins;
}
for (let set = 1; set <= gNumSets; set++) {
let lockdown = options[`lockdown${set}`];
if (lockdown) {
getElement(`blockSet${set}`).checked = lockdown;
}
// Append custom set name to check box label (if specified)
let setName = options[`setName${set}`];
if (setName) {
getElement(`blockSetLabel${set}`).innerText += ` (${setName})`;
}
}
$("#form").show();
}
function onError(error) {
warn("Cannot get options: " + error);
$("#alertRetrieveError").dialog("open");
}
}
// Handle activate button click
//
function onActivate() {
//log("onActivate");
// Get lockdown duration
let hours = getElement("hours").value;
let mins = getElement("mins").value;
let duration = hours * 3600 + mins * 60;
if (!duration || duration < 0) {
$("#alertNoDuration").dialog("open");
return;
}
// Calculate end time for lockdown
let endTime = Math.floor(Date.now() / 1000) + (gClockOffset * 60) + duration;
// Request lockdown for each selected set
let noneSelected = true;
for (let set = 1; set <= gNumSets; set++) {
let selected = getElement(`blockSet${set}`).checked;
if (selected) {
noneSelected = false;
let message = {
type: "lockdown",
endTime: endTime,
set: set
};
// Request lockdown for this set
browser.runtime.sendMessage(message);
}
}
if (noneSelected) {
$("#alertNoSets").dialog("open");
return;
}
// Save options for next time
let options = {};
options["lockdownHours"] = hours;
options["lockdownMins"] = mins;
for (let set = 1; set <= gNumSets; set++) {
options[`lockdown${set}`] = getElement(`blockSet${set}`).checked;
}
gStorage.set(options).catch(
function (error) { warn("Cannot set options: " + error); }
);
$("#form").hide({ effect: "fade", complete: closePage });
}
// Handle cancel button click
//
function onCancel() {
//log("onCancel");
closePage();
}
// Close page
//
function closePage() {
// Request tab close
browser.runtime.sendMessage({ type: "close" });
}
/*** STARTUP CODE BEGINS HERE ***/
// Save original HTML of form
gFormHTML = $("#form").html();
// Initialize alert dialogs
$("div[id^='alert']").dialog({
autoOpen: false,
modal: true,
width: 500,
buttons: {
OK: function () { $(this).dialog("close"); }
}
});
document.addEventListener("DOMContentLoaded", refreshPage);
document.addEventListener("focus", refreshPage);