forked from jrie/flagCookies
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cookies.js
2631 lines (2128 loc) · 122 KB
/
cookies.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
const logData = {}; // The log data we seen as a report to the settings view
const logTime = {};
const cookieData = {}; // Storage for cookie shadow, for the interface only!
const removedData = {};
const removedByDomain = {};
const permittedData = {};
const openTabData = {};
const cookieCount = {};
let removedByUser = 0;
const localStorageData = {};
const sessionStorageData = {};
// Chrome
const useChrome = typeof (browser) === 'undefined';
// Localization
const getMsg = useChrome ? getChromeMessage : getFirefoxMessage;
// Firefox
let browserActionAPI;
if (!useChrome) {
browserActionAPI = typeof (browser.action) === 'undefined' ? browser.browserAction : browser.action;
} else {
browserActionAPI = typeof (chrome.action) === 'undefined' ? chrome.browserAction : chrome.action;
}
function getChromeMessage (messageName, params) {
if (params !== undefined) return chrome.i18n.getMessage(messageName, params);
return chrome.i18n.getMessage(messageName);
}
function getFirefoxMessage (messageName, params) {
if (params !== undefined) return browser.i18n.getMessage(messageName, params);
return browser.i18n.getMessage(messageName);
}
async function getStoreValue (key, keyStore, targetValue) {
let value = {};
if (keyStore !== undefined) {
let request = {};
if (targetValue !== undefined) {
request = { [keyStore]: { [key]: targetValue } };
} else {
request = { [keyStore]: key };
}
if (useChrome) {
value = await chrome.storage.local.get(request);
} else {
value = await browser.storage.local.get(request);
}
} else {
if (useChrome) {
value = await chrome.storage.local.get(key);
} else {
value = await browser.storage.local.get(key);
}
}
if (Object.keys(value).length === 0) {
return;
}
return value;
}
async function setStoreValue (key, keyStore, targetValue) {
if (keyStore !== undefined) {
let request = {};
if (targetValue !== undefined) {
request = { [keyStore]: { [key]: targetValue } };
} else {
request = { [keyStore]: key };
}
if (useChrome) {
await chrome.storage.local.set(request);
} else {
await browser.storage.local.set(request);
}
} else {
if (useChrome) {
await chrome.storage.local.set(key);
} else {
await browser.storage.local.set(key);
}
}
return true;
}
async function clearCookiesWrapper (action, cookieDetails, currentTab) {
if (currentTab === undefined || currentTab.url === undefined || currentTab.url === null) {
return;
}
let contextName = 'default';
if (currentTab.cookieStoreId !== undefined) {
contextName = currentTab.cookieStoreId;
}
const tabWindowId = currentTab.windowId;
const tabTabId = currentTab.id;
const cookieDomainDetected = currentTab.url.replace(/^(http:|https:)\/\//i, '').replace(/^\./, '').match(/.[^/]*/);
if (cookieDomainDetected === null) {
return;
}
const cookieDomain = cookieDomainDetected[0];
let firstPartyIsolate = null;
if (cookieDetails !== null && cookieDetails.firstPartyDomain !== undefined) {
firstPartyIsolate = cookieDetails.firstPartyDomain;
}
let cookieList = [];
const splittedDomain = cookieDomain.split('.');
const rootDomain = splittedDomain.splice(splittedDomain.length - 2, 2).join('.');
if (!useChrome) {
const domainHttp = 'http://' + cookieDomain;
const domainHttps = 'https://' + cookieDomain;
const domainDot = '.' + cookieDomain;
const rootDomainDot = '.' + rootDomain;
const cookiesBase = await browser.cookies.getAll({ domain: cookieDomain, firstPartyDomain: firstPartyIsolate, storeId: contextName });
const cookiesBaseWWW = await browser.cookies.getAll({ domain: 'www.' + cookieDomain, firstPartyDomain: firstPartyIsolate, storeId: contextName });
const cookiesSec = await browser.cookies.getAll({ domain: cookieDomain, secure: true, firstPartyDomain: firstPartyIsolate, storeId: contextName });
const cookies2 = await browser.cookies.getAll({ domain: domainDot, firstPartyDomain: firstPartyIsolate, storeId: contextName });
const cookiesSec2 = await browser.cookies.getAll({ domain: domainDot, secure: true, firstPartyDomain: firstPartyIsolate, storeId: contextName });
const cookies3 = await browser.cookies.getAll({ domain: domainHttp, firstPartyDomain: firstPartyIsolate, storeId: contextName });
const cookiesSec3 = await browser.cookies.getAll({ domain: domainHttp, secure: true, firstPartyDomain: firstPartyIsolate, storeId: contextName });
const cookies4 = await browser.cookies.getAll({ domain: domainHttps, firstPartyDomain: firstPartyIsolate, storeId: contextName });
const cookiesSec4 = await browser.cookies.getAll({ domain: domainHttps, secure: true, firstPartyDomain: firstPartyIsolate, storeId: contextName });
const cookies5 = await browser.cookies.getAll({ domain: rootDomain, firstPartyDomain: firstPartyIsolate, storeId: contextName });
const cookiesSec5 = await browser.cookies.getAll({ domain: rootDomain, secure: true, firstPartyDomain: firstPartyIsolate, storeId: contextName });
const cookies6 = await browser.cookies.getAll({ domain: rootDomainDot, firstPartyDomain: firstPartyIsolate, storeId: contextName });
const cookiesSec6 = await browser.cookies.getAll({ domain: rootDomainDot, secure: true, firstPartyDomain: firstPartyIsolate, storeId: contextName });
cookieList = [cookiesBase, cookiesBaseWWW, cookiesSec, cookies2, cookiesSec2, cookies3, cookiesSec3, cookies4, cookiesSec4, cookies5, cookiesSec5, cookies6, cookiesSec6];
} else {
const domainSplit = cookieDomain.split('.');
const targetDomain = domainSplit.splice(domainSplit.length - 2, 2).join('.');
const cookiesBase = await chrome.cookies.getAll({ domain: cookieDomain });
const cookiesRoot = await chrome.cookies.getAll({ domain: targetDomain });
const cookiesRootDot = await chrome.cookies.getAll({ domain: '.' + targetDomain });
cookieList = [cookiesBase, cookiesRoot, cookiesRootDot];
if (openTabData !== undefined && openTabData[tabWindowId] !== undefined && openTabData[tabWindowId][tabTabId] !== undefined) {
for (const tabUrl of Object.keys(openTabData[tabWindowId][tabTabId])) {
const targetURL = openTabData[tabWindowId][tabTabId][tabUrl].d;
cookieList.push(await chrome.cookies.getAll({ domain: targetURL }));
}
}
}
const cookies = [];
for (const list of cookieList) {
for (const cookie of list) {
let hasCookie = false;
for (const cookieEntry of cookies) {
if (cookieEntry.name === cookie.name && cookieEntry.domain === cookie.domain) {
hasCookie = true;
break;
}
}
if (hasCookie) {
continue;
}
for (const key of Object.keys(cookie)) {
switch (key) {
case 'name':
case 'value':
case 'domain':
case 'path':
case 'secure':
case 'expirationDate':
case 'firstPartyDomain':
case 'partitionKey':
// case 'storeId':
continue;
default:
delete cookie[key];
continue;
}
}
cookies.push(cookie);
addTabURLtoDataList(currentTab, { url: currentTab.url }, cookie.domain);
}
}
preSetMouseOverTitle(contextName, currentTab.id);
if (cookies.length === 0) return;
clearCookiesAction(action, cookies, currentTab);
}
async function handleTabChange (activeInfo) {
let tab = null;
if (useChrome) {
tab = await chrome.tabs.get(activeInfo.tabId);
} else {
tab = await browser.tabs.get(activeInfo.tabId);
}
const activeTabUrl = tab.url.toLowerCase();
if (activeTabUrl.startsWith('chrome:') || activeTabUrl.startsWith('about:') || activeTabUrl.startsWith('edge:')) {
return;
}
if (useChrome) {
chrome.tabs.sendMessage(activeInfo.tabId, { getStorageData: true });
return;
}
browser.tabs.sendMessage(activeInfo.tabId, { getStorageData: true });
}
function setTabForClearCookies (tab) {
clearCookiesWrapper(getMsg('ActionDomainClearByButton'), null, tab);
}
async function clearByDomainJob (request, sender, sendResponse) {
const tabId = request.tabId;
const windowId = request.windowId;
const contextName = request.contextName;
const cookieDomain = request.cookieDomain;
const rootDomain = request.rootDomain;
let cookieCount = cookieData[contextName][windowId][tabId][cookieDomain].length;
let removedCookies = 0;
let data = null;
if (useChrome) {
data = await chrome.storage.local.get();
} else {
data = await browser.storage.local.get();
}
const hasGlobalProfile = data.flagCookies_accountMode !== undefined && data.flagCookies_accountMode[contextName] !== undefined && data.flagCookies_accountMode[contextName][rootDomain] !== undefined && data.flagCookies_accountMode[contextName][rootDomain] === true;
if (hasGlobalProfile) {
return false;
}
const hasLogged = data.flagCookies_logged !== undefined && data.flagCookies_logged[contextName] !== undefined && data.flagCookies_logged[contextName][rootDomain] !== undefined;
const doRemoveByUser = data.flagCookies_removeUserDeleted !== undefined && data.flagCookies_removeUserDeleted === true;
let index = 0;
const cookiesCopy = Array.from(cookieData[contextName][windowId][tabId][cookieDomain]);
for (const cookie of cookiesCopy) {
if (hasLogged && data.flagCookies_logged[contextName][rootDomain][cookieDomain] !== undefined && data.flagCookies_logged[contextName][rootDomain][cookieDomain][cookie.name] !== undefined && data.flagCookies_logged[contextName][rootDomain][cookieDomain][cookie.name] === true) {
++index;
continue;
}
const details = { url: 'https://' + cookieDomain + cookie.path, name: cookie.name };
const details2 = { url: 'http://' + cookieDomain + cookie.path, name: cookie.name };
if (useChrome) {
if (await chrome.cookies.get(details) === null && await chrome.cookies.get(details2) === null) {
--cookieCount;
if (doRemoveByUser) {
cookieData[contextName][windowId][tabId][cookieDomain].splice(index, 1);
continue;
}
} else if (await chrome.cookies.remove(details) !== null || await chrome.cookies.remove(details2) !== null) {
++removedCookies;
cookieData[contextName][windowId][tabId][cookieDomain][index].fgCleared = true;
if (doRemoveByUser) {
cookieData[contextName][windowId][tabId][cookieDomain].splice(index, 1);
continue;
}
}
} else {
if (await browser.cookies.get(details) === null && await browser.cookies.get(details2) === null) {
--cookieCount;
if (doRemoveByUser) {
cookieData[contextName][windowId][tabId][cookieDomain].splice(index, 1);
continue;
}
} else if ((await browser.cookies.remove(details) !== null && await browser.cookies.get(details) === null) || (await browser.cookies.remove(details2) !== null && await browser.cookies.get(details2) === null)) {
++removedCookies;
cookieData[contextName][windowId][tabId][cookieDomain][index].fgCleared = true;
if (doRemoveByUser) {
cookieData[contextName][windowId][tabId][cookieDomain].splice(index, 1);
continue;
}
}
}
++index;
}
if (removedCookies === cookieCount || removedCookies !== 0) {
if (cookieData[contextName][windowId][tabId][cookieDomain].length === 0) {
delete cookieData[contextName][windowId][tabId][cookieDomain];
if (Object.keys(cookieData[contextName][windowId][tabId]).length === 1 && cookieData[contextName][windowId][tabId].fgRoot !== undefined) {
delete cookieData[contextName][windowId][tabId];
if (Object.keys(cookieData[contextName][windowId]).length === 0) {
delete cookieData[contextName][windowId];
if (Object.keys(cookieData[contextName]).length === 0) {
delete cookieData[contextName];
}
}
}
}
removedByUser += removedCookies;
preSetMouseOverTitle(contextName, tabId);
return true;
}
return false;
}
function handleMessage (request, sender, sendResponse) {
if (request.clearOnActivation !== undefined && request.tabId !== undefined) {
if (useChrome) {
chrome.tabs.get(request.tabId).then(setTabForClearCookies);
return;
}
browser.tabs.get(request.tabId).then(setTabForClearCookies);
return;
}
if (request.clearStorage !== undefined && request.tabId !== undefined) {
if (useChrome) {
chrome.tabs.sendMessage(request.tabId, { clearStorage: request.clearStorage });
} else {
browser.tabs.sendMessage(request.tabId, { clearStorage: request.clearStorage });
}
return;
}
if (request.clearByDomain !== undefined && request.clearByDomain === true) {
return clearByDomainJob(request, sender, sendResponse);
}
if (request.getLocalData !== undefined && request.windowId !== undefined && request.tabId !== undefined) {
const sessionData = { local: {}, session: {} };
if (localStorageData[request.windowId] !== undefined && localStorageData[request.windowId][request.tabId] !== undefined) {
sessionData.local = localStorageData[request.windowId][request.tabId];
}
if (sessionStorageData[request.windowId] !== undefined && sessionStorageData[request.windowId][request.tabId] !== undefined) {
sessionData.session = sessionStorageData[request.windowId][request.tabId];
}
sendResponse(sessionData);
return;
}
if (request.local !== undefined && request.session !== undefined) {
const tab = sender.tab;
let contextName = 'default';
if (tab.cookieStoreId !== undefined) {
contextName = tab.cookieStoreId;
}
const tabWindowId = tab.windowId;
const tabTabId = tab.id;
if (localStorageData[tabWindowId] === undefined) localStorageData[tabWindowId] = {};
localStorageData[tabWindowId][tabTabId] = {};
if (sessionStorageData[tabWindowId] === undefined) sessionStorageData[tabWindowId] = {};
sessionStorageData[tabWindowId][tabTabId] = {};
for (const key of Object.keys(request.local)) {
try {
localStorageData[tabWindowId][tabTabId][key] = JSON.parse(request.local[key]);
} catch {
localStorageData[tabWindowId][tabTabId][key] = request.local[key];
}
}
for (const key of Object.keys(request.session)) {
try {
sessionStorageData[tabWindowId][tabTabId][key] = JSON.parse(request.session[key]);
} catch {
sessionStorageData[tabWindowId][tabTabId][key] = request.session[key];
}
}
preSetMouseOverTitle(contextName, tabTabId);
return;
}
if (request.updateCookies !== undefined && request.cookies !== undefined && request.targetDomain !== undefined && request.windowId !== undefined && request.tabId !== undefined && request.storeId !== undefined) {
// TODO: Add action if targetDomain !== null
if (cookieData[request.storeId] !== undefined && cookieData[request.storeId][request.windowId] !== undefined && cookieData[request.storeId][request.windowId][request.tabId] !== undefined) {
if (request.targetDomain === null) {
for (const domainKey of Object.keys(request.cookies)) {
if (domainKey === 'fgRoot') {
continue;
}
cookieData[request.storeId][request.windowId][request.tabId][domainKey] = request.cookies[domainKey];
}
} else if (cookieData[request.storeId][request.windowId][request.tabId][request.targetDomain] !== undefined) {
cookieData[request.storeId][request.windowId][request.tabId][request.targetDomain] = request.cookies[request.targetDomain];
} else {
sendResponse({ updateStatus: false });
return;
}
sendResponse({ updateStatus: true });
return;
}
sendResponse({ updateStatus: false });
return;
}
if (request.getCookies !== undefined && request.windowId !== undefined && request.tabId !== undefined) {
const cookieDataDomain = {};
let contextName = 'default';
if (request.storeId !== undefined) {
contextName = request.storeId;
}
// console.log('cookieData:', cookieData)
if (cookieData[contextName] === undefined || cookieData[contextName][request.windowId] === undefined || cookieData[contextName][request.windowId][request.tabId] === undefined || cookieData[contextName][request.windowId][request.tabId].fgRoot === undefined) {
sendResponse({ sessionData: null, cookies: null, rootDomain: null, msg: getMsg('UnknownDomain'), logData: null });
return;
}
const rootDomain = cookieData[contextName][request.windowId][request.tabId].fgRoot;
if (request.targetDomain !== undefined && request.targetDomain !== null) {
if (cookieData[contextName][request.windowId][request.tabId][request.targetDomain] !== undefined) {
for (const cookie of cookieData[contextName][request.windowId][request.tabId][request.targetDomain]) {
if (cookieDataDomain[request.targetDomain] === undefined) cookieDataDomain[request.targetDomain] = [];
for (const key of Object.keys(cookie)) {
if (key.startsWith('fg')) {
continue;
}
switch (key) {
case 'name':
case 'value':
case 'domain':
case 'path':
case 'secure':
case 'expirationDate':
case 'firstPartyDomain':
case 'partitionKey':
// case 'storeId':
continue;
default:
delete cookie[key];
continue;
}
}
cookieDataDomain[request.targetDomain].push(cookie);
}
} else {
cookieDataDomain[request.targetDomain] = [];
}
} else {
for (const cookieDomainKey of Object.keys(cookieData[contextName][request.windowId][request.tabId])) {
if (cookieDomainKey === 'fgRoot') continue;
if (cookieDataDomain[cookieDomainKey] === undefined) cookieDataDomain[cookieDomainKey] = [];
for (const cookie of cookieData[contextName][request.windowId][request.tabId][cookieDomainKey]) {
for (const key of Object.keys(cookie)) {
if (key.startsWith('fg')) {
continue;
}
switch (key) {
case 'name':
case 'value':
case 'domain':
case 'path':
case 'secure':
case 'expirationDate':
case 'firstPartyDomain':
case 'partitionKey':
// case 'storeId':
continue;
default:
delete cookie[key];
continue;
}
}
cookieDataDomain[cookieDomainKey].push(cookie);
}
}
}
if (logData[contextName] !== undefined && logData[contextName][request.windowId] !== undefined && logData[contextName][request.windowId][request.tabId] !== undefined) {
sendResponse({ cookies: cookieDataDomain, rootDomain, msg: false, logData: logData[contextName][request.windowId][request.tabId] });
} else {
sendResponse({ cookies: cookieDataDomain, rootDomain, msg: false, logData: null });
}
return;
}
sendResponse({ sessionData: null, cookies: null, rootDomain: null, msg: getMsg('UnknownDomain'), logData: null });
}
function resetCookieInformation (tab) {
let contextName = 'default';
if (tab.cookieStoreId !== undefined) {
contextName = tab.cookieStoreId;
}
const tabWindowId = tab.windowId;
const tabTabId = tab.id;
removedByUser = 0;
if (removedData[contextName] === undefined) removedData[contextName] = {};
if (removedData[contextName][tabWindowId] === undefined) removedData[contextName][tabWindowId] = {};
removedData[contextName][tabWindowId][tabTabId] = { count: 0, domains: {} };
if (permittedData[contextName] === undefined) permittedData[contextName] = {};
if (permittedData[contextName][tabWindowId] === undefined) permittedData[contextName][tabWindowId] = {};
permittedData[contextName][tabWindowId][tabTabId] = { count: 0, domains: {} };
if (cookieCount[contextName] === undefined) cookieCount[contextName] = {};
if (cookieCount[contextName][tabWindowId] === undefined) cookieCount[contextName][tabWindowId] = {};
cookieCount[contextName][tabWindowId][tabTabId] = { count: 0, domains: {} };
if (removedByDomain[contextName] === undefined) removedByDomain[contextName] = {};
if (removedByDomain[contextName][tabWindowId] === undefined) removedByDomain[contextName][tabWindowId] = {};
removedByDomain[contextName][tabWindowId][tabTabId] = { count: 0, domains: {} };
if (cookieData[contextName] === undefined) cookieData[contextName] = {};
if (cookieData[contextName][tabWindowId] === undefined) cookieData[contextName][tabWindowId] = {};
cookieData[contextName][tabWindowId][tabTabId] = {};
}
function increaseCount (contextName, tabWindowId, tabTabId, cookieName, domain) {
const strippedDomainURL = domain.replace(/^(http:|https:)\/\//i, '');
if (cookieCount[contextName] === undefined) cookieCount[contextName] = {};
if (cookieCount[contextName][tabWindowId] === undefined) cookieCount[contextName][tabWindowId] = {};
if (cookieCount[contextName][tabWindowId][tabTabId] === undefined) cookieCount[contextName][tabWindowId][tabTabId] = { count: 0, domains: {} };
if (cookieCount[contextName][tabWindowId][tabTabId].domains[strippedDomainURL] === undefined) cookieCount[contextName][tabWindowId][tabTabId].domains[strippedDomainURL] = [];
if (cookieCount[contextName][tabWindowId][tabTabId].domains[strippedDomainURL].indexOf(cookieName) === -1) {
cookieCount[contextName][tabWindowId][tabTabId].domains[strippedDomainURL].push(cookieName);
++cookieCount[contextName][tabWindowId][tabTabId].count;
}
}
function isInRemoved (contextName, tabWindowId, tabTabId, cookieName, domain) {
const strippedDomainURL = domain.replace(/^(http:|https:)\/\//i, '');
if (removedData[contextName] !== undefined && removedData[contextName][tabWindowId] !== undefined && removedData[contextName][tabWindowId][tabTabId] !== undefined && removedData[contextName][tabWindowId][tabTabId].domains[strippedDomainURL] !== undefined) {
if (removedData[contextName][tabWindowId][tabTabId].domains[strippedDomainURL].indexOf(cookieName) !== -1) {
return true;
}
}
return false;
}
function addRemovedByDomain (contextName, tabWindowId, tabTabId, cookieName, domain) {
const strippedDomainURL = domain.replace(/^(http:|https:)\/\//i, '');
if (removedByDomain[contextName] === undefined) removedByDomain[contextName] = {};
if (removedByDomain[contextName][tabWindowId] === undefined) removedByDomain[contextName][tabWindowId] = {};
if (removedByDomain[contextName][tabWindowId][tabTabId] === undefined) removedByDomain[contextName][tabWindowId][tabTabId] = { count: 0, domains: {} };
if (removedByDomain[contextName][tabWindowId][tabTabId].domains[strippedDomainURL] === undefined) removedByDomain[contextName][tabWindowId][tabTabId].domains[strippedDomainURL] = [];
if (removedByDomain[contextName][tabWindowId][tabTabId].domains[strippedDomainURL].indexOf(cookieName) === -1) {
removedByDomain[contextName][tabWindowId][tabTabId].domains[strippedDomainURL].push(cookieName);
++removedByDomain[contextName][tabWindowId][tabTabId].count;
}
}
function increaseRemoved (contextName, tabWindowId, tabTabId, cookieName, domain) {
const strippedDomainURL = domain.replace(/^(http:|https:)\/\//i, '');
if (removedData[contextName] === undefined) removedData[contextName] = {};
if (removedData[contextName][tabWindowId] === undefined) removedData[contextName][tabWindowId] = {};
if (removedData[contextName][tabWindowId][tabTabId] === undefined) removedData[contextName][tabWindowId][tabTabId] = { count: 0, domains: {} };
if (removedData[contextName][tabWindowId][tabTabId].domains[strippedDomainURL] === undefined) removedData[contextName][tabWindowId][tabTabId].domains[strippedDomainURL] = [];
if (removedData[contextName][tabWindowId][tabTabId].domains[strippedDomainURL].indexOf(cookieName) === -1) {
removedData[contextName][tabWindowId][tabTabId].domains[strippedDomainURL].push(cookieName);
++removedData[contextName][tabWindowId][tabTabId].count;
}
}
function increasePermitted (contextName, tabWindowId, tabTabId, cookieName, domain) {
const strippedDomainURL = domain.replace(/^(http:|https:)\/\//i, '');
if (permittedData[contextName] === undefined) permittedData[contextName] = {};
if (permittedData[contextName][tabWindowId] === undefined) permittedData[contextName][tabWindowId] = {};
if (permittedData[contextName][tabWindowId][tabTabId] === undefined) permittedData[contextName][tabWindowId][tabTabId] = { count: 0, domains: {} };
if (permittedData[contextName][tabWindowId][tabTabId].domains[strippedDomainURL] === undefined) permittedData[contextName][tabWindowId][tabTabId].domains[strippedDomainURL] = [];
if (permittedData[contextName][tabWindowId][tabTabId].domains[strippedDomainURL].indexOf(cookieName) === -1) {
permittedData[contextName][tabWindowId][tabTabId].domains[strippedDomainURL].push(cookieName);
++permittedData[contextName][tabWindowId][tabTabId].count;
}
}
async function preSetMouseOverTitle (contextName, tabId) {
let tab = null;
if (useChrome) {
tab = await chrome.tabs.get(tabId);
} else {
tab = await browser.tabs.get(tabId);
}
if (tab === null || tab === undefined || tab.id === undefined) {
return;
}
setMouseOverTitle(contextName, tab.windowId, tab.id);
}
function setMouseOverTitle (contextName, tabWindowId, tabId) {
let titleString = '::::::::::::::::::: ' + getMsg('IconDisplayLog') + ' :::::::::::::::::::';
if (cookieCount[contextName] !== undefined && cookieCount[contextName][tabWindowId] !== undefined && cookieCount[contextName][tabWindowId][tabId] !== undefined) {
titleString += '\n' + getMsg('cookieCountDisplayIconHover', cookieCount[contextName][tabWindowId][tabId].count.toString());
}
if (localStorageData[tabWindowId] !== undefined && localStorageData[tabWindowId][tabId] !== undefined) {
titleString += '\n' + getMsg('localStorageKeyCountTitle', Object.keys(localStorageData[tabWindowId][tabId]).length.toString());
}
if (sessionStorageData[tabWindowId] !== undefined && sessionStorageData[tabWindowId][tabId] !== undefined) {
titleString += '\n' + getMsg('sessionStorageKeyCountTitle', Object.keys(sessionStorageData[tabWindowId][tabId]).length.toString());
}
let msgsAdded = false;
if (logData[contextName] !== undefined && logData[contextName][tabWindowId] !== undefined && logData[contextName][tabWindowId][tabId] !== undefined) {
const statuses = [getMsg('GlobalFlagState'), getMsg('AutoFlagState'), getMsg('PermittedState'), getMsg('AllowedState'), getMsg('DeletedStateMsg')];
let hasTitleChange = false;
const cookiesInMessages = [];
for (let status of statuses) {
const titleJoin = [];
let index = 0;
const statusLower = status.toLowerCase();
for (const msg of logData[contextName][tabWindowId][tabId]) {
if (msg.toLowerCase().indexOf(statusLower) !== -1) {
const cookieName = msg.match(/ '([^']*)' /)[1];
if (cookiesInMessages.indexOf(cookieName) === -1) {
titleJoin.push(cookieName);
cookiesInMessages.push(cookieName);
if (index !== 0 && index % 7 === 0) titleJoin.push('\n');
++index;
}
}
}
if (titleJoin.length !== 0) {
if (status === getMsg('DeletedStateMsg')) status = getMsg('DeletedState');
titleString += '\n' + status.replace(' ', '') + ': ' + titleJoin.join(', ');
hasTitleChange = true;
msgsAdded = true;
}
}
if (!hasTitleChange) {
titleString += '\n' + getMsg('NoActionOnPage');
msgsAdded = true;
}
}
let countStr = '0';
const hasRemove = removedData[contextName] !== undefined && removedData[contextName][tabWindowId] !== undefined && removedData[contextName][tabWindowId][tabId] !== undefined && removedData[contextName][tabWindowId][tabId].count !== undefined && removedData[contextName][tabWindowId][tabId].count !== 0;
const hasPermit = permittedData[contextName] !== undefined && permittedData[contextName][tabWindowId] !== undefined && permittedData[contextName][tabWindowId][tabId] !== undefined && permittedData[contextName][tabWindowId][tabId].count !== undefined && permittedData[contextName][tabWindowId][tabId].count !== 0;
const hasDomainRemoved = removedByDomain[contextName] !== undefined && removedByDomain[contextName][tabWindowId] !== undefined && removedByDomain[contextName][tabWindowId][tabId] !== undefined && removedByDomain[contextName][tabWindowId][tabId].count !== undefined && removedByDomain[contextName][tabWindowId][tabId].count !== 0;
if (hasRemove) {
countStr = removedData[contextName][tabWindowId][tabId].count.toString();
titleString += '\n' + getMsg('DeletedCookiesMsg', countStr);
}
if (removedByUser !== 0) {
titleString += '\n' + getMsg('DeletedByUserMsg', removedByUser.toString());
}
if (hasPermit) {
titleString += '\n' + getMsg('PermittedCookiesMsg', permittedData[contextName][tabWindowId][tabId].count.toString());
}
if (hasDomainRemoved) {
titleString += '\n' + getMsg('RemovedByDomainCookiesMsg', removedByDomain[contextName][tabWindowId][tabId].count.toString());
}
if (!hasRemove && !hasPermit && !msgsAdded) {
titleString += '\n' + getMsg('NoActionOnPage');
}
const totalRemoved = (parseInt(countStr) + removedByUser).toString();
if (totalRemoved !== '0') {
browserActionAPI.setBadgeText({ text: totalRemoved, tabId });
} else {
browserActionAPI.setBadgeText({ text: '', tabId });
}
browserActionAPI.setTitle({ title: titleString, tabId });
}
// Clear the cookies which are enabled for the domain in browser storage
async function clearCookiesAction (action, cookies, currentTab) {
const tabWindowId = currentTab.windowId;
const tabTabId = currentTab.id;
if (openTabData[tabWindowId] === undefined || openTabData[tabWindowId][tabTabId] === undefined || openTabData[tabWindowId][tabTabId][0] === undefined) return;
const rootDomain = openTabData[tabWindowId][tabTabId][0].u;
let contextName = 'default';
if (currentTab.cookieStoreId !== undefined) {
contextName = currentTab.cookieStoreId;
}
const strippedRootDomain = rootDomain.replace(/^(http:|https:)\/\//i, '');
// TODO: Start here
/*
// This will need a lot of refactoring, but should be able to improve the performance quite a bunch
// I am not sure when this will be implemented so as everything inside here needs to be refactored, trimmed down to 1/3..
// .. and also be reflected in the UI accordingly using the "cookie.fg" values
// If anynone is interested, feel free to make improvements :)
const flagCookiesAccountMode = await getStoreValue(contextName, 'flagCookies_accountMode', strippedRootDomain);
const flagCookiesLoggedDomain = await getStoreValue(contextName, 'flagCookies_logged', strippedRootDomain);
const flagCookiesLogSetting = await getStoreValue('flagCookies_logEnabled');
const flagCookiesAutoFlagEnabled = await getStoreValue(contextName, 'flagCookies_autoFlag', strippedRootDomain);
const flagCookiesGlobalFlagEnabled = await getStoreValue(contextName, 'flagCookies_flagGlobal', strippedRootDomain);
const isLogEnabled = !!flagCookiesLogSetting?.flagCookies_logEnabled;
const accountMode = !!flagCookiesAccountMode?.flagCookies_accountMode?.[contextName]?.[strippedRootDomain];
const autoFlagDomain = !!flagCookiesAutoFlagEnabled?.flagCookies_autoFlag?.[contextName]?.[strippedRootDomain];
const globalFlagDomain = flagCookiesGlobalFlagEnabled?.flagCookies_flagGlobal?.[contextName] === true;
const loggedDomainData = !!flagCookiesLoggedDomain?.flagCookies_logged?.[contextName]?.[strippedRootDomain];
*/
let data = {};
if (useChrome) {
data = await chrome.storage.local.get();
} else {
data = await browser.storage.local.get();
}
const hasDataContext = data[contextName] !== undefined && data[contextName][strippedRootDomain] !== undefined;
if (data[contextName] === undefined) data[contextName] = {};
if (data[contextName][strippedRootDomain] === undefined) data[contextName][strippedRootDomain] = {};
const hasAccountsInContext = data.flagCookies_accountMode !== undefined && data.flagCookies_accountMode[contextName] !== undefined;
let accountDomain = null;
let foundCookie = false;
if (cookies !== null) {
for (const cookie of cookies) {
foundCookie = false;
for (const key of Object.keys(cookie)) {
if (key.startsWith('fg')) {
delete cookie[key];
continue;
}
}
const domainKey = cookie.domain;
const domain = domainKey.replace(/^\./, '');
let hasHttpProfile = false;
let hasHttpsProfile = false;
if (hasAccountsInContext) {
hasHttpProfile = data.flagCookies_accountMode[contextName]['http://' + domain] !== undefined;
hasHttpsProfile = data.flagCookies_accountMode[contextName]['https://' + domain] !== undefined;
}
if (hasHttpProfile || hasHttpsProfile) {
for (const cookieDomainKey of Object.keys(cookieData[contextName][tabWindowId][tabTabId])) {
let index = 0;
for (const cookieEntry of cookieData[contextName][tabWindowId][tabTabId][cookieDomainKey]) {
if (cookieEntry.name === cookie.name && cookieEntry.domain === cookieDomainKey) {
foundCookie = true;
if (data.flagCookies_logged !== undefined && data.flagCookies_logged[contextName] !== undefined && data.flagCookies_logged[contextName][strippedRootDomain] !== undefined && data.flagCookies_logged[contextName][strippedRootDomain][cookieDomainKey] !== undefined && data.flagCookies_logged[contextName][strippedRootDomain][cookieDomainKey][cookie.name] !== undefined && data.flagCookies_logged[contextName][strippedRootDomain][cookieDomainKey][cookieEntry.name] === true) {
cookie.fgProfile = true;
cookie.fgAllowed = true;
cookie.fgProtected = true;
cookie.fgDomain = strippedRootDomain;
}
cookieData[contextName][tabWindowId][tabTabId][cookieDomainKey][index] = cookie;
break;
}
++index;
}
if (foundCookie) {
break;
}
}
} else {
for (const cookieDomainKey of Object.keys(cookieData[contextName][tabWindowId][tabTabId])) {
if (cookieDomainKey === 'fgRoot') continue;
let index = 0;
for (const cookieEntry of cookieData[contextName][tabWindowId][tabTabId][cookieDomainKey]) {
if (cookieEntry.name === cookie.name && cookieEntry.domain === cookieDomainKey) {
foundCookie = true;
for (const key of Object.keys(cookieEntry)) {
cookie[key] = cookieEntry[key];
}
cookieData[contextName][tabWindowId][tabTabId][cookie.domain][index] = cookie;
break;
}
++index;
}
if (foundCookie) {
break;
}
}
}
if (!foundCookie) {
if (cookieData[contextName][tabWindowId][tabTabId][domainKey] === undefined) {
cookieData[contextName][tabWindowId][tabTabId][domainKey] = [];
}
cookieData[contextName][tabWindowId][tabTabId][domainKey].push(cookie);
}
}
}
let protectDomainCookies = false;
let hasLogged = false;
let hasLocalProfile = false;
let hasGlobalProfile = false;
if (accountDomain === null && hasAccountsInContext) {
hasGlobalProfile = data.flagCookies_accountMode[contextName][strippedRootDomain] !== undefined && data.flagCookies_accountMode[contextName][strippedRootDomain] === true;
hasLocalProfile = data.flagCookies_accountMode[contextName][strippedRootDomain] !== undefined;
if (hasGlobalProfile) {
if (data.flagCookies_accountMode[contextName][strippedRootDomain] !== undefined) {
accountDomain = strippedRootDomain;
}
hasLogged = data.flagCookies_logged !== undefined && data.flagCookies_logged[contextName] !== undefined && data.flagCookies_logged[contextName][strippedRootDomain] !== undefined;
if (hasLogged && Object.keys(data.flagCookies_logged[contextName][strippedRootDomain]).length === 0) protectDomainCookies = true;
} else if (hasLocalProfile) {
hasLogged = data.flagCookies_logged !== undefined && data.flagCookies_logged[contextName] !== undefined && data.flagCookies_logged[contextName][strippedRootDomain] !== undefined;
if (hasLogged && Object.keys(data.flagCookies_logged[contextName][strippedRootDomain]).length === 0) protectDomainCookies = true;
accountDomain = strippedRootDomain;
} else if (strippedRootDomain !== undefined) {
const strippedDomainURL = strippedRootDomain.replace(/^(http:|https:)\/\//i, '').replace(/^www/i, '').replace(/^\./, '');
const targetTab = openTabData[tabWindowId][tabTabId];
for (const tab of Object.values(targetTab)) {
if (tab.d === strippedDomainURL) {
protectDomainCookies = true;
accountDomain = strippedDomainURL;
break;
}
}
} else {
return;
}
}
let timeString = '';
let timestamp = 0;
let isLogEnabled = true;
if (data.flagCookies_logEnabled === undefined || data.flagCookies_logEnabled !== true) {
isLogEnabled = false;
} else {
const dateObj = new Date();
timeString = (dateObj.getHours() < 10 ? '0' + dateObj.getHours() : dateObj.getHours()) + ':' + (dateObj.getMinutes() < 10 ? '0' + dateObj.getMinutes() : dateObj.getMinutes()) + ':' + (dateObj.getSeconds() < 10 ? '0' + dateObj.getSeconds() : dateObj.getSeconds());
timestamp = dateObj.getTime();
}
const urlInFlag = data.flagCookies_autoFlag !== undefined && data.flagCookies_autoFlag[contextName] !== undefined && data.flagCookies_autoFlag[contextName][strippedRootDomain] !== undefined && openTabData[tabWindowId] !== undefined && data.flagCookies_autoFlag[contextName][strippedRootDomain];
const globalFlagEnabled = data.flagCookies_flagGlobal !== undefined && data.flagCookies_flagGlobal[contextName] !== undefined && data.flagCookies_flagGlobal[contextName];
if (!globalFlagEnabled && urlInFlag) {
for (const cookieDomainKey of Object.keys(cookieData[contextName][tabWindowId][tabTabId])) {
if (cookieDomainKey === 'fgRoot') continue;
let cookieDomain = cookieDomainKey.replace(/^(http:|https:)\/\//i, '').replace(/^www/i, '').replace(/^\./, '');
let index = 0;
for (const cookie of cookieData[contextName][tabWindowId][tabTabId][cookieDomainKey]) {
increaseCount(contextName, tabWindowId, tabTabId, cookie.name, cookieDomainKey);
let firstPartyIsolate = null;
if (cookie.firstPartyDomain !== undefined) {
firstPartyIsolate = cookie.firstPartyDomain;
}
const isManagedCookieHttp = hasDataContext && data[contextName]['http://' + cookieDomain] !== undefined && data[contextName]['http://' + cookieDomain][cookieDomainKey] !== undefined && data[contextName]['http://' + cookieDomain][cookieDomainKey][cookie.name] !== undefined;
const isManagedCookieHttps = hasDataContext && data[contextName]['https://' + cookieDomain] !== undefined && data[contextName]['https://' + cookieDomain][cookieDomainKey] !== undefined && data[contextName]['https://' + cookieDomain][cookieDomainKey][cookie.name] !== undefined;
if (!isManagedCookieHttp && isManagedCookieHttps) cookieDomain = 'https://' + cookieDomain;
else if (isManagedCookieHttp) cookieDomain = 'http://' + cookieDomain;
if (cookieDomain === strippedRootDomain) cookie.fgRoot = true;
else if (cookie.fgRoot !== undefined) delete cookie.fgRoot;
const isManagedCookie = hasDataContext && data[contextName][strippedRootDomain] !== undefined && data[contextName][strippedRootDomain][cookieDomainKey] !== undefined && data[contextName][strippedRootDomain][cookieDomainKey][cookie.name] !== undefined;
const isLogged = data.flagCookies_logged !== undefined && data.flagCookies_logged[contextName] !== undefined && data.flagCookies_logged[contextName][strippedRootDomain] !== undefined && (data.flagCookies_logged[contextName][strippedRootDomain][cookieDomainKey] !== undefined && data.flagCookies_logged[contextName][strippedRootDomain][cookieDomainKey] !== undefined && data.flagCookies_logged[contextName][strippedRootDomain][cookieDomainKey][cookie.name] !== undefined) && data.flagCookies_logged[contextName][strippedRootDomain] !== undefined && data.flagCookies_logged[contextName][strippedRootDomain][cookieDomainKey] !== undefined && data.flagCookies_logged[contextName][strippedRootDomain][cookieDomainKey][cookie.name] !== undefined && data.flagCookies_logged[contextName][strippedRootDomain][cookieDomainKey][cookie.name] === true;
cookieData[contextName][tabWindowId][tabTabId][cookieDomainKey][index] = cookie;
if (!isManagedCookie && hasLocalProfile) {
if (isLogged) {
if (isLogEnabled) {
const msg = getMsg('AllowedProfileCookieMsg', [action, cookie.name, cookieDomainKey]);
addToLogData(contextName, tabWindowId, tabTabId, msg, timeString, timestamp);
}
increasePermitted(contextName, tabWindowId, tabTabId, cookie.name, cookieDomainKey);
cookie.fgPermitted = true;
cookie.fgDomain = strippedRootDomain;
cookie.fgProfile = true;
cookie.fgProtected = true;
cookie.fgAllowed = true;
if (cookie.fgLogged !== undefined) delete cookie.fgLogged;
cookieData[contextName][tabWindowId][tabTabId][cookieDomainKey][index] = cookie;
++index;
continue;
}
if ((hasLogged && isLogged) || !hasLogged) {
if (isLogEnabled) {
const msg = getMsg('AllowedGlobalProfileCookieMsg', [action, cookie.name, strippedRootDomain]);
addToLogData(contextName, tabWindowId, tabTabId, msg, timeString, timestamp);
}
increasePermitted(contextName, tabWindowId, tabTabId, cookie.name, cookieDomainKey);
cookie.fgPermitted = true;
cookie.fgDomain = strippedRootDomain;
cookie.fgAllowed = true;
cookie.fgProfile = true;
if (cookie.fgProtected !== undefined) {
delete cookie.fgProtected;
cookie.fgLogged = true;
}
cookieData[contextName][tabWindowId][tabTabId][cookieDomainKey][index] = cookie;
++index;
continue;
}
}
if (cookie.fgProfile !== undefined) delete cookie.fgProfile;
cookieData[contextName][tabWindowId][tabTabId][cookieDomainKey][index] = cookie;
if (hasDataContext && data[contextName][strippedRootDomain] !== undefined && data[contextName][strippedRootDomain][cookieDomainKey] !== undefined && data[contextName][strippedRootDomain][cookieDomainKey][cookie.name] === false) {
if (isLogEnabled) {
const msg = getMsg('PermittedCookieMsg', [action, cookie.name, strippedRootDomain]);
addToLogData(contextName, tabWindowId, tabTabId, msg, timeString, timestamp);
}
increasePermitted(contextName, tabWindowId, tabTabId, cookie.name, cookieDomainKey);
cookie.fgPermitted = true;
cookie.fgDomain = strippedRootDomain;
cookie.fgAllowed = true;