/
popup.js
1178 lines (1072 loc) · 37.7 KB
/
popup.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
/*
Licensed per https://github.com/privacy-tech-lab/gpc-optmeowt/blob/main/LICENSE.md
privacy-tech-lab, https://privacytechlab.org/
*/
/*
popup.js
================================================================================
popup.js supplements and renders complex elements on popup.html
*/
import { stores, storage } from "../background/storage.js";
import { initIAB, isValidSignalIAB } from "../background/cookiesIAB.js";
import { csvGenerator } from "../common/csvGenerator.js";
import { modes } from "../data/modes.js";
import "../../node_modules/uikit/dist/css/uikit.min.css";
import "../../node_modules/animate.css/animate.min.css";
import "./styles.css";
import psl from "psl";
import "../../node_modules/uikit/dist/js/uikit.js";
import "../../node_modules/uikit/dist/js/uikit-icons.js";
import "../../node_modules/@popperjs/core/dist/umd/popper.js";
import tippy from "../../node_modules/tippy.js/dist/tippy-bundle.umd.js";
import UIkit from "uikit";
import Darkmode from "../theme/darkmode.js";
import {
addDomainToDomainlistAndRules,
removeDomainFromDomainlistAndRules,
updateRemovalScript,
} from "../common/editDomainlist.js";
import { reloadDynamicRules, addDynamicRule } from "../common/editRules.js";
// Global scope settings variables
var isEnabled;
var isDomainlisted;
var mode;
var parsedDomain;
// Protection mode data
var domainsInfo;
var wellknownInfo;
// Analysis mode data
var analysis;
var analysis_userend;
// Darkmode
const darkmode = new Darkmode();
// TODO: Update the 3rd party domain listeners to correctly toggle their 3rd- \
// TODO: --party sites, instead of the 1st party site (they all connect to that)
/******************************************************************************/
/******************************************************************************/
/********** # First-to-load popup components (essential) **********/
/******************************************************************************/
/******************************************************************************/
// Sets the current mode icon
function changeOptModeIcon() {
let optMode = document.getElementById("optMode");
let p = document.getElementById("p");
let a = document.getElementById("a");
a.style.display = "none";
p.style.display = "none";
// optMode badge icon
mode === modes.analysis ? (a.style.display = "") : (p.style.display = "");
}
// Changes-the-icon listener
function changeOptModeIconListenerCallback() {
let optMode = document.getElementById("optMode");
let p = document.getElementById("p");
let a = document.getElementById("a");
a.style.display = "none";
p.style.display = "none";
// optMode badge icon
mode === modes.analysis ? (a.style.display = "") : (p.style.display = "");
}
//Init: initialize darkmode button (NOTE: accesses global scope `mode`)
function generateDarkmodeElement() {
let darkSwitch = document.getElementById("darkSwitch");
let darkmodeText = "";
if (darkmode.isActivated()) {
darkmodeText = `<input
type="checkbox"
class="custom-control-input"
id="darkSwitch"
checked
/>`;
} else {
darkmodeText = `<input
type="checkbox"
class="custom-control-input"
id="darkSwitch"
/>`;
}
darkSwitch.outerHTML = darkmodeText;
document.getElementById("darkSwitch").addEventListener("click", () => {
chrome.runtime.sendMessage({
msg: "DARKSWITCH_PRESSED",
});
changeOptModeIconListenerCallback();
darkmode.toggle();
});
}
// Fetches the current domain
function getCurrentParsedDomain() {
return new Promise((resolve, reject) => {
try {
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
let tab = tabs[0];
let url = new URL(tab.url);
let parsed = psl.parse(url.hostname);
let domain = parsed.domain;
parsedDomain = domain; // for global scope variable
resolve(domain);
});
} catch (e) {
reject();
}
});
}
/**
* In sync with global scope `parsedDomain`
* @param {String} parsedDomain
*/
function renderFirstPartyDomain(parsedDomain) {
if (parsedDomain) {
document.getElementById("domain-title").innerHTML = parsedDomain;
initPopUpWalkthrough();
} else {
document.getElementById("domain-title").style.display = "none";
}
}
// Extension on/off renderer helper
function renderExtenionIsEnabledDisabled(isEnabled, isDomainlisted) {
if (isEnabled === undefined || isDomainlisted === undefined) {
document.getElementById("img").src = "../assets/play-circle-outline.svg";
document
.getElementById("enable-disable")
.setAttribute("uk-tooltip", "Enable");
} else if (isEnabled) {
document.getElementById("img").src = "../assets/pause-circle-outline.svg";
document
.getElementById("enable-disable")
.setAttribute("uk-tooltip", "Disable");
document.getElementById("content").style.opacity = "1";
document.getElementById("extension-disabled-message").style.opacity = "0";
document.getElementById("extension-disabled-message").style.display =
"none";
} else {
document.getElementById("extension-disabled-message").style.display = "";
document.getElementById("img").src = "../assets/play-circle-outline.svg";
document
.getElementById("enable-disable")
.setAttribute("uk-tooltip", "Enable");
document.getElementById("content").style.opacity = "0.1";
document.getElementById("extension-disabled-message").style.opacity = "1";
}
}
function turnonoff(isEnabled) {
if ("$BROWSER" == "chrome") {
if (isEnabled) {
chrome.scripting.updateContentScripts([
{
id: "2",
matches: ["<all_urls>"],
js: ["content-scripts/registration/gpc-remove.js"],
runAt: "document_start",
},
]);
addDynamicRule(4999, "*");
} else {
updateRemovalScript();
reloadDynamicRules();
}
}
}
function listenerExtensionIsEnabledDisabledButton(
isEnabled,
isDomainlisted,
mode
) {
document
.getElementById("enable-disable")
.addEventListener("click", async () => {
isEnabled = await storage.get(stores.settings, "IS_ENABLED");
if (isEnabled) {
document.getElementById("extension-disabled-message").style.display =
"";
document.getElementById("img").src =
"../assets/play-circle-outline.svg";
document
.getElementById("enable-disable")
.setAttribute("uk-tooltip", "Enable");
document.getElementById("content").style.opacity = "0.1";
document.getElementById("extension-disabled-message").style.opacity =
"1";
chrome.runtime.sendMessage({
msg: "TURN_ON_OFF",
data: { isEnabled: false },
});
} else {
document.getElementById("img").src =
"../assets/pause-circle-outline.svg";
document
.getElementById("enable-disable")
.setAttribute("uk-tooltip", "Disable");
document.getElementById("content").style.opacity = "1";
document.getElementById("extension-disabled-message").style.opacity =
"0";
document.getElementById("extension-disabled-message").style.display =
"none";
chrome.runtime.sendMessage({
msg: "TURN_ON_OFF",
data: { isEnabled: true },
});
}
turnonoff(isEnabled);
});
}
// Domain counter for Protection mode helper
async function renderDomainCounter() {
const domainlistValues = await storage.getAll(stores.domainlist);
let count = Object.keys(domainlistValues).filter((key) => {
return domainlistValues[key] == null;
}).length;
document.getElementById("visited-domains-stats").innerHTML = `
<p id = "domain-count" class="blue-heading" style="font-size:25px;
font-weight: bold">${count}</p> domains receiving signals
`;
}
// First party domain and the Do Not Sell listener helper
async function renderFirstPartyDomainDNSToggle() {
let checkbox = "";
let text = "";
if (parsedDomain) {
try {
const parsedDomainValue = await storage.get(
stores.domainlist,
parsedDomain
);
// TODO: Note: we must flip the logic here because of the logical truthiness flip in the domainlist
if (!parsedDomainValue) {
checkbox = `<input type="checkbox" id="input" checked/><span></span>`;
text = "Do Not Sell Enabled";
} else {
checkbox = `<input type="checkbox" id="input"/><span></span>`;
text = "Do Not Sell Disabled";
}
document.getElementById("switch-label").innerHTML = checkbox;
document.getElementById("more-info-text").innerHTML = text;
} catch (e) {
console.error(e);
document.getElementById("switch-label").innerHTML = checkbox;
document.getElementById("more-info-text").innerHTML = text;
}
} else {
document.getElementById("switch-label").innerHTML = checkbox;
document.getElementById("more-info-text").innerHTML = text;
}
}
async function listenerFirstPartyDomainDNSToggleCallback() {
chrome.runtime.sendMessage({ msg: "TURN_ON_OFF", data: { isEnabled: true } });
chrome.runtime.sendMessage({
msg: "CHANGE_IS_DOMAINLISTED",
data: { isDomainlisted: true },
});
const parsedDomainValue = await storage.get(stores.domainlist, parsedDomain);
let elemString = "";
if (!parsedDomainValue) {
elemString = "Do Not Sell Disabled";
addDomainToDomainlistAndRules(parsedDomain);
} else {
elemString = "Do Not Sell Enabled";
removeDomainFromDomainlistAndRules(parsedDomain);
}
updateRemovalScript();
document.getElementById("more-info-text").innerHTML = elemString;
}
function listenerFirstPartyDomainDNSToggle() {
document
.getElementById("switch-label")
.addEventListener("click", listenerFirstPartyDomainDNSToggleCallback);
}
function removeFirstPartyDomainDNSToggle() {
document
.getElementById("switch-label")
.removeEventListener("click", listenerFirstPartyDomainDNSToggleCallback);
document.getElementById("switch-label").innerHTML = "";
document.getElementById("more-info-text").innerHTML = "";
}
// Dropdown helpers
function renderDropdown1Toggle() {
if (
document.getElementById("dropdown-1-expandable").style.display === "none"
) {
document.getElementById("dropdown-chevron-1").src =
"../assets/chevron-down.svg";
document.getElementById("dropdown-1-expandable").style.display = "none";
document
.getElementById("dropdown-1")
.classList.remove("dropdown-tab-click");
document.getElementById("divider-4").style.display = "none";
} else {
document.getElementById("dropdown-chevron-1").src =
"../assets/chevron-up.svg";
document.getElementById("dropdown-1-expandable").style.display = "";
document.getElementById("dropdown-1").classList.add("dropdown-tab-click");
document.getElementById("divider-4").style.display = "";
}
}
function renderDropdown2Toggle() {
if (
document.getElementById("dropdown-2-expandable").style.display === "none"
) {
document.getElementById("dropdown-chevron-2").src =
"../assets/chevron-down.svg";
document.getElementById("dropdown-2-expandable").style.display = "none";
document
.getElementById("dropdown-2")
.classList.remove("dropdown-tab-click");
document.getElementById("divider-6").style.display = "none";
} else {
document.getElementById("dropdown-chevron-2").src =
"../assets/chevron-up.svg";
document.getElementById("dropdown-2-expandable").style.display = "";
document.getElementById("dropdown-2").classList.add("dropdown-tab-click");
document.getElementById("divider-6").style.display = "";
}
}
function listenerDropdown1ToggleCallback() {
if (
document.getElementById("dropdown-1-expandable").style.display === "none"
) {
document.getElementById("dropdown-chevron-1").src =
"../assets/chevron-up.svg";
document.getElementById("dropdown-1-expandable").style.display = "";
document.getElementById("dropdown-1").classList.add("dropdown-tab-click");
document.getElementById("divider-4").style.display = "";
} else {
document.getElementById("dropdown-chevron-1").src =
"../assets/chevron-down.svg";
document.getElementById("dropdown-1-expandable").style.display = "none";
document
.getElementById("dropdown-1")
.classList.remove("dropdown-tab-click");
document.getElementById("divider-4").style.display = "none";
}
}
function listenerDropdown2ToggleCallback() {
if (
document.getElementById("dropdown-2-expandable").style.display === "none"
) {
document.getElementById("dropdown-chevron-2").src =
"../assets/chevron-up.svg";
document.getElementById("dropdown-2-expandable").style.display = "";
document.getElementById("dropdown-2").classList.add("dropdown-tab-click");
document.getElementById("divider-6").style.display = "";
} else {
document.getElementById("dropdown-chevron-2").src =
"../assets/chevron-down.svg";
document.getElementById("dropdown-2-expandable").style.display = "none";
document
.getElementById("dropdown-2")
.classList.remove("dropdown-tab-click");
document.getElementById("divider-6").style.display = "none";
}
}
function listenerDropdown1Toggle() {
document
.getElementById("dropdown-1")
.addEventListener("click", listenerDropdown1ToggleCallback);
}
function listenerDropdown2Toggle() {
document
.getElementById("dropdown-2")
.addEventListener("click", listenerDropdown2ToggleCallback);
}
function removeListenerDropdown1Toggle() {
document
.getElementById("dropdown-1")
.removeEventListener("click", listenerDropdown1ToggleCallback);
}
function removeListenerDropdown2Toggle() {
document
.getElementById("dropdown-2")
.removeEventListener("click", listenerDropdown2ToggleCallback);
}
/******************************************************************************/
/******************************************************************************/
/********** # Inflates main content **********/
/******************************************************************************/
/******************************************************************************/
/**
* Redraws the popup for protection mode
*/
function showProtectionInfo() {
removeFirstPartyDomainDNSToggle();
removeListenerDropdown1Toggle();
removeListenerDropdown2Toggle();
document.getElementById("optMode-text").innerText = "Protection Mode";
document.getElementById("switch-label").innerHTML = "";
document.getElementById("more-info-body").style.display = "";
document.getElementById("more-info-text").innerHTML = "Do Not Sell Enabled!";
document.getElementById("dropdown-1").style.display = "";
document.getElementById("dropdown-2").style.display = "";
document.getElementById("dropdown-1-text").innerHTML = "3rd Party Domains";
document.getElementById("dropdown-2-text").innerHTML = "Website Response";
document.getElementById("dropdown-1-expandable").innerHTML = "";
document.getElementById("dropdown-2-expandable").innerHTML = "";
document.getElementById("dropdown-1-expandable").style.display = "none";
document.getElementById("dropdown-2-expandable").style.display = "none";
document.getElementById("visited-domains-stats").style.display = "";
document.getElementById("run-analysis").style.display = "none";
document.getElementById("stop-analysis").style.display = "none";
document.getElementById("misc-options").style.display = "none";
document.getElementById("download-analysis-data").style.display = "none";
document.getElementById("analysis-list").style.display = "none";
document.getElementById("domain-list").style.display = "";
// Generate `Do Not Sell Enabled` elem
renderDomainCounter(); // Render "X domains receiving signals" info section
renderFirstPartyDomainDNSToggle(); // Render 1P domain "DNS Enabled/Disabled" text+toggle
// Listeners associated with the buttons / toggles rendered above
listenerFirstPartyDomainDNSToggle();
listenerDropdown1Toggle();
listenerDropdown2Toggle();
chrome.runtime.sendMessage({
msg: "POPUP_PROTECTION",
data: null,
});
if ("$BROWSER" == "chrome") {
chrome.runtime.sendMessage({
msg: "POPUP_PROTECTION_REQUESTS",
data: null,
});
}
}
/**
* Redraws the popup for analysis mode
*/
function showAnalysisInfo() {
removeFirstPartyDomainDNSToggle();
removeListenerDropdown1Toggle();
removeListenerDropdown2Toggle();
document.getElementById("optMode-text").innerText = "Analysis Mode";
document.getElementById("switch-label").innerHTML = "";
document.getElementById("more-info-body").style.display = "none";
document.getElementById("more-info-text").innerHTML = "none";
document.getElementById("dropdown-1").style.display = "";
document.getElementById("dropdown-2").style.display = "none";
document.getElementById("dropdown-1-text").innerHTML = "Analysis Breakdown";
document.getElementById("dropdown-2-text").innerHTML = "";
document.getElementById("dropdown-1-expandable").innerHTML = "";
document.getElementById("dropdown-2-expandable").innerHTML = "";
document.getElementById("dropdown-1-expandable").style.display = "none";
document.getElementById("dropdown-2-expandable").style.display = "none";
document.getElementById("visited-domains-stats").style.display = "none";
document.getElementById("run-analysis").style.display = "";
document.getElementById("stop-analysis").style.display = "";
document.getElementById("misc-options").style.display = "";
document.getElementById("download-analysis-data").style.display = "";
document.getElementById("analysis-list").style.display = "";
document.getElementById("domain-list").style.display = "none";
listenerDropdown1Toggle();
chrome.runtime.sendMessage({
msg: "POPUP_ANALYSIS",
data: null,
});
}
/**
* In sync with global scope `mode`
* @param {Modes} mode - from modes.js
*/
async function switchMode(mode) {
changeOptModeIcon();
if (mode === modes.protection) {
showProtectionInfo();
} else {
showAnalysisInfo();
}
}
/**
* Initializes the popup window after DOM content is loaded
* @param {Object} event - contains information about the event
*/
document.addEventListener("DOMContentLoaded", async (event) => {
isEnabled = await storage.get(stores.settings, "IS_ENABLED");
isDomainlisted = await storage.get(stores.settings, "IS_DOMAINLISTED");
mode = await storage.get(stores.settings, "MODE");
parsedDomain = await getCurrentParsedDomain(); // This must happen first
renderExtenionIsEnabledDisabled(isEnabled, isDomainlisted); // Render global ENABLED/DISABLED mode
listenerExtensionIsEnabledDisabledButton(isEnabled);
renderFirstPartyDomain(parsedDomain); // Render 1P domain
generateDarkmodeElement(); // Render darkmode
switchMode(mode); // requires global scope mode to be loaded
});
/******************************************************************************/
/******************************************************************************/
/********** # Second-to-load components (dropdowns) **********/
/******************************************************************************/
/******************************************************************************/
/**
* Builds the listener to enable toggling 3rd party domains on/off in domainlist
* @param {String} requestDomain - the domain related to the element which
* the listener should be attached
*/
function addThirdPartyDomainDNSToggleListener(requestDomain) {
document
.getElementById(`input-${requestDomain}`)
.addEventListener("click", async () => {
chrome.runtime.sendMessage({
msg: "TURN_ON_OFF",
data: { isEnabled: true },
});
chrome.runtime.sendMessage({
msg: "CHANGE_IS_DOMAINLISTED",
data: { isDomainlisted: true },
});
const requestDomainValue = await storage.get(
stores.domainlist,
requestDomain
);
let elemString = "";
if (!requestDomainValue) {
elemString = "Do Not Sell Disabled";
addDomainToDomainlistAndRules(requestDomain);
} else {
elemString = "Do Not Sell Enabled";
removeDomainFromDomainlistAndRules(requestDomain);
}
updateRemovalScript();
document.getElementById(`dns-enabled-text-${requestDomain}`).innerHTML =
elemString;
});
}
/**
* Builds the requested domains HTML of the popup window
* @param {Object} requests - Contains all request domains for the current tab
* (requests = tabs[activeTabID].requestDomainS; passed from background page)
*/
async function buildDomains(requests) {
let domain = await getCurrentParsedDomain();
let items = "";
const domainlistKeys = await storage.getAllKeys(stores.domainlist)
const domainlistValues = await storage.getAll(stores.domainlist)
// Sets the 3rd party domain elements
for (let requestDomain in requests) {
if (requestDomain != domain) {
let checkbox = "";
let text = "";
// Find correct index
let index = domainlistKeys.indexOf(requestDomain);
if (index > -1 && !domainlistValues[index]) {
checkbox = `<input type="checkbox" id="input-${requestDomain}" checked/>`;
text = "Do Not Sell Enabled";
} else {
checkbox = `<input type="checkbox" id="input-${requestDomain}"/>`;
text = "Do Not Sell Disabled";
}
items += `
<li>
<div
class="blue-heading uk-flex-inline uk-width-1-1 uk-flex-center uk-text-center uk-text-bold uk-text-truncate"
style="font-size: medium"
id="domain"
>
${requestDomain}
</div>
<div uk-grid style="margin-top: 4%; ">
<div
id="dns-enabled-text-${requestDomain}"
class="uk-width-expand uk-margin-auto-vertical"
style="font-size: small;"
>
${text}
</div>
<div>
<div uk-grid>
<div class="uk-width-auto">
<label class="switch switch-smaller" id="switch-label-${requestDomain}">
<!-- Populate switch preference here -->
<!-- <input type="checkbox" id="input"/> -->
${checkbox}
<span class="tooltip-1"></span>
</label>
</div>
</div>
</div>
</div>
<!-- Response info -->
<div uk-grid uk-grid-row-collapse style="margin-top:0px;">
</div>
</li>
`;
}
}
document.getElementById("dropdown-1-expandable").innerHTML = items;
// Sets the 3rd party domain toggle listeners
for (let requestDomain in requests) {
if (requestDomain != domain) {
addThirdPartyDomainDNSToggleListener(requestDomain);
}
}
}
/**
* Builds the Well Known HTML for the popup window
* @param {Object} requests - Contains all well-known info in current tab
* (requests passed from contentScript.js page as of v1.1.3)
*/
async function buildWellKnown(requests) {
let explainer;
/*
This iterates over the cases of possible combinations of
having found a GPC policy, and whether or not a site respects
the signal or not, setting both the `explainer` and `tabDetails`
for GPC v1
*/
if (requests !== null && requests["gpc"] == true) {
explainer = `
<li>
<p class="uk-text-center uk-text-small uk-text-bold">
GPC Signals Accepted
</p>
</li>
<li>
<p class="uk-text-center uk-text-small">
This website respects GPC signals
</p>
</li>
`;
} else if (requests !== null && requests["gpc"] == false) {
explainer = `
<li>
<p class="uk-text-center uk-text-small uk-text-bold">
GPC Signals Rejected
</p>
</li>
<li>
<p class="uk-text-center uk-text-small">
This website does not respect GPC signals
</p>
</li>
`;
} else if (requests === null || requests["gpc"] == null) {
explainer = `
<li>
<p class="uk-text-center uk-text-small uk-text-bold">
GPC Policy Missing
</p>
</li>
<li>
<p class="uk-text-center uk-text-small">
It seems this website does not have a GPC signal processing policy!
</p>
</li>
`;
}
let wellknown =
requests !== null && requests["gpc"] != null
? `
<li class="uk-text-center uk-text-small">
Here is the GPC policy:
</li>
<li>
<pre class="wellknown-bg">
${JSON.stringify(requests, null, 4)
.replace(/['"\{\}\n]/g, "")
.replace(/,/g, "\n")}
</pre>
</li>
`
: ``;
document.getElementById(
"dropdown-2-expandable"
).innerHTML = `${explainer} ${wellknown}`;
}
/**
* Builds Analysis info dropdown in dropdown-1
*/
async function buildAnalysis(data) {
let pos = "../../../assets/cat-w-text/check1.png";
let neg = "../../../assets/cat-w-text/cross1.png";
let specs = `style= "
margin-right: -20px;
margin-left: auto;
margin-top: auto;
margin-bottom: auto;
padding-right: 5px;
padding-left: 5px;"
`;
let items = "";
let dnsLink = data.DO_NOT_SELL_LINK_EXISTS ? pos : neg;
let stringFound;
let gpcSent = data.SENT_GPC ? pos : neg;
let beforeGPCUSPAPI = data.USPAPI_BEFORE_GPC;
let afterGPCUSPAPI = data.USPAPI_AFTER_GPC;
let beforeGPCUSPCookies = data.USP_COOKIES_BEFORE_GPC;
let afterGPCUSPCookies = data.USP_COOKIES_AFTER_GPC;
let uspStringBeforeGPC;
let uspStringAfterGPC;
// Generate the US Privacy String BEFORE GPC is sent
// Give priority to the USPAPI over USP Cookie
if (
beforeGPCUSPAPI &&
beforeGPCUSPAPI[0] &&
beforeGPCUSPAPI[0]["uspString"]
) {
uspStringBeforeGPC = beforeGPCUSPAPI[0]["uspString"]; // USPAPI exists
} else {
if (
beforeGPCUSPCookies &&
beforeGPCUSPCookies[0] &&
beforeGPCUSPCookies[0]["value"]
) {
uspStringBeforeGPC = beforeGPCUSPCookies[0]["value"]; // USP Cookie exists
} else {
uspStringBeforeGPC = data.USPAPI_OPTED_OUT || data.USP_COOKIE_OPTED_OUT;
}
}
// Generate the US Privacy String AFTER GPC is sent
// Give priority to the USPAPI over USP Cookie
if (afterGPCUSPAPI && afterGPCUSPAPI[0] && afterGPCUSPAPI[0]["uspString"]) {
uspStringAfterGPC = afterGPCUSPAPI[0]["uspString"];
} else {
if (
afterGPCUSPCookies &&
afterGPCUSPCookies[0] &&
afterGPCUSPCookies[0]["value"]
) {
uspStringAfterGPC = afterGPCUSPCookies[0]["value"];
} else {
uspStringAfterGPC = data.USPAPI_OPTED_OUT || data.USP_COOKIE_OPTED_OUT;
}
}
if (!beforeGPCUSPAPI && !beforeGPCUSPCookies) {
stringFound = neg;
} else {
let existsUSP =
beforeGPCUSPAPI.length != 0 || beforeGPCUSPCookies.length != 0
? true
: false;
let existsAndIsValidBeforeGPCUSPAPI;
let existsAndIsValidBeforeGPCUSPCookies;
if (beforeGPCUSPAPI && beforeGPCUSPAPI[0] && beforeGPCUSPAPI[0].uspString) {
existsAndIsValidBeforeGPCUSPAPI = isValidSignalIAB(
beforeGPCUSPAPI[0].uspString
);
} else {
existsAndIsValidBeforeGPCUSPAPI = false;
}
if (
beforeGPCUSPCookies &&
beforeGPCUSPCookies[0] &&
beforeGPCUSPCookies[0].value
) {
existsAndIsValidBeforeGPCUSPCookies = isValidSignalIAB(
beforeGPCUSPCookies[0].value
);
} else {
existsAndIsValidBeforeGPCUSPCookies = false;
}
stringFound =
existsUSP &&
(existsAndIsValidBeforeGPCUSPAPI || existsAndIsValidBeforeGPCUSPCookies)
? pos
: neg;
}
let stringChanged;
let optedOut;
if (data.USPAPI_OPTED_OUT) {
optedOut = data.USPAPI_OPTED_OUT;
} else {
optedOut = data.USP_COOKIE_OPTED_OUT;
}
if (typeof optedOut === "string") {
if (optedOut === "PARSE_FAILED") {
stringChanged = neg;
} else if (optedOut === "NOT_IN_CA") {
stringChanged = neg;
}
} else {
stringChanged = optedOut ? pos : neg;
}
// Sets the 3rd party domain elements
items += `
<li>
<div uk-grid class="uk-grid-small uk-width-1-1" style="font-size: medium;">
<div class="domain uk-width-expand">
Do Not Sell Link
</div>
<img src = ${dnsLink} width = "40px" height = "40px" ${specs}>
</div>
</li>
<li>
<div uk-grid class="uk-grid-small uk-width-1-1" style="font-size: medium;">
<div class="domain uk-width-expand">
US Privacy String
</div>
<button class="uk-badge uspStringElem">${uspStringBeforeGPC}</button>
<img src = ${stringFound} width = "40px" height = "40px" ${specs}>
</div>
</li>
<li>
<div uk-grid class="uk-grid-small uk-width-1-1" style="font-size: medium;">
<div class="domain uk-width-expand">
GPC Signal Sent
</div>
<img src = ${gpcSent} width = "40px" height = "40px" ${specs}>
</div>
</li>
<li>
<div uk-grid class="uk-grid-small uk-width-1-1" style="font-size: medium;">
<div class="domain uk-width-expand">
US Privacy String Updated
</div>
<button class="uk-badge uspStringElem">${uspStringAfterGPC}</button>
<img src = ${stringChanged} width = "40px" height = "40px" ${specs}>
</div>
</li>`;
document.getElementById("dropdown-1-expandable").innerHTML = items;
}
/**
* Builds per-site compliance snippet
*/
async function buildComplianceInfo(data) {
let checkbox = "";
if (parsedDomain) {
try {
if (
data.DO_NOT_SELL_LINK_EXISTS &&
data.SENT_GPC &&
(data.USPAPI_OPTED_OUT || data.USP_COOKIE_OPTED_OUT) &&
(data.USPAPI_BEFORE_GPC.length != 0 ||
data.USP_COOKIES_BEFORE_GPC.length != 0) &&
(isValidSignalIAB(data.USPAPI_BEFORE_GPC[0].uspString) ||
isValidSignalIAB(data.USP_COOKIES_BEFORE_GPC[0].uspString))
) {
checkbox = `<div
id = "${parsedDomain} compliance"
class="uk-badge"
style="
margin-right: auto;
margin-left: auto;
margin-top: auto;
margin-bottom: auto;
padding-right: auto;
padding-left: auto;
background-color: white;
border: 1px solid rgb(64,107,202);
color: rgb(64,107,202);
font-size: 12px;
"
>
Compliant
</div>`;
} else {
checkbox = `<div
id = "${parsedDomain} compliance"
class="uk-badge"
style="
margin-right: auto;
margin-left: auto;
margin-top: auto;
margin-bottom: auto;
padding-right: auto;
padding-left: auto;
background-color: white;
border: 1px solid rgb(222,107,20);
color: rgb(222,107,20);
font-size: 12px;
"
>
Not Compliant
</div>`;
}
document.getElementById("switch-label").innerHTML = checkbox;
} catch (e) {
console.error(e);
document.getElementById("switch-label").innerHTML = checkbox;
}
} else {
document.getElementById("switch-label").innerHTML = checkbox;
}
}
/******************************************************************************/
/******************************************************************************/
/********** # Message handling **********/
/******************************************************************************/
/******************************************************************************/
// Initializng a longterm port with the top-level background for the onDisconnect event
let backgroundPort = chrome.runtime.connect({ name: "POPUP" });
// TODO: THIS IS OUTMODED IN MV3
// CHANGE THIS TO BE ASYNC NOW
backgroundPort.postMessage({ msg: "REQUEST_MODE" }); // queries control.js for mode
backgroundPort.onMessage.addListener(function (message) {
if (message.msg === "RESPONSE_MODE") {
// when mode is recieved from control.js
mode = message.data; // global mode variable
}
});
/**
* Listens for messages from background page that call functions to populate
* the popup badge counter and build the popup domain list HTML, respectively
*/
chrome.runtime.onMessage.addListener(function (message, _, __) {
if (message.msg === "RELOAD_DUE_TO_MODE_CHANGE") {
}
if (message.msg === "POPUP_PROTECTION_DATA") {
let { requests, wellknown } = message.data;
domainsInfo = requests;
wellknownInfo = wellknown;
buildDomains(requests);
buildWellKnown(wellknown);
}
if (message.msg === "POPUP_PROTECTION_DATA_REQUESTS") {
let requests = message.data;
buildDomains(requests);
}
if (message.msg === "POPUP_ANALYSIS_DATA") {
analysis = message.data.analysis;
analysis_userend = message.data.analysis_userend;
let data = analysis_userend[parsedDomain] || {};
buildAnalysis(data);
}
if (message.msg === "CSV_DATA_RESPONSE") {
const csvData = fetchcsvData();
csvData.then((csvData) => csvGenerator(csvData, message.data.titles));
}
});