forked from proginosko/LeechBlockNG
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lockdown.js
140 lines (115 loc) · 3.39 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
/* 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); }
// Initialize form
//
function initializeForm() {
//log("initializeForm");
browser.storage.local.get().then(onGot, onError);
function onGot(options) {
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 <= NUM_SETS; 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})`;
}
}
}
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) + duration;
// Request lockdown for each selected set
let noneSelected = true;
for (let set = 1; set <= NUM_SETS; 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 <= NUM_SETS; set++) {
options[`lockdown${set}`] = getElement(`blockSet${set}`).checked;
}
browser.storage.local.set(options).catch(
function (error) { warn("Cannot set options: " + error); }
);
// Request tab close
browser.runtime.sendMessage({ type: "close" });
}
// Handle cancel button click
//
function onCancel() {
//log("onCancel");
// Request tab close
browser.runtime.sendMessage({ type: "close" });
}
/*** STARTUP CODE BEGINS HERE ***/
// Use HTML for first check box to create other check boxes
let blockSetHTML = $("#blockSets").html();
for (let set = 2; set <= NUM_SETS; set++) {
let nextSetHTML = blockSetHTML
.replace(/Block Set 1/g, `Block Set ${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);
// Initialize alert dialogs
$("div[id^='alert']").dialog({
autoOpen: false,
modal: true,
width: 500,
buttons: {
OK: function () { $(this).dialog("close"); }
}
});
$("#form").show();
document.addEventListener("DOMContentLoaded", initializeForm);