This repository has been archived by the owner on Jun 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prefs.js
1431 lines (1230 loc) · 34.5 KB
/
prefs.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
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"use strict";
/**
* @var preferences [Object] JSON that holds preferences
*
* @see background.js
* @see default-prefs.js
* @see https://github.com/An-dz/ScriptJaeger/wiki/Dev:-Preferences
*/
let preferences = {};
/**
* @var mergePreferences [Object] Rules to be merged
*
* This is used when merging new preferences so it
* can keep the stuff without costly operations.
*/
let mergingPref = {};
/**
* @var jaegerhut [Object] Badge icons, one for each policy
*/
const jaegerhut = {
0: {
offset: 1,
name: "allowall",
text: chrome.i18n.getMessage("policyAllowAll")
},
1: {
offset: 2,
name: "relaxed",
text: chrome.i18n.getMessage("policyRelaxed")
},
2: {
offset: 3,
name: "filtered",
text: chrome.i18n.getMessage("policyFiltered")
},
3: {
offset: 4,
name: "blockall",
text: chrome.i18n.getMessage("policyBlockAll")
},
true: {
offset: 2,
name: "blacklist",
text: chrome.i18n.getMessage("settingsRulesBlack")
},
false: {
offset: 1,
name: "whitelist",
text: chrome.i18n.getMessage("settingsRulesWhite")
},
null: {
offset: 0,
name: "undefined",
text: chrome.i18n.getMessage("settingsRulesNone")
},
delete: {
name: "delete",
text: chrome.i18n.getMessage("settingsDelete")
}
};
/**
* @brief Save settings and tell background script about it
*
* Saves the new settings and then send them to the background
* script so it's aware of changes and can replace them right away
*
* As the message API is async the saving might not be in the same
* order as the functions are called, so a timeout is implemented to
* reduce the calls to the background page and the storage API.
*/
function saveAndAlertBackground() {
clearTimeout(window.saveDelay);
window.saveDelay = setTimeout(() => {
// send message to background
chrome.runtime.sendMessage({
type: 3, // alert of preferences changes
prefs: preferences
});
// save preferences
chrome.storage.local.set({preferences: preferences});
}, 1000);
}
/* ====================================================================== */
/**
* @brief Display an alert box
*
* Shows an alert box in the middle of the page
*
* @param okaction [String] Action to execute when clicking the OK Button
* @param title [String] Title of the alert box
* @param msg [String] Message of the alert box (can be html)
* @param text [String] Textbox content, send null to hide
* @param cancelBtn [Boolean] If the cancel button must be shown
*/
function showAlert(okaction, title, msg, text, cancelBtn) {
const alertbox = document.getElementById("alertbox");
const textarea = document.getElementById("textarea");
textarea.hidden = text === null;
textarea.value = text;
alertbox.className = "visible";
alertbox.dataset.okaction = okaction;
alertbox.querySelector("h3").textContent = title;
alertbox.querySelector("p").innerHTML = msg;
alertbox.querySelector("button:last-child").hidden = !cancelBtn;
}
/**
* @brief Open rule selector
*
* Opens a 'dropdown' to allow changing the policy on the specified
* level
*
* @param e [Event] Event interface on the clicked DIV
*/
function openRuleSelector(e) {
const ruleNode = e.target;
const dropdown = document.getElementById("dropdown");
const rule = jaegerhut[ruleNode.dataset.rule];
const div = ruleNode.parentNode;
const li = div.parentNode;
const pos = div.getBoundingClientRect();
// position is relative to visible portion, need to add scrolled distance
pos.y = pos.y + window.scrollY -
// open at the position of the current rule
(rule.offset * document.querySelector("li").offsetHeight);
dropdown.style.top = `${pos.y}px`;
dropdown.style.left = `${ruleNode.offsetLeft}px`;
// copy domains and scripts url parts to dropdown element
dropdown.dataset.domains = li.dataset.domains;
dropdown.dataset.scripts = li.dataset.scripts;
// highlight selected
document.getElementById(rule.name).checked = true;
// hide options that are not possible
if (li.dataset.scripts === "[]") {
dropdown.className = "policy";
}
else {
dropdown.className = "bwl";
}
ruleNode.id = "active";
}
/**
* @brief Toggle sublevel rules
*
* Opens or closes the viewing of sublevel rules, like scripts or
* subdomain rules.
*
* @param e [Event] Event interface on the clicked DIV
*/
function toggleSubLevel(e) {
const li = e.target.parentNode;
if (!li.hasAttribute("class")) {
li.className = "show";
return;
}
li.removeAttribute("class");
li.querySelectorAll("li.show").forEach(node => {
node.removeAttribute("class");
});
}
/**
* @brief Delete rule
*
* Deletes the rules where the X button was pressed
*
* @param e [Event] Event interface on the clicked X button
*/
function deleteRule(e) {
// nodes
const div = e.target.parentNode;
const li = div.parentNode;
const ul = li.parentNode;
const liP = ul.parentNode;
const subUrls = JSON.parse(li.dataset.domains);
const scriptUrls = JSON.parse(li.dataset.scripts);
let level = preferences;
const isRules = scriptUrls.length > 0;
subUrls.forEach((url, index) => {
if (!isRules && subUrls.length - 1 === index) {
delete level.urls[url];
return;
}
level = level.urls[url];
});
if (isRules) {
level = level.rules;
}
scriptUrls.forEach((url, index) => {
if (scriptUrls.length - 1 === index) {
delete level.urls[url];
return;
}
level = level.urls[url];
});
// now that the rule has been deleted time to deal with the DOM
const subNumber = liP.querySelector(".number:not(.scripts)");
const scriptNumber = liP.querySelector(".number.scripts");
const number = isRules ? scriptNumber : subNumber;
// update numbers
if (number) {
--number.textContent;
if (number.textContent === "0") {
number.textContent = "";
}
}
// remove elements
li.remove();
// if no more children exists remove clicking event
if (subNumber.textContent === 0 && scriptNumber.textContent === 0) {
liP.removeAttribute("class");
const divP = liP.firstElementChild;
divP.removeAttribute("class");
divP.removeEventListener("click", toggleSubLevel);
}
// save preferences and alert the background page
saveAndAlertBackground();
}
/**
* @brief Change the rule
*
* Changes the rule for the specified level on the dropdown
*
* @param e [Event] Event interface on the clicked rule
*/
function changeRule(e) {
const dropdown = e.target.parentNode;
const subUrls = JSON.parse(dropdown.dataset.domains);
const scriptUrls = JSON.parse(dropdown.dataset.scripts);
let level = preferences;
const isRules = scriptUrls.length > 0;
subUrls.forEach(url => {
level = level.urls[url];
});
if (isRules) {
level = level.rules;
}
scriptUrls.forEach(url => {
level = level.urls[url];
});
let rule = parseInt(e.target.value, 10);
// -1 is for null
if (rule < 0) {
rule = null;
}
// rules is boolean
else if (isRules) {
rule = Boolean(rule);
}
// save rule key
level.rule = rule;
// change icon to reflect new rule
const active = document.getElementById("active");
active.src = `images/${e.target.id}38.png`;
active.alt = jaegerhut[rule].text[0];
active.title = jaegerhut[rule].text;
active.dataset.rule = rule;
active.removeAttribute("id");
saveAndAlertBackground();
}
/**
* @brief Change toplevel policy
*
* Changes the global policy or global private policy
*
* @param e [Event] Event interface on input change
*/
function changePolicy(e) {
preferences[e.target.name] = parseInt(e.target.value, 10);
saveAndAlertBackground();
}
/* ====================================================================== */
const ruleNodes = Object.freeze({
rule: document.createElement("img"),
url: document.createElement("span"),
subRules: document.createElement("span"),
scriptRules: document.createElement("span"),
delete: document.createElement("button"),
subRulesUl: document.createElement("ul"),
scriptRulesUl: document.createElement("ul"),
});
ruleNodes.rule.className = "rule";
ruleNodes.url.className = "site";
ruleNodes.subRules.className = "number";
ruleNodes.scriptRules.className = "number scripts";
ruleNodes.delete.className = "delete";
ruleNodes.subRulesUl.className = "subrules";
ruleNodes.scriptRulesUl.className = "scripts";
ruleNodes.subRules.title = chrome.i18n.getMessage("settingsNumLevels");
ruleNodes.scriptRules.title = chrome.i18n.getMessage("settingsNumScripts");
ruleNodes.delete.title = chrome.i18n.getMessage("settingsDelete");
ruleNodes.rule.tabIndex = 0;
/* ====================================================================== */
/**
* @brief Build an LI DOM Element for the rule
*
* Creates the LI for the preferences list for a single rule.
*
* @param url [String] The URL of the rule
* @param rule [Number/Boolean] Rule index for Jaegerhut index
* @param subRules [Number] number of sub rules
* @param scriptRules [Number] number of script rules
* @param subUrls [Array] List of url parts of the rule level
* @param scriptUrls [Array] List of url parts of the script rule
*
* @return [Element] An LI DOM Element for the rule
*/
function createRuleLi(url, rule, subRules, scriptRules, subUrls, scriptUrls) {
const ruleLi = document.createElement("li");
const ruleDiv = document.createElement("div");
ruleLi.dataset.domains = JSON.stringify(subUrls);
ruleLi.dataset.scripts = JSON.stringify(scriptUrls);
const nodes = Object.freeze({
rule: ruleNodes.rule.cloneNode(false),
url: ruleNodes.url.cloneNode(false),
subRules: ruleNodes.subRules.cloneNode(false),
scriptRules: ruleNodes.scriptRules.cloneNode(false),
delete: ruleNodes.delete.cloneNode(false),
});
// the URL
nodes.url.textContent = url;
// Jaegerhut: Rule applied
nodes.rule.src = `images/${jaegerhut[rule].name}38.png`;
nodes.rule.alt = jaegerhut[rule].text[0];
nodes.rule.title = jaegerhut[rule].text;
nodes.rule.dataset.rule = rule;
nodes.rule.addEventListener("click", openRuleSelector);
// add icons with the number of sub and script rules
if (subRules > 0) {
nodes.subRules.textContent = subRules;
}
if (scriptRules > 0) {
nodes.scriptRules.textContent = scriptRules;
}
// add listener for toggling subrules view
if (subRules > 0 || scriptRules > 0) {
ruleDiv.className = "pointer";
ruleDiv.tabIndex = 0;
ruleDiv.addEventListener("click", toggleSubLevel);
}
// delete button listener
nodes.delete.addEventListener("click", deleteRule);
// append nodes
ruleDiv.appendChild(nodes.rule);
ruleDiv.appendChild(nodes.url);
ruleDiv.appendChild(nodes.subRules);
ruleDiv.appendChild(nodes.scriptRules);
ruleDiv.appendChild(nodes.delete);
ruleLi.appendChild(ruleDiv);
return ruleLi;
}
/**
* @brief Fill the list with the rules
*
* Creates and injects the DOM elements to represent the rules
*
* @param rulesList [Array] Rules objects to be printed
* @param node [Element] Node where to inject the generated DOMs
*
* @note Sorting must be done beforehand
*/
function fillList(rulesList, node) {
rulesList.forEach(item => {
// get url parts, these are useful for knowing the rule we must change
const subUrls = JSON.parse(node.parentNode.dataset.domains);
const scriptUrls = JSON.parse(node.parentNode.dataset.scripts);
// we need to append the new url part in the correct part
const useScript = scriptUrls.length > 0 || node.className === "scripts";
const urlParts = useScript ? scriptUrls : subUrls;
urlParts.push(item[0]);
// print the url as a whole to make it easier for the user
const url = `${
urlParts[1] === undefined ? "" : (
urlParts[1].length > 0 ? `${urlParts[1]}.` : "*://"
)
}${
urlParts[0] || ""
}${
urlParts[2] || ""
}`;
const data = item[1];
// get the sub-levels
const subRules = Object.entries(data.urls);
const scriptRules = data.rules ? Object.entries(data.rules.urls) : [];
// build the DOM Element for the item
const ruleLi = createRuleLi(
url,
data.rule,
subRules.length,
scriptRules.length,
subUrls,
scriptUrls
);
const subRulesUl = ruleNodes.subRulesUl.cloneNode(false);
const scriptRulesUl = ruleNodes.scriptRulesUl.cloneNode(false);
// append everything together
ruleLi.appendChild(subRulesUl);
ruleLi.appendChild(scriptRulesUl);
// call recursively
fillList(subRules.sort(), subRulesUl);
fillList(scriptRules.sort(), scriptRulesUl);
node.appendChild(ruleLi);
});
}
/**
* @brief Display loaded preferences on screen
*
* Gets the current loaded settings and show them on the screen
*/
function showPreferences() {
// fill policy preferences
document.querySelector(
`input[name='rule'][value='${preferences.rule}']`
).checked = true;
document.querySelector(
`input[name='private'][value='${preferences.private}']`
).checked = true;
// ping pref
document.querySelector(
"input[type='checkbox']"
).checked = !preferences.ping;
// fill global blackwhitelist
fillList(
Object.entries(preferences.rules.urls).sort(),
document.getElementById("bwl")
);
// fill site specific settings
fillList(
Object.entries(preferences.urls).sort(),
document.getElementById("rules")
);
}
/**
* @brief Save preferences and rebuild UI
*
* When lots of changes in the preferences happen, this will
* save the new preferences and will rebuild the rules lists.
*/
function newPreferences() {
// clear the tables to allow the new rules to fill
document.getElementById("bwl").innerText = "";
document.getElementById("rules").innerText = "";
showPreferences();
saveAndAlertBackground();
}
/* ====================================================================== */
/**
* @brief Update merge checkboxes
*
* If a lower level is checked, upper levels should be checked too.
* If an upper level is unchecked all sublevel should too.
*
* @param event [Event] raised by the checked listener
*/
function checkboxMerge(event) {
const checked = event.target.checked;
const li = event.target.parentNode.parentNode;
// if enabling a level, all parent levels must be enabled too
if (checked) {
const upperLi = li.parentNode.parentNode;
// check upper level
if (upperLi.tagName === "LI") {
upperLi.querySelector("input").checked = true;
}
// rules that delete a point must check its children
// but others should not
if (!event.target.dataset.deleted) {
return;
}
}
// if disabling a level, all sublevels must be disabled too
li.querySelectorAll("ul > li > label > input").forEach(input => {
input.checked = checked;
});
}
/**
* @brief Appends an item to the merge settings UI
*
* This appends a single line (LI DOM) for a single rule in the
* merging settings UI (UL DOM).
*
* @param[in] url [String] Name of the rule level
* @param[in] current [String] Name of the current/old rule
* @param[in] change [String] Name of the merging/new rule
*
* @return [Element] constructed li html element
*/
function createMergeItem(url, current, change) {
const li = document.createElement("li");
const label = document.createElement("label");
label.className = "flex";
const checkbox = document.createElement("input");
checkbox.type = "checkbox";
checkbox.checked = true;
// add event that controls selection of parents and children
checkbox.addEventListener("change", checkboxMerge);
if (change === "deletes") {
change = "delete";
checkbox.dataset.deleted = true;
checkbox.title = chrome.i18n.getMessage("settingsEnabledCheckbox");
}
else if (change === "delete") {
checkbox.dataset.deleted = true;
checkbox.disabled = true;
checkbox.title = chrome.i18n.getMessage("settingsDisabledCheckbox");
}
const span = document.createElement("span");
span.innerText = url;
const currentImg = document.createElement("img");
currentImg.className = "rule";
currentImg.src = `images/${jaegerhut[current].name}38.png`;
currentImg.alt = jaegerhut[current].text[0];
currentImg.title = chrome.i18n.getMessage(
"settingsMergeCurrent", jaegerhut[current].text
);
const changeImg = document.createElement("img");
changeImg.className = "rule";
changeImg.src = `images/${jaegerhut[change].name}38.png`;
changeImg.alt = jaegerhut[change].text[0];
changeImg.title = chrome.i18n.getMessage(
"settingsMergeNew", jaegerhut[change].text
);
label.appendChild(checkbox);
label.appendChild(span);
label.appendChild(currentImg);
label.appendChild(changeImg);
li.appendChild(label);
return li;
}
/**
* @brief Builds the UI for merging preferences
*
* The preferences object that will be merged into the other does not
* need to have all keys.
*
* @note The `this` argument is sent by the function itself when a
* branch of the preferences is being deleted so that all subitems
* are added on the UI as being deleted as well.
*
* @param[in] this [Boolean] If this whole rule is being deleted
* @param[in] node [Element] DOM Element to inject element
* @param[in] from [Array] List of preferences to be merged
* @param[in] to [Object] Where the preferences must be merged into
*/
function checkMerge(node, from, to) {
from.forEach(site => {
const key = site[0];
const value = site[1];
// move rule to variable to prevent reference
let rule = value.rule;
let urls = value.urls ? Object.entries(value.urls) : [];
let rules = value.rules && value.rules.urls ?
Object.entries(value.rules.urls) : [];
const oldValue = {
rule: (to[key] && to[key].rule !== undefined) ?
to[key].rule : "delete",
rules: (to[key] && to[key].rules) ?
to[key].rules : {urls:{}},
urls: (to[key] && to[key].urls) ?
to[key].urls : {}
};
const toDelete = this || value.delete;
if (toDelete) {
rule = `delete${value.delete ? "s" : ""}`;
urls = Object.entries(oldValue.urls);
rules = Object.entries(oldValue.rules.urls);
}
else if (rule === undefined) {
rule = oldValue.rule;
}
const ulUrls = document.createElement("ul");
const ulRules = document.createElement("ul");
ulUrls.className = node.className;
ulRules.className = "scripts";
checkMerge.call(toDelete, ulUrls, urls, oldValue.urls);
checkMerge.call(toDelete, ulRules, rules, oldValue.rules.urls);
// we only show it on the UI if the new `rule` is different
// or if the other keys are present
if (
rule !== oldValue.rule ||
ulUrls.childElementCount > 0 ||
ulRules.childElementCount > 0
) {
const li = createMergeItem(key, oldValue.rule, rule);
if (!this) {
value.checkbox = li.firstElementChild.firstElementChild;
}
if (ulUrls.childElementCount > 0) {
li.className = "show";
li.appendChild(ulUrls);
}
if (ulRules.childElementCount > 0) {
li.className = "show";
li.appendChild(ulRules);
}
node.appendChild(li);
}
});
}
/**
* @brief Builds the UI for merging preferences
*
* The preferences object that will be merged into the other does not
* need to have all keys.
*
* @note The `this` argument is sent by the function itself when a
* branch of the preferences is being deleted so that all subitems
* are added on the UI as being deleted as well.
*
* @param[in] this [Boolean] If this whole rule is being deleted
* @param[in] node [Element] DOM Element to inject element
* @param[in] from [Object] Preferences to be merged
*
* @param[out] to [Object] Where the preferences must be merged into
*/
function buildMergeUI(mergingRules) {
const ul = document.querySelector("#alertbox ul");
const ulUrls = document.createElement("ul");
const ulRules = document.createElement("ul");
const liUrls = document.createElement("li");
const liRules = document.createElement("li");
ulUrls.className = "subrules";
ulRules.className = "scripts";
liUrls.textContent = chrome.i18n.getMessage("settingsRules");
liRules.textContent = chrome.i18n.getMessage("settingsScripts");
// it's already valid so no need to check type or content
const urls = mergingRules.urls ? Object.entries(mergingRules.urls) : [];
const rules = mergingRules.rules && mergingRules.rules.urls ?
Object.entries(mergingRules.rules.urls) : [];
checkMerge(ulUrls, urls, preferences.urls);
checkMerge(ulRules, rules, preferences.rules.urls);
ul.appendChild(liUrls);
ul.appendChild(ulUrls);
ul.appendChild(liRules);
ul.appendChild(ulRules);
}
/* ====================================================================== */
/**
* @brief Merges two preferences file
*
* The preferences object that will be merged into the other does
* not need to have all keys.
*
* @param[in] from [Object] Preferences to be merged
*
* @param[out] to [Object] Where the preferences must be merged into
*/
function mergePreferences(from, to) {
Object.entries(from).forEach(site => {
const key = site[0];
const value = site[1];
if (value.checkbox.checked === false) {
return;
}
if (value.delete === true) {
if (to[key] !== undefined) {
delete to[key];
}
return;
}
if (to[key] === undefined) {
to[key] = {
rule: null,
rules: {urls:{}},
urls: {}
};
}
if (value.rule !== undefined) {
to[key].rule = value.rule;
}
if (value.rules !== undefined) {
mergePreferences(value.rules.urls, to[key].rules.urls);
}
if (value.urls !== undefined) {
mergePreferences(value.urls, to[key].urls);
}
});
}
/**
* @brief Prints all levels the rule is under
*
* Builds an HTML with all the levels the problem is under.
*
* @param at [Array] Each level is an entry
*
* @return [String] HTML string
*/
function validatePrintLevels(at) {
return at.map(
level => chrome.i18n.getMessage("settingsInvalidUnder", level)
).join("<br>");
}
/**
* @brief Validates a preferences object
*
* Validation can be done at multiple levels as long as the level
* contains a 'rule' key. Validation is recursive and checks the
* entire object.
*
* @note Unknown keys are deleted
*
* @note Full validation check if the whole object is a valid
* preferences object, it will raise errors if any object is missing.
* Partial only checks if the values are correct and ignores if some
* keys are missing, this check is for validating merging operations.
*
* @param[in] obj [Object] The level in the object to validate
* @param[in] isFull [Boolean] If validation is full or partial
* @param[in] isRules [Boolean] If the level is a blacklist
* @param[in] at [Array] Array identifying the levels
*
* @param[out] warn [Array] Array that will contain warnings
*/
function validate(obj, isFull, isRules, at, warn) {
// this check is to ensure that first all necessary entries exist
if (isFull) {
if (obj.rule === undefined) {
throw new TypeError(
`${chrome.i18n.getMessage(
"settingsInvalidBooleanNull", "rule"
)}<span>${
validatePrintLevels(at)
}</span>`
);
}
if (obj.urls === undefined) {
throw new TypeError(
`${chrome.i18n.getMessage(
"settingsInvalidObject", "urls"
)}<span>${
validatePrintLevels(at)
}</span>`
);
}
if (!isRules && obj.rules === undefined) {
throw new TypeError(
`${chrome.i18n.getMessage(
"settingsInvalidObject", "rules"
)}<span>${
validatePrintLevels(at)
}</span>`
);
}
}
// validate each entry
Object.entries(obj).forEach(entry => {
const key = entry[0];
const value = entry[1];
if (key === "rule") {
// if rule is null then it's correct
if (value === null) {
return;
}
// otherwise more checks are necessary
// blacklist (rules key) uses boolean
if (isRules) {
if (typeof value !== "boolean") {
throw new TypeError(
`${chrome.i18n.getMessage(
"settingsInvalidBooleanNull", "rule"
)}<span>${
validatePrintLevels(at)
}</span>`
);
}
return;
}
// policy uses a number
if (typeof value !== "number") {
throw new TypeError(
`${chrome.i18n.getMessage(
"settingsInvalidNumberNull", "rule"
)}<span>${
validatePrintLevels(at)
}</span>`
);
}
if (value < 0 || value > 3) {
throw new RangeError(
`${chrome.i18n.getMessage(
"settingsInvalidRangeNull", "rule"
)}<span>${
validatePrintLevels(at)
}</span>`
);
}
return;
}
if (key === "rules") {
// blacklist (rules key) doesn't contain rules subkeys
if (isRules) {
throw new SyntaxError(
`${chrome.i18n.getMessage(
"settingsInvalidLevel", "rules"
)}<span>${
validatePrintLevels(at)
}</span>`
);
}
// policy objects must contain a rules key/object
if (typeof value !== "object") {
throw new TypeError(
`${chrome.i18n.getMessage(
"settingsInvalidObject", "rules"
)}<span>${
validatePrintLevels(at)
}</span>`
);
}
const atNew = [...at, "rules"];
// rules key/object must contain a urls key/object
if (typeof value.urls !== "object") {
throw new TypeError(
`${chrome.i18n.getMessage(
"settingsInvalidObject", "urls"
)}<span>${
validatePrintLevels(atNew)
}</span>`
);
}
// rules key should only have urls key
// this is here because of the full check
Object.keys(value).forEach(subkey => {
if (subkey !== "urls") {
warn.push(
`${chrome.i18n.getMessage(
"settingsWarningText", subkey
)}<span>${
validatePrintLevels(atNew)
}</span>`
);
delete value[subkey];
}
});
// validate children
Object.entries(value.urls).forEach(object => {
validate(
object[1],
isFull,
true,
[...at, "urls", object[0]],
warn
);
});
return;
}
if (key === "urls") {
// all policy and blacklist objects must contain a urls object
if (typeof value !== "object") {
throw new TypeError(
`${chrome.i18n.getMessage(
"settingsInvalidObject", "urls"
)}<span>${
validatePrintLevels(at)
}</span>`