forked from proginosko/LeechBlockNG
-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.js
305 lines (267 loc) · 10.5 KB
/
options.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
/* 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); }
// Save options to local storage
//
function saveOptions() {
//log("saveOptions");
// Check format for text fields
for (let set = 1; set <= NUM_SETS; set++) {
// Get field values
let times = document.querySelector(`#times${set}`).value;
let limitMins = document.querySelector(`#limitMins${set}`).value;
let delaySecs = document.querySelector(`#delaySecs${set}`).value;
let blockURL = document.querySelector(`#blockURL${set}`).value;
let parsedURL = getParsedURL(blockURL);
// Check field values
if (!checkTimePeriodsFormat(times)) {
$("#panes").accordion("option", "active", (set - 1));
$("#alertBadTimes").dialog("open");
return;
}
if (!checkPosIntFormat(limitMins)) {
$("#panes").accordion("option", "active", (set - 1));
$("#alertBadTimeLimit").dialog("open");
return;
}
if (!delaySecs || !checkPosIntFormat(delaySecs)) {
$("#panes").accordion("option", "active", (set - 1));
$("#alertBadSeconds").dialog("open");
return;
}
if (blockURL != DEFAULT_BLOCK_URL && blockURL != DELAYED_BLOCK_URL && !parsedURL.page) {
$("#panes").accordion("option", "active", (set - 1));
$("#alertBadBlockURL").dialog("open");
return;
}
}
let options = {};
for (let set = 1; set <= NUM_SETS; set++) {
// Get component values
let setName = document.querySelector(`#setName${set}`).value;
let sites = document.querySelector(`#sites${set}`).value;
sites = sites.replace(/\s+/g, " ").replace(/(^ +)|( +$)|(\w+:\/+)/g, "");
sites = sites.split(" ").sort().join(" "); // sort alphabetically
//let sitesURL = document.querySelector(`#sitesURL${set}`).value;
let times = document.querySelector(`#times${set}`).value;
let limitMins = document.querySelector(`#limitMins${set}`).value;
let limitPeriod = document.querySelector(`#limitPeriod${set}`).value;
let conjMode = document.querySelector(`#conjMode${set}`).selectedIndex == 1;
let days = [];
for (let i = 0; i < 7; i++) {
days.push(document.querySelector(`#day${i}${set}`).checked);
}
let blockURL = document.querySelector(`#blockURL${set}`).value;
let activeBlock = document.querySelector(`#activeBlock${set}`).checked;
let countFocus = document.querySelector(`#countFocus${set}`).checked;
let delayFirst = document.querySelector(`#delayFirst${set}`).checked;
let delaySecs = document.querySelector(`#delaySecs${set}`).value;
let prevOpts = document.querySelector(`#prevOpts${set}`).checked;
let prevAddons = document.querySelector(`#prevAddons${set}`).checked;
let prevConfig = document.querySelector(`#prevConfig${set}`).checked;
// Get regular expressions to match sites
let regexps = getRegExpSites(sites);
// Set option values
options[`setName${set}`] = setName;
options[`sites${set}`] = sites;
options[`times${set}`] = cleanTimePeriods(times);
options[`limitMins${set}`] = limitMins;
options[`limitPeriod${set}`] = limitPeriod;
options[`conjMode${set}`] = conjMode;
options[`days${set}`] = days;
options[`blockURL${set}`] = blockURL;
options[`activeBlock${set}`] = activeBlock;
options[`countFocus${set}`] = countFocus;
options[`delayFirst${set}`] = delayFirst;
options[`delaySecs${set}`] = delaySecs;
options[`prevOpts${set}`] = prevOpts;
options[`prevAddons${set}`] = prevAddons;
options[`prevConfig${set}`] = prevConfig;
options[`blockRE${set}`] = regexps.block;
options[`allowRE${set}`] = regexps.allow;
options[`keywordRE${set}`] = regexps.keyword;
}
browser.storage.local.set(options);
// Notify extension that options have been updated
browser.runtime.sendMessage({ type: "options" });
$("#form").hide({ effect: "fade", complete: retrieveOptions });
$("#form").show({ effect: "fade" });
}
// Save options and close tab
//
function saveOptionsClose() {
saveOptions();
// Request tab close
browser.runtime.sendMessage({ type: "close" });
}
// Retrieve options from local storage
//
function retrieveOptions() {
//log("retrieveOptions");
browser.storage.local.get().then(onGot, onError);
function onGot(options) {
cleanOptions(options);
cleanTimeData(options);
// Get current time/date
let timedate = new Date();
// Get current time in seconds
let now = Math.floor(Date.now() / 1000);
// Check whether a lockdown is currently active
for (let set = 1; set <= NUM_SETS; set++) {
let timedata = options[`timedata${set}`];
if (now < timedata[4]) {
document.querySelector(`#cancelLockdown${set}`).disabled = false;
}
}
// Check whether access to options should be prevented
for (let set = 1; set <= NUM_SETS; set++) {
if (options[`prevOpts${set}`]) {
// Get options
let timedata = options[`timedata${set}`];
let times = options[`times${set}`];
let minPeriods = getMinPeriods(times);
let limitMins = options[`limitMins${set}`];
let limitPeriod = options[`limitPeriod${set}`];
let periodStart = getTimePeriodStart(now, limitPeriod);
let conjMode = options[`conjMode${set}`];
let days = options[`days${set}`];
// Check day
let onSelectedDay = days[timedate.getDay()];
// Check time periods
let withinTimePeriods = false;
if (onSelectedDay && times != "") {
// Get number of minutes elapsed since midnight
let mins = timedate.getHours() * 60 + timedate.getMinutes();
// Check each time period in turn
for (let mp of minPeriods) {
if (mins >= mp.start && mins < mp.end) {
withinTimePeriods = true;
}
}
}
// Check time limit
let afterTimeLimit = false;
if (onSelectedDay && limitMins != "" && limitPeriod != "") {
// Check time period and time limit
if (timedata[2] == periodStart && timedata[3] >= (limitMins * 60)) {
afterTimeLimit = true;
}
}
// Check lockdown condition
let lockdown = (timedata[4] > now);
// Disable options if specified block conditions are fulfilled
if (lockdown
|| (!conjMode && (withinTimePeriods || afterTimeLimit))
|| (conjMode && (withinTimePeriods && afterTimeLimit))) {
// Disable options for this set
disableSetOptions(set);
}
}
}
for (let set = 1; set <= NUM_SETS; set++) {
// Get option values
let setName = options[`setName${set}`];
let sites = options[`sites${set}`].replace(/\s+/g, "\n");
let times = options[`times${set}`];
let limitMins = options[`limitMins${set}`];
let limitPeriod = options[`limitPeriod${set}`];
let conjMode = options[`conjMode${set}`];
let days = options[`days${set}`];
let blockURL = options[`blockURL${set}`];
let activeBlock = options[`activeBlock${set}`];
let countFocus = options[`countFocus${set}`];
let delayFirst = options[`delayFirst${set}`];
let delaySecs = options[`delaySecs${set}`];
let prevOpts = options[`prevOpts${set}`];
let prevAddons = options[`prevAddons${set}`];
let prevConfig = options[`prevConfig${set}`];
// Append custom set name to panel heading (if specified)
if (setName) {
document.querySelector(`#blockSetCustomName${set}`).innerText = ` (${setName})`;
} else {
document.querySelector(`#blockSetCustomName${set}`).innerText = "";
}
// Set component values
document.querySelector(`#setName${set}`).value = setName;
document.querySelector(`#sites${set}`).value = sites;
document.querySelector(`#times${set}`).value = times;
document.querySelector(`#limitMins${set}`).value = limitMins;
document.querySelector(`#limitPeriod${set}`).value = limitPeriod;
document.querySelector(`#conjMode${set}`).selectedIndex = conjMode ? 1 : 0;
for (let i = 0; i < 7; i++) {
document.querySelector(`#day${i}${set}`).checked = days[i];
}
document.querySelector(`#blockURL${set}`).value = blockURL;
document.querySelector(`#activeBlock${set}`).checked = activeBlock;
document.querySelector(`#countFocus${set}`).checked = countFocus;
document.querySelector(`#delayFirst${set}`).checked = delayFirst;
document.querySelector(`#delaySecs${set}`).value = delaySecs;
document.querySelector(`#prevOpts${set}`).checked = prevOpts;
document.querySelector(`#prevAddons${set}`).checked = prevAddons;
document.querySelector(`#prevConfig${set}`).checked = prevConfig;
}
}
function onError(error) {
warn("Cannot get options: " + error);
}
}
// Disable options for block set
//
function disableSetOptions(set) {
let items = [
"setName", "sites", "times", "allDay", "limitMins", "limitPeriod", "conjMode",
"day0", "day1", "day2", "day3", "day4", "day5", "day6",
"blockURL", "defaultPage", "delayingPage", "blankPage", "homePage",
"activeBlock", "countFocus", "delayFirst", "delaySecs",
"prevOpts", "prevAddons", "prevConfig", "cancelLockdown"
];
for (let item of items) {
let element = document.querySelector(`#${item}${set}`);
if (element) {
element.disabled = true;
}
}
}
/*** STARTUP CODE BEGINS HERE ***/
// Use HTML for first block set to create other block sets
let blockSetHTML = $("#panes").html();
for (let set = 2; set <= NUM_SETS; set++) {
let nextBlockSetHTML = blockSetHTML
.replace(/Block Set 1/g, `Block Set ${set}`)
.replace(/(id|for)="(\w+)1"/g, `$1="$2${set}"`);
$("#panes").append(nextBlockSetHTML);
}
// Set up JQuery UI widgets
$("#panes").accordion({
collapsible: false,
heightStyle: "content"
});
for (let set = 1; set <= NUM_SETS; set++) {
$(`#allDay${set}`).click(function (e) { $(`#times${set}`).val(ALL_DAY_TIMES); });
$(`#defaultPage${set}`).click(function (e) { $(`#blockURL${set}`).val(DEFAULT_BLOCK_URL); });
$(`#delayingPage${set}`).click(function (e) { $(`#blockURL${set}`).val(DELAYED_BLOCK_URL); });
$(`#blankPage${set}`).click(function (e) { $(`#blockURL${set}`).val("about:blank"); });
$(`#homePage${set}`).click(function (e) { $(`#blockURL${set}`).val("about:home"); });
$(`#showAdvOpts${set}`).click(function (e) {
$(`#showAdvOpts${set}`).css("display", "none");
$(`#advOpts${set}`).css("display", "inline");
});
}
$("#saveOptions").button();
$("#saveOptions").click(saveOptions);
$("#saveOptionsClose").button();
$("#saveOptionsClose").click(saveOptionsClose);
let alerts = ["alertBadTimes", "alertBadTimeLimit", "alertBadSeconds", "alertBadBlockURL"];
for (let alert of alerts) {
$(`#${alert}`).dialog({
autoOpen: false,
modal: true,
buttons: {
OK: function() { $(this).dialog("close"); }
}
});
}
$("#form").show();
document.addEventListener("DOMContentLoaded", retrieveOptions);