forked from proginosko/LeechBlockNG
-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.js
822 lines (754 loc) · 26.3 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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
/* 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/. */
const DEFAULT_OPTIONS_FILE = "LeechBlockOptions.txt";
function log(message) { console.log("[LBNG] " + message); }
function warn(message) { console.warn("[LBNG] " + message); }
function getElement(id) { return document.getElementById(id); }
var gAccessConfirmed = false;
var gAccessRequiredInput;
// Save options to local storage (returns true if success)
//
function saveOptions() {
//log("saveOptions");
// Check format for text fields in block sets
for (let set = 1; set <= NUM_SETS; set++) {
// Get field values
let times = getElement(`times${set}`).value;
let limitMins = getElement(`limitMins${set}`).value;
let delaySecs = getElement(`delaySecs${set}`).value;
let blockURL = getElement(`blockURL${set}`).value;
// Check field values
if (!checkTimePeriodsFormat(times)) {
$("#tabs").tabs("option", "active", (set - 1));
$(`#times${set}`).focus();
$("#alertBadTimes").dialog("open");
return false;
}
if (!checkPosIntFormat(limitMins)) {
$("#tabs").tabs("option", "active", (set - 1));
$(`#limitMins${set}`).focus();
$("#alertBadTimeLimit").dialog("open");
return false;
}
if (!delaySecs || !checkPosIntFormat(delaySecs)) {
$("#tabs").tabs("option", "active", (set - 1));
$(`#delaySecs${set}`).focus();
$("#alertBadSeconds").dialog("open");
return false;
}
if (blockURL != DEFAULT_BLOCK_URL && blockURL != DELAYED_BLOCK_URL
&& !getParsedURL(blockURL).page) {
$("#tabs").tabs("option", "active", (set - 1));
$(`#blockURL${set}`).focus();
$("#alertBadBlockURL").dialog("open");
return false;
}
}
// Check format for text fields in general options
let warnSecs = getElement("warnSecs").value;
if (!checkPosIntFormat(warnSecs)) {
$("#tabs").tabs("option", "active", NUM_SETS);
$("warnSecs").focus();
$("#alertBadSeconds").dialog("open");
return false;
}
let options = {};
// General options
options["oa"] = getElement("optionsAccess").value;
options["password"] = getElement("accessPassword").value;
options["hpp"] = getElement("hidePassword").checked;
options["timerVisible"] = getElement("timerVisible").checked;
options["timerSize"] = getElement("timerSize").value;
options["timerLocation"] = getElement("timerLocation").value;
options["timerBadge"] = getElement("timerBadge").checked;
options["warnSecs"] = getElement("warnSecs").value;
options["warnImmediate"] = getElement("warnImmediate").checked;
options["contextMenu"] = getElement("contextMenu").checked;
options["toolsMenu"] = getElement("toolsMenu").checked;
options["matchSubdomains"] = getElement("matchSubdomains").checked;
for (let set = 1; set <= NUM_SETS; set++) {
// Get component values
let setName = getElement(`setName${set}`).value;
let sites = getElement(`sites${set}`).value;
sites = sites.replace(/\s+/g, " ").replace(/(^ +)|( +$)|(\w+:\/+)/g, "");
sites = sites.split(" ").sort().join(" "); // sort alphabetically
let times = getElement(`times${set}`).value;
let limitMins = getElement(`limitMins${set}`).value;
let limitPeriod = getElement(`limitPeriod${set}`).value;
let conjMode = getElement(`conjMode${set}`).selectedIndex == 1;
let days = [];
for (let i = 0; i < 7; i++) {
days.push(getElement(`day${i}${set}`).checked);
}
let blockURL = getElement(`blockURL${set}`).value;
let activeBlock = getElement(`activeBlock${set}`).checked;
let countFocus = getElement(`countFocus${set}`).checked;
let delayFirst = getElement(`delayFirst${set}`).checked;
let delaySecs = getElement(`delaySecs${set}`).value;
let prevOpts = getElement(`prevOpts${set}`).checked;
let prevAddons = getElement(`prevAddons${set}`).checked;
let prevSupport = getElement(`prevSupport${set}`).checked;
let sitesURL = getElement(`sitesURL${set}`).value;
let regexpBlock = getElement(`regexpBlock${set}`).value;
let regexpAllow = getElement(`regexpAllow${set}`).value;
let ignoreHash = getElement(`ignoreHash${set}`).checked;
// Get regular expressions to match sites
let regexps = getRegExpSites(sites, options["matchSubdomains"]);
// 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[`prevSupport${set}`] = prevSupport;
options[`sitesURL${set}`] = sitesURL;
options[`regexpBlock${set}`] = regexpBlock;
options[`regexpAllow${set}`] = regexpAllow;
options[`ignoreHash${set}`] = ignoreHash;
options[`blockRE${set}`] = regexps.block;
options[`allowRE${set}`] = regexps.allow;
options[`keywordRE${set}`] = regexps.keyword;
// Request permission to load sites from URL
if (sitesURL) {
let permissions = { origins: ["<all_urls>"] };
browser.permissions.request(permissions);
}
}
browser.storage.local.set(options).catch(
function (error) { warn("Cannot set options: " + error); }
);
// Notify extension that options have been updated
browser.runtime.sendMessage({ type: "options" });
$("#form").hide({ effect: "fade", complete: retrieveOptions });
return true;
}
// Save options and close tab
//
function saveOptionsClose() {
if (saveOptions()) {
closeOptions();
}
}
// Close options tab
//
function closeOptions() {
// 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}`];
let lockdown = (timedata[4] > now);
getElement(`cancelLockdown${set}`).disabled = !lockdown;
}
// 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 prevSupport = options[`prevSupport${set}`];
let sitesURL = options[`sitesURL${set}`];
let regexpBlock = options[`regexpBlock${set}`];
let regexpAllow = options[`regexpAllow${set}`];
let ignoreHash = options[`ignoreHash${set}`];
// Apply custom set name to tab (if specified)
if (setName) {
getElement(`blockSetName${set}`).innerText = setName;
} else {
getElement(`blockSetName${set}`).innerText = `Block Set ${set}`;
}
// Set component values
getElement(`setName${set}`).value = setName;
getElement(`sites${set}`).value = sites;
getElement(`times${set}`).value = times;
getElement(`limitMins${set}`).value = limitMins;
getElement(`limitPeriod${set}`).value = limitPeriod;
getElement(`conjMode${set}`).selectedIndex = conjMode ? 1 : 0;
for (let i = 0; i < 7; i++) {
getElement(`day${i}${set}`).checked = days[i];
}
getElement(`blockURL${set}`).value = blockURL;
getElement(`activeBlock${set}`).checked = activeBlock;
getElement(`countFocus${set}`).checked = countFocus;
getElement(`delayFirst${set}`).checked = delayFirst;
getElement(`delaySecs${set}`).value = delaySecs;
getElement(`prevOpts${set}`).checked = prevOpts;
getElement(`prevAddons${set}`).checked = prevAddons;
getElement(`prevSupport${set}`).checked = prevSupport;
getElement(`sitesURL${set}`).value = sitesURL;
getElement(`regexpBlock${set}`).value = regexpBlock;
getElement(`regexpAllow${set}`).value = regexpAllow;
getElement(`ignoreHash${set}`).checked = ignoreHash;
}
// General options
getElement("optionsAccess").value = options["oa"];
getElement("accessPassword").value = options["password"];
getElement("hidePassword").checked = options["hpp"];
getElement("timerVisible").checked = options["timerVisible"];
getElement("timerSize").value = options["timerSize"];
getElement("timerLocation").value = options["timerLocation"];
getElement("timerBadge").checked = options["timerBadge"];
getElement("warnSecs").value = options["warnSecs"];
getElement("warnImmediate").checked = options["warnImmediate"];
getElement("contextMenu").checked = options["contextMenu"];
getElement("toolsMenu").checked = options["toolsMenu"];
getElement("matchSubdomains").checked = options["matchSubdomains"];
confirmAccess(options);
}
function onError(error) {
warn("Cannot get options: " + error);
$("#alertRetrieveError").dialog("open");
}
}
// Confirm access to options
//
function confirmAccess(options) {
if (gAccessConfirmed) {
// Access already confirmed
$("#form").show({ effect: "fade" });
return;
}
let oa = options["oa"];
let password = options["password"];
let hpp = options["hpp"];
if (oa == 1 && password) {
gAccessRequiredInput = password;
if (hpp) {
$("#promptPasswordInput").attr("type", "password");
} else {
$("#promptPasswordInput").attr("type", "text");
}
$("#promptPasswordInput").val("");
$("#promptPassword").dialog("open");
$("#promptPasswordInput").focus();
} else if (oa > 1) {
let code = createAccessCode(32);
let codeText = code;
gAccessRequiredInput = code;
if (oa > 2) {
code = createAccessCode(32);
codeText += code;
gAccessRequiredInput += code;
}
if (oa > 3) {
code = createAccessCode(32);
codeText += "<br>" + code;
gAccessRequiredInput += code;
code = createAccessCode(32);
codeText += code;
gAccessRequiredInput += code;
}
$("#promptAccessCodeText").html(codeText);
$("#promptAccessCodeInput").val("");
$("#promptAccessCode").dialog("open");
$("#promptAccessCodeInput").focus();
} else {
gAccessConfirmed = true;
$("#form").show({ effect: "fade" });
}
}
// Export options
//
function exportOptions() {
let options = {};
for (let set = 1; set <= NUM_SETS; set++) {
// Get component values
let setName = getElement(`setName${set}`).value;
let sites = getElement(`sites${set}`).value;
sites = sites.replace(/\s+/g, " ").replace(/(^ +)|( +$)|(\w+:\/+)/g, "");
let times = getElement(`times${set}`).value;
let limitMins = getElement(`limitMins${set}`).value;
let limitPeriod = getElement(`limitPeriod${set}`).value;
let conjMode = getElement(`conjMode${set}`).selectedIndex == 1;
let days = [];
for (let i = 0; i < 7; i++) {
days.push(getElement(`day${i}${set}`).checked);
}
let blockURL = getElement(`blockURL${set}`).value;
let activeBlock = getElement(`activeBlock${set}`).checked;
let countFocus = getElement(`countFocus${set}`).checked;
let delayFirst = getElement(`delayFirst${set}`).checked;
let delaySecs = getElement(`delaySecs${set}`).value;
let prevOpts = getElement(`prevOpts${set}`).checked;
let prevAddons = getElement(`prevAddons${set}`).checked;
let prevSupport = getElement(`prevSupport${set}`).checked;
let sitesURL = getElement(`sitesURL${set}`).value;
let regexpBlock = getElement(`regexpBlock${set}`).value;
let regexpAllow = getElement(`regexpAllow${set}`).value;
let ignoreHash = getElement(`ignoreHash${set}`).checked;
// 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}`] = encodeDays(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[`prevSupport${set}`] = prevSupport;
options[`sitesURL${set}`] = sitesURL;
options[`regexpBlock${set}`] = regexpBlock;
options[`regexpAllow${set}`] = regexpAllow;
options[`ignoreHash${set}`] = ignoreHash;
}
// General options
options["oa"] = getElement("optionsAccess").value;
options["password"] = getElement("accessPassword").value;
options["hpp"] = getElement("hidePassword").checked;
options["timerVisible"] = getElement("timerVisible").checked;
options["timerSize"] = getElement("timerSize").value;
options["timerLocation"] = getElement("timerLocation").value;
options["timerBadge"] = getElement("timerBadge").checked;
options["warnSecs"] = getElement("warnSecs").value;
options["warnImmediate"] = getElement("warnImmediate").checked;
options["contextMenu"] = getElement("contextMenu").checked;
options["toolsMenu"] = getElement("toolsMenu").checked;
options["matchSubdomins"] = getElement("matchSubdomains").checked;
// Convert options to text lines
let lines = [];
for (let option in options) {
lines.push(option + "=" + options[option] + "\n");
}
// Create blob and download it
let blob = new Blob(lines, { type: "text/plain", endings: "native" });
var url = URL.createObjectURL(blob);
browser.downloads.download({ url: url, filename: DEFAULT_OPTIONS_FILE, saveAs: true });
}
// Import options
//
function importOptions() {
let file = getElement("importFile").files[0];
if (!file) {
$("#alertNoImportFile").dialog("open");
return;
}
// Read and process file
let reader = new FileReader();
reader.onload = processImportFile;
reader.readAsText(file);
function processImportFile(event) {
let text = event.target.result;
if (!text) {
$("#alertImportError").dialog("open");
return;
}
function isTrue(str) { return /^true$/i.test(str); }
// Extract options from text
let regexp = /^(\w+)=(.*)$/;
let lines = text.split(/[\n\r]+/);
let options = {};
let hasOptions = false;
for (let line of lines) {
let results = regexp.exec(line);
if (results) {
options[results[1]] = results[2];
hasOptions = true;
}
}
if (!hasOptions) {
$("#alertImportError").dialog("open");
return;
}
for (let set = 1; set <= NUM_SETS; set++) {
// Get option values
let setName = options[`setName${set}`];
let sites = options[`sites${set}`];
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 prevSupport = options[`prevSupport${set}`];
let sitesURL = options[`sitesURL${set}`];
let regexpBlock = options[`regexpBlock${set}`];
let regexpAllow = options[`regexpAllow${set}`];
let ignoreHash = options[`ignoreHash${set}`];
// Set component values
if (setName != undefined) {
let element = getElement(`setName${set}`);
if (!element.disabled) {
element.value = setName;
if (setName) {
getElement(`blockSetName${set}`).innerText = setName;
} else {
getElement(`blockSetName${set}`).innerText = `Block Set ${set}`;
}
}
}
if (sites != undefined) {
let element = getElement(`sites${set}`);
if (!element.disabled) {
element.value = sites.replace(/\s+/g, "\n");
}
}
if (times != undefined) {
let element = getElement(`times${set}`);
if (!element.disabled) {
element.value = times;
}
}
if (limitMins != undefined) {
let element = getElement(`limitMins${set}`);
if (!element.disabled) {
element.value = limitMins;
}
}
if (limitPeriod != undefined) {
let element = getElement(`limitPeriod${set}`);
if (!element.disabled) {
element.value = limitPeriod;
}
}
if (conjMode != undefined) {
let element = getElement(`conjMode${set}`);
if (!element.disabled) {
element.selectedIndex = isTrue(conjMode) ? 1 : 0;
}
}
if (days != undefined) {
days = decodeDays(days);
for (let i = 0; i < 7; i++) {
let element = getElement(`day${i}${set}`);
if (!element.disabled) {
element.checked = days[i];
}
}
}
if (blockURL != undefined) {
let element = getElement(`blockURL${set}`);
if (!element.disabled) {
element.value = blockURL;
}
}
if (activeBlock != undefined) {
let element = getElement(`activeBlock${set}`);
if (!element.disabled) {
element.checked = isTrue(activeBlock);
}
}
if (countFocus != undefined) {
let element = getElement(`countFocus${set}`);
if (!element.disabled) {
element.checked = isTrue(countFocus);
}
}
if (delayFirst != undefined) {
let element = getElement(`delayFirst${set}`);
if (!element.disabled) {
element.checked = isTrue(delayFirst);
}
}
if (delaySecs != undefined) {
let element = getElement(`delaySecs${set}`);
if (!element.disabled) {
element.value = delaySecs;
}
}
if (prevOpts != undefined) {
let element = getElement(`prevOpts${set}`);
if (!element.disabled) {
element.checked = isTrue(prevOpts);
}
}
if (prevAddons != undefined) {
let element = getElement(`prevAddons${set}`);
if (!element.disabled) {
element.checked = isTrue(prevAddons);
}
}
if (prevSupport != undefined) {
let element = getElement(`prevSupport${set}`);
if (!element.disabled) {
element.checked = isTrue(prevSupport);
}
}
if (sitesURL != undefined) {
let element = getElement(`sitesURL${set}`);
if (!element.disabled) {
element.value = sitesURL;
}
}
if (regexpBlock != undefined) {
let element = getElement(`regexpBlock${set}`);
if (!element.disabled) {
element.value = regexpBlock;
}
}
if (regexpAllow != undefined) {
let element = getElement(`regexpAllow${set}`);
if (!element.disabled) {
element.value = regexpAllow;
}
}
if (ignoreHash != undefined) {
let element = getElement(`ignoreHash${set}`);
if (!element.disabled) {
element.checked = isTrue(ignoreHash);
}
}
}
// General options
let oa = options["oa"];
let password = options["password"];
let hpp = options["hpp"];
let timerVisible = options["timerVisible"];
let timerSize = options["timerSize"];
let timerLocation = options["timerLocation"];
let timerBadge= options["timerBadge"];
let warnSecs = options["warnSecs"];
let warnImmediate = options["warnImmediate"];
let contextMenu = options["contextMenu"];
let toolsMenu = options["toolsMenu"];
let matchSubdomains = options["matchSubdomains"]
if (oa != undefined) {
getElement("optionsAccess").value = oa;
}
if (password != undefined) {
getElement("accessPassword").value = password;
}
if (hpp != undefined) {
getElement("hidePassword").checked = hpp;
}
if (timerVisible != undefined) {
getElement("timerVisible").checked = timerVisible;
}
if (timerSize != undefined) {
getElement("timerSize").value = timerSize;
}
if (timerLocation != undefined) {
getElement("timerLocation").value = timerLocation;
}
if (timerBadge != undefined) {
getElement("timerBadge").checked = timerBadge;
}
if (warnSecs != undefined) {
getElement("warnSecs").value = warnSecs;
}
if (warnImmediate != undefined) {
getElement("warnImmediate").value = warnImmediate;
}
if (contextMenu != undefined) {
getElement("contextMenu").checked = contextMenu;
}
if (toolsMenu != undefined) {
getElement("toolsMenu").checked = toolsMenu;
}
if (matchSubdomains != undefined) {
getElement("matchSubdomains").checked = matchSubdomains;
}
$("#alertImportSuccess").dialog("open");
}
}
// 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", "prevSupport", "sitesURL",
"regexpBlock", "clearRegExpBlock", "genRegExpBlock",
"regexpAllow", "clearRegExpAllow", "genRegExpAllow",
"ignoreHash", "cancelLockdown"
];
for (let item of items) {
let element = getElement(`${item}${set}`);
if (element) {
element.disabled = true;
}
}
}
// Initialize access control prompt
//
function initAccessControlPrompt(prompt) {
// Create functions for buttons
let dialogButtons = {
OK: function () {
let input = $(`#${prompt}Input`);
if (input.val() == gAccessRequiredInput) {
gAccessConfirmed = true;
$("#form").show({ effect: "fade" });
$(`#${prompt}`).dialog("close");
} else {
input.val("");
input.effect({ effect: "highlight", color: "#ff0000" });
input.focus();
}
},
Cancel: function () {
$(`#${prompt}`).dialog("close");
}
};
// Initialize prompt dialog
$(`#${prompt}`).dialog({
autoOpen: false,
modal: true,
width: 600,
buttons: dialogButtons,
close: function (event, ui) { if (!gAccessConfirmed) closeOptions(); }
});
// Connect ENTER key to OK button
$(`#${prompt}Input`).keydown(
function (event) {
if (event.which == 13) {
dialogButtons.OK();
}
}
);
}
/*** STARTUP CODE BEGINS HERE ***/
// Use HTML for first block set to create other block sets
let tabHTML = $("#tabBlockSet1").html();
let setHTML = $("#blockSet1").html();
for (let set = 2; set <= NUM_SETS; set++) {
let nextTabHTML = tabHTML
.replace(/Block Set 1/g, `Block Set ${set}`)
.replace(/(id|href)="(#?\w+)1"/g, `$1="$2${set}"`);
let nextSetHTML = setHTML
.replace(/(id|for)="(\w+)1"/g, `$1="$2${set}"`);
$("#tabGeneral").before(`<li id="tabBlockSet${set}">${nextTabHTML}</li>`);
$("#paneGeneral").before(`<div id="blockSet${set}">${nextSetHTML}</div>`);
}
// Set up JQuery UI widgets
$("#tabs").tabs();
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", "block");
});
$(`#clearRegExpBlock${set}`).click(function (e) { $(`#regexpBlock${set}`).val(""); });
$(`#genRegExpBlock${set}`).click(function (e) {
let sites = $(`#sites${set}`).val();
let matchSubdomains = getElement("matchSubdomains").checked;
$(`#regexpBlock${set}`).val(getRegExpSites(sites, matchSubdomains).block);
});
$(`#clearRegExpAllow${set}`).click(function (e) { $(`#regexpAllow${set}`).val(""); });
$(`#genRegExpAllow${set}`).click(function (e) {
let sites = $(`#sites${set}`).val();
let matchSubdomains = getElement("matchSubdomains").checked;
$(`#regexpAllow${set}`).val(getRegExpSites(sites, matchSubdomains).allow);
});
$(`#cancelLockdown${set}`).click(function (e) {
browser.runtime.sendMessage({ type: "lockdown", set: set });
this.disabled = true;
$("#alertLockdownCancel").dialog("open");
});
$(`#advOpts${set}`).css("display", "none");
}
$("#exportOptions").click(exportOptions);
$("#importOptions").click(importOptions);
$("#saveOptions").button();
$("#saveOptions").click(saveOptions);
$("#saveOptionsClose").button();
$("#saveOptionsClose").click(saveOptionsClose);
// Initialize alert dialogs
$("div[id^='alert']").dialog({
autoOpen: false,
modal: true,
width: 600,
buttons: {
OK: function () { $(this).dialog("close"); }
}
});
// Initialize access control prompts
initAccessControlPrompt("promptPassword");
initAccessControlPrompt("promptAccessCode");
document.addEventListener("DOMContentLoaded", retrieveOptions);