@duckduckgo/autoconsent 4.0.0 → 4.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (90) hide show
  1. package/.github/ISSUE_TEMPLATE/broken-site-issue.md +29 -0
  2. package/CHANGELOG.md +27 -0
  3. package/Jenkinsfile +2 -2
  4. package/api.md +10 -1
  5. package/dist/addon-firefox/background.bundle.js +312 -1
  6. package/dist/addon-firefox/content.bundle.js +1912 -1
  7. package/dist/addon-firefox/manifest.json +2 -2
  8. package/dist/addon-firefox/rules.json +134 -38
  9. package/dist/addon-mv3/background.bundle.js +312 -1
  10. package/dist/addon-mv3/content.bundle.js +1912 -1
  11. package/dist/addon-mv3/devtools/background.html +10 -0
  12. package/dist/addon-mv3/devtools/bulma.min.css +1 -0
  13. package/dist/addon-mv3/devtools/loader.js +2 -0
  14. package/dist/addon-mv3/devtools/panel.html +88 -0
  15. package/dist/addon-mv3/devtools/panel.js +148 -0
  16. package/dist/addon-mv3/devtools/panel.ts +145 -0
  17. package/dist/addon-mv3/manifest.json +5 -3
  18. package/dist/addon-mv3/popup.bundle.js +173 -1
  19. package/dist/addon-mv3/popup.html +14 -0
  20. package/dist/addon-mv3/rules.json +134 -38
  21. package/dist/autoconsent.cjs.js +1 -1
  22. package/dist/autoconsent.esm.js +1 -1
  23. package/dist/autoconsent.playwright.js +1 -1
  24. package/lib/cmps/airbnb.ts +4 -0
  25. package/lib/cmps/base.ts +8 -0
  26. package/lib/cmps/consentmanager.ts +4 -0
  27. package/lib/cmps/consentomatic.ts +1 -0
  28. package/lib/cmps/conversant.ts +4 -0
  29. package/lib/cmps/cookiebot.ts +4 -0
  30. package/lib/cmps/evidon.ts +4 -0
  31. package/lib/cmps/klaro.ts +4 -0
  32. package/lib/cmps/onetrust.ts +4 -0
  33. package/lib/cmps/sourcepoint-frame.ts +4 -0
  34. package/lib/cmps/tiktok.ts +4 -0
  35. package/lib/cmps/trustarc-frame.ts +4 -0
  36. package/lib/cmps/trustarc-top.ts +4 -0
  37. package/lib/cmps/uniconsent.ts +4 -0
  38. package/lib/eval-handler.ts +2 -6
  39. package/lib/messages.ts +44 -13
  40. package/lib/random.ts +6 -0
  41. package/lib/rules.ts +2 -1
  42. package/lib/types.ts +27 -0
  43. package/lib/web.ts +127 -82
  44. package/package.json +4 -3
  45. package/playwright/runner.ts +1 -0
  46. package/rollup.config.js +18 -5
  47. package/rules/autoconsent/ausopen.json +2 -2
  48. package/rules/autoconsent/baden-wuerttemberg-de.json +1 -0
  49. package/rules/autoconsent/cc-banner.json +1 -0
  50. package/rules/autoconsent/complianz-notice.json +1 -0
  51. package/rules/autoconsent/cookie-notice.json +2 -1
  52. package/rules/autoconsent/dsgvo.json +1 -0
  53. package/rules/autoconsent/eu-cookie-compliance.json +1 -1
  54. package/rules/autoconsent/eu-cookie-law.json +1 -0
  55. package/rules/autoconsent/ezoic.json +1 -0
  56. package/rules/autoconsent/generic-cosmetic.json +9 -0
  57. package/rules/autoconsent/indeed-com.json +9 -0
  58. package/rules/autoconsent/jquery-cookiebar.json +1 -0
  59. package/rules/autoconsent/marksandspencer.json +2 -2
  60. package/rules/autoconsent/notice-cookie.json +1 -0
  61. package/rules/autoconsent/osano.json +1 -0
  62. package/rules/autoconsent/pornhub.json +12 -0
  63. package/rules/autoconsent/uk-cookie-consent.json +1 -0
  64. package/rules/autoconsent/xnxx-com.json +9 -0
  65. package/rules/rules.json +134 -38
  66. package/tests/borlabs.spec.ts +1 -1
  67. package/tests/complianz-optin.spec.ts +4 -4
  68. package/tests/cookie-notice.spec.ts +3 -1
  69. package/tests/cookiebot.spec.ts +0 -4
  70. package/tests/cookieinformation.spec.ts +0 -1
  71. package/tests/dsgvo.spec.ts +4 -1
  72. package/tests/evidon.spec.ts +2 -4
  73. package/tests/ezoic.spec.ts +0 -1
  74. package/tests/fundingchoices.spec.ts +0 -1
  75. package/tests/generic-cosmetic.spec.ts +11 -0
  76. package/tests/indeed.spec.ts +7 -0
  77. package/tests/moove.spec.ts +0 -6
  78. package/tests/pornhub.spec.ts +7 -0
  79. package/tests/pubtech.spec.ts +0 -1
  80. package/tests/reddit.spec.ts +1 -1
  81. package/tests/sibbo.spec.ts +0 -1
  82. package/tests/sirdata.spec.ts +0 -1
  83. package/tests/trustarc.spec.ts +0 -2
  84. package/tests/uniconsent.spec.ts +1 -1
  85. package/tests/usercentrics-api.spec.ts +1 -0
  86. package/tests/usercentrics-button.spec.ts +0 -1
  87. package/tests/xnxx.spec.ts +8 -0
  88. package/tsconfig.json +2 -1
  89. package/rules/autoconsent/motor-talk-de.json +0 -24
  90. package/tests/motor-talk.spec.ts +0 -7
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "manifest_version": 2,
3
3
  "name": "Autoconsent",
4
- "version": "2022.11.21",
4
+ "version": "2022.12.14",
5
5
  "background": {
6
6
  "scripts": [
7
7
  "background.bundle.js"
@@ -29,4 +29,4 @@
29
29
  "browser_style": true,
30
30
  "default_title": "Autoconsent"
31
31
  }
32
- }
32
+ }
@@ -169,7 +169,7 @@
169
169
  },
170
170
  {
171
171
  "name": "ausopen.com",
172
- "isHidingRule": true,
172
+ "cosmetic": true,
173
173
  "detectCmp": [
174
174
  {
175
175
  "exists": ".gdpr-popup__message"
@@ -189,9 +189,7 @@
189
189
  ],
190
190
  "optIn": [
191
191
  {
192
- "click": [
193
- ".gdpr-popup__message button"
194
- ]
192
+ "click": ".gdpr-popup__message button"
195
193
  }
196
194
  ]
197
195
  },
@@ -277,6 +275,7 @@
277
275
  "prehideSelectors": [
278
276
  ".cookie-alert.t-dark"
279
277
  ],
278
+ "cosmetic": true,
280
279
  "detectCmp": [
281
280
  {
282
281
  "exists": ".cookie-alert.t-dark"
@@ -419,6 +418,7 @@
419
418
  },
420
419
  {
421
420
  "name": "cc_banner",
421
+ "cosmetic": true,
422
422
  "prehideSelectors": [
423
423
  ".cc_banner-wrapper"
424
424
  ],
@@ -570,6 +570,7 @@
570
570
  "prehideSelectors": [
571
571
  ".cc-type-info[aria-describedby=\"cookieconsent:desc\"]"
572
572
  ],
573
+ "cosmetic": true,
573
574
  "detectCmp": [
574
575
  {
575
576
  "exists": ".cc-type-info[aria-describedby=\"cookieconsent:desc\"]"
@@ -693,6 +694,7 @@
693
694
  "prehideSelectors": [
694
695
  "#cookie-notice"
695
696
  ],
697
+ "cosmetic": true,
696
698
  "detectCmp": [
697
699
  {
698
700
  "visible": "#cookie-notice .cookie-notice-container"
@@ -705,9 +707,7 @@
705
707
  ],
706
708
  "optIn": [
707
709
  {
708
- "click": [
709
- "#cn-accept-cookie"
710
- ]
710
+ "click": "#cn-accept-cookie"
711
711
  }
712
712
  ],
713
713
  "optOut": [
@@ -957,6 +957,7 @@
957
957
  "prehideSelectors": [
958
958
  ".sp-dsgvo"
959
959
  ],
960
+ "cosmetic": true,
960
961
  "detectCmp": [
961
962
  {
962
963
  "exists": ".sp-dsgvo.sp-dsgvo-popup-overlay"
@@ -1070,7 +1071,7 @@
1070
1071
  ],
1071
1072
  "detectPopup": [
1072
1073
  {
1073
- "visible": ".eu-cookie-compliance-banner-info"
1074
+ "exists": ".eu-cookie-compliance-popup-open"
1074
1075
  }
1075
1076
  ],
1076
1077
  "optIn": [
@@ -1101,6 +1102,7 @@
1101
1102
  "prehideSelectors": [
1102
1103
  ".pea_cook_wrapper,.pea_cook_more_info_popover"
1103
1104
  ],
1105
+ "cosmetic": true,
1104
1106
  "detectCmp": [
1105
1107
  {
1106
1108
  "exists": ".pea_cook_wrapper"
@@ -1158,6 +1160,9 @@
1158
1160
  }
1159
1161
  ],
1160
1162
  "optOut": [
1163
+ {
1164
+ "wait": 500
1165
+ },
1161
1166
  {
1162
1167
  "click": "#ez-manage-settings"
1163
1168
  },
@@ -1250,6 +1255,31 @@
1250
1255
  }
1251
1256
  ]
1252
1257
  },
1258
+ {
1259
+ "name": "generic-cosmetic",
1260
+ "cosmetic": true,
1261
+ "prehideSelectors": [
1262
+ "#js-cookie-banner,.js-cookie-banner,.cookie-banner,#cookie-banner"
1263
+ ],
1264
+ "detectCmp": [
1265
+ {
1266
+ "exists": "#js-cookie-banner,.js-cookie-banner,.cookie-banner,#cookie-banner"
1267
+ }
1268
+ ],
1269
+ "detectPopup": [
1270
+ {
1271
+ "visible": "#js-cookie-banner,.js-cookie-banner,.cookie-banner,#cookie-banner"
1272
+ }
1273
+ ],
1274
+ "optIn": [],
1275
+ "optOut": [
1276
+ {
1277
+ "hide": [
1278
+ "#js-cookie-banner,.js-cookie-banner,.cookie-banner,#cookie-banner"
1279
+ ]
1280
+ }
1281
+ ]
1282
+ },
1253
1283
  {
1254
1284
  "name": "google-consent-standalone",
1255
1285
  "prehideSelectors": [],
@@ -1409,6 +1439,35 @@
1409
1439
  }
1410
1440
  ]
1411
1441
  },
1442
+ {
1443
+ "name": "indeed.com",
1444
+ "cosmetic": true,
1445
+ "prehideSelectors": [
1446
+ "#CookiePrivacyNotice"
1447
+ ],
1448
+ "detectCmp": [
1449
+ {
1450
+ "exists": "#CookiePrivacyNotice"
1451
+ }
1452
+ ],
1453
+ "detectPopup": [
1454
+ {
1455
+ "visible": "#CookiePrivacyNotice"
1456
+ }
1457
+ ],
1458
+ "optIn": [
1459
+ {
1460
+ "click": "#CookiePrivacyNotice button[data-gnav-element-name=CookiePrivacyNoticeOk]"
1461
+ }
1462
+ ],
1463
+ "optOut": [
1464
+ {
1465
+ "hide": [
1466
+ "#CookiePrivacyNotice"
1467
+ ]
1468
+ }
1469
+ ]
1470
+ },
1412
1471
  {
1413
1472
  "name": "ionos.de",
1414
1473
  "prehideSelectors": [
@@ -1519,6 +1578,7 @@
1519
1578
  "prehideSelectors": [
1520
1579
  ".cookie-bar"
1521
1580
  ],
1581
+ "cosmetic": true,
1522
1582
  "detectCmp": [
1523
1583
  {
1524
1584
  "exists": ".cookie-bar .cookie-bar__message,.cookie-bar .cookie-bar__buttons"
@@ -1598,7 +1658,7 @@
1598
1658
  },
1599
1659
  {
1600
1660
  "name": "marksandspencer.com",
1601
- "isHidingRule": true,
1661
+ "cosmetic": true,
1602
1662
  "detectCmp": [
1603
1663
  {
1604
1664
  "exists": ".navigation-cookiebbanner"
@@ -1618,9 +1678,7 @@
1618
1678
  ],
1619
1679
  "optIn": [
1620
1680
  {
1621
- "click": [
1622
- ".navigation-cookiebbanner__submit"
1623
- ]
1681
+ "click": ".navigation-cookiebbanner__submit"
1624
1682
  }
1625
1683
  ]
1626
1684
  },
@@ -1857,32 +1915,6 @@
1857
1915
  }
1858
1916
  ]
1859
1917
  },
1860
- {
1861
- "name": "motor-talk.de",
1862
- "prehideSelectors": [
1863
- ".mt-cc-bnnr__wrapper"
1864
- ],
1865
- "detectCmp": [
1866
- {
1867
- "exists": ".mt-cc-bnnr"
1868
- }
1869
- ],
1870
- "detectPopup": [
1871
- {
1872
- "visible": ".mt-cc-bnnr__wrapper"
1873
- }
1874
- ],
1875
- "optIn": [
1876
- {
1877
- "click": ".mt-cc-bnnr__button-main"
1878
- }
1879
- ],
1880
- "optOut": [
1881
- {
1882
- "click": ".mt-cc-bnnr__decline-link"
1883
- }
1884
- ]
1885
- },
1886
1918
  {
1887
1919
  "name": "national-lottery.co.uk",
1888
1920
  "detectCmp": [
@@ -1973,6 +2005,7 @@
1973
2005
  "prehideSelectors": [
1974
2006
  ".button--notice"
1975
2007
  ],
2008
+ "cosmetic": true,
1976
2009
  "detectCmp": [
1977
2010
  {
1978
2011
  "exists": ".notice--cookie"
@@ -2027,6 +2060,7 @@
2027
2060
  "prehideSelectors": [
2028
2061
  ".osano-cm-window"
2029
2062
  ],
2063
+ "cosmetic": true,
2030
2064
  "detectCmp": [
2031
2065
  {
2032
2066
  "exists": ".osano-cm-window"
@@ -2114,6 +2148,38 @@
2114
2148
  }
2115
2149
  ]
2116
2150
  },
2151
+ {
2152
+ "name": "pornhub.com",
2153
+ "runContext": {
2154
+ "urlPattern": "^https://(www\\.)?pornhub\\.com/"
2155
+ },
2156
+ "cosmetic": true,
2157
+ "prehideSelectors": [
2158
+ ".cookiesBanner"
2159
+ ],
2160
+ "detectCmp": [
2161
+ {
2162
+ "exists": ".cookiesBanner"
2163
+ }
2164
+ ],
2165
+ "detectPopup": [
2166
+ {
2167
+ "visible": ".cookiesBanner"
2168
+ }
2169
+ ],
2170
+ "optIn": [
2171
+ {
2172
+ "click": ".cookiesBanner .okButton"
2173
+ }
2174
+ ],
2175
+ "optOut": [
2176
+ {
2177
+ "hide": [
2178
+ ".cookiesBanner"
2179
+ ]
2180
+ }
2181
+ ]
2182
+ },
2117
2183
  {
2118
2184
  "name": "PrimeBox CookieBar",
2119
2185
  "prehideSelectors": [
@@ -2749,6 +2815,7 @@
2749
2815
  "prehideSelectors": [
2750
2816
  "#catapult-cookie-bar"
2751
2817
  ],
2818
+ "cosmetic": true,
2752
2819
  "detectCmp": [
2753
2820
  {
2754
2821
  "exists": "#catapult-cookie-bar"
@@ -3025,6 +3092,35 @@
3025
3092
  }
3026
3093
  ]
3027
3094
  },
3095
+ {
3096
+ "name": "xnxx-com",
3097
+ "cosmetic": true,
3098
+ "prehideSelectors": [
3099
+ "#cookies-use-alert"
3100
+ ],
3101
+ "detectCmp": [
3102
+ {
3103
+ "exists": "#cookies-use-alert"
3104
+ }
3105
+ ],
3106
+ "detectPopup": [
3107
+ {
3108
+ "visible": "#cookies-use-alert"
3109
+ }
3110
+ ],
3111
+ "optIn": [
3112
+ {
3113
+ "click": "#cookies-use-alert .close"
3114
+ }
3115
+ ],
3116
+ "optOut": [
3117
+ {
3118
+ "hide": [
3119
+ "#cookies-use-alert"
3120
+ ]
3121
+ }
3122
+ ]
3123
+ },
3028
3124
  {
3029
3125
  "name": "youtube-desktop",
3030
3126
  "prehideSelectors": [
@@ -1 +1,312 @@
1
- !function(){"use strict";const e=chrome.runtime.getManifest().manifest_version,t={};async function o(o){if(2!==e)return chrome.storage.local.set(o);Object.assign(t,o)}async function a(o){return null===o?2===e?t:await chrome.storage.local.get(null):2===e?t[o]:(await chrome.storage.local.get(o))?.[o]}async function s(o){if(2!==e)return chrome.storage.local.remove(o);delete t[o]}async function n(e,t,o=""){let a="",s="icons/cookie-idle.png";"success"===t?(a=`Opt out successful! (${o})`,s="icons/party.png"):"complete"===t?(a=`Opt out complete! (${o})`,s="icons/tick.png"):"working"===t?(a=`Processing... (${o})`,s="icons/cog.png"):"verified"===t?(a=`Verified (${o})`,s="icons/verified.png"):"idle"===t?(a="Idle",s="icons/cookie-idle.png"):"available"===t&&(a=`Click to opt out (${o})`,s="icons/cookie.png");const n=chrome.action||chrome.pageAction;chrome.pageAction&&chrome.pageAction.show(e),await n.setTitle({tabId:e,title:a}),await n.setIcon({tabId:e,path:s})}async function c(){const e=await fetch("./rules.json");o({rules:await e.json()})}async function i(){console.log("init sw");const e=await a("config");if(console.log("storedConfig",e),!e){console.log("init config");const e={enabled:!0,autoAction:"optOut",disabledCmps:[],enablePrehide:!0,detectRetries:20};await o({config:e})}}chrome.runtime.onInstalled.addListener((()=>{c(),i()})),2===e&&(c(),i()),chrome.tabs.onRemoved.addListener((e=>{s(`detected${e}`)})),chrome.runtime.onMessage.addListener((async(t,c)=>{const i=c.tab.id,r=c.frameId,l=await a("rules"),d=await a("config");switch(t.type){case"init":0===r&&await n(i,"idle"),chrome.tabs.sendMessage(i,{type:"initResp",rules:l,config:d},{frameId:r});break;case"eval":(async function(t,o,a){return 2===e?new Promise((e=>{chrome.tabs.executeScript(t,{frameId:o,code:`!!window.eval(decodeURIComponent("${encodeURIComponent(a)}"))`},(t=>{e([{result:t[0],frameId:o}])}))})):chrome.scripting.executeScript({target:{tabId:t,frameIds:[o]},world:"MAIN",args:[a],func:e=>{try{return window.eval(e)}catch(t){return void console.warn("eval error",e,t)}}})})(i,r,t.code).then((([e])=>{chrome.tabs.sendMessage(i,{id:t.id,type:"evalResp",result:e.result},{frameId:r})}));break;case"popupFound":await n(i,"available",t.cmp),o({[`detected${i}`]:r});break;case"optOutResult":case"optInResult":t.result&&(await n(i,"working",t.cmp),t.scheduleSelfTest&&await o({[`selfTest${i}`]:r}));break;case"selfTestResult":t.result&&await n(i,"verified",t.cmp);break;case"autoconsentDone":{await n(i,"success",t.cmp);const e=`selfTest${i}`,o=(await chrome.storage.local.get(e))?.[e];"number"==typeof o&&(s(e),chrome.tabs.sendMessage(i,{type:"selfTest"},{frameId:o}));break}case"autoconsentError":console.error("Error:",t.details)}})),2===e&&chrome.pageAction.onClicked.addListener((async e=>{const t=e.id,o=`detected${t}`,c=await a(o);"number"==typeof c&&(s(o),await n(t,"working"),chrome.tabs.sendMessage(t,{type:"optOut"},{frameId:c}))}))}();
1
+ (function () {
2
+ 'use strict';
3
+
4
+ const manifestVersion = chrome.runtime.getManifest().manifest_version;
5
+ // Storage abstraction: MV2 keeps everything in memory, MV3 uses chrome.storage
6
+ const storage = {};
7
+ async function storageSet(obj) {
8
+ if (manifestVersion === 2) {
9
+ Object.assign(storage, obj);
10
+ return;
11
+ }
12
+ return chrome.storage.local.set(obj);
13
+ }
14
+ async function storageGet(key) {
15
+ if (key === null) {
16
+ if (manifestVersion === 2) {
17
+ return storage;
18
+ }
19
+ return await chrome.storage.local.get(null);
20
+ }
21
+ if (manifestVersion === 2) {
22
+ return storage[key];
23
+ }
24
+ return (await chrome.storage.local.get(key))?.[key];
25
+ }
26
+ async function storageRemove(key) {
27
+ if (manifestVersion === 2) {
28
+ delete storage[key];
29
+ return;
30
+ }
31
+ return chrome.storage.local.remove(key);
32
+ }
33
+
34
+ async function showOptOutStatus(tabId, status, cmp = '') {
35
+ let title = "";
36
+ let icon = "icons/cookie-idle.png";
37
+ if (status === "success") {
38
+ title = `Opt out successful! (${cmp})`;
39
+ icon = "icons/party.png";
40
+ }
41
+ else if (status === "complete") {
42
+ title = `Opt out complete! (${cmp})`;
43
+ icon = "icons/tick.png";
44
+ }
45
+ else if (status === "working") {
46
+ title = `Processing... (${cmp})`;
47
+ icon = "icons/cog.png";
48
+ }
49
+ else if (status === "verified") {
50
+ title = `Verified (${cmp})`;
51
+ icon = "icons/verified.png";
52
+ }
53
+ else if (status === "idle") {
54
+ title = "Idle";
55
+ icon = "icons/cookie-idle.png";
56
+ }
57
+ else if (status === "available") {
58
+ title = `Click to opt out (${cmp})`;
59
+ icon = "icons/cookie.png";
60
+ }
61
+ const action = chrome.action || chrome.pageAction;
62
+ if (chrome.pageAction) {
63
+ chrome.pageAction.show(tabId);
64
+ }
65
+ await action.setTitle({
66
+ tabId,
67
+ title,
68
+ });
69
+ await action.setIcon({
70
+ tabId,
71
+ path: icon,
72
+ });
73
+ }
74
+
75
+ /**
76
+ * Mapping of tabIds to Port connections to open devtools panels.
77
+ * This is kept in memory, as the values are only relevant while the background service worker is
78
+ * alive. Once the service worker stops, Ports to devtools will be severed, and the panel will
79
+ * reestablish the connection.
80
+ */
81
+ const openDevToolsPanels = new Map();
82
+ async function loadRules() {
83
+ const res = await fetch("./rules.json");
84
+ storageSet({
85
+ rules: await res.json(),
86
+ });
87
+ }
88
+ async function initConfig() {
89
+ console.log('init sw');
90
+ const storedConfig = await storageGet('config');
91
+ console.log('storedConfig', storedConfig);
92
+ if (!storedConfig) {
93
+ console.log('init config');
94
+ const defaultConfig = {
95
+ enabled: true,
96
+ autoAction: 'optOut',
97
+ disabledCmps: [],
98
+ enablePrehide: true,
99
+ enableCosmeticRules: true,
100
+ detectRetries: 20,
101
+ };
102
+ await storageSet({
103
+ config: defaultConfig,
104
+ });
105
+ }
106
+ else if (typeof storedConfig.enableCosmeticRules === 'undefined') { // upgrade from old versions
107
+ storedConfig.enableCosmeticRules = true;
108
+ await storageSet({
109
+ config: storedConfig,
110
+ });
111
+ }
112
+ }
113
+ async function evalInTab(tabId, frameId, code) {
114
+ if (manifestVersion === 2) {
115
+ return new Promise((resolve) => {
116
+ chrome.tabs.executeScript(tabId, {
117
+ frameId,
118
+ code: `!!window.eval(decodeURIComponent("${encodeURIComponent(code)}"))`
119
+ }, (resultArr) => {
120
+ resolve([{
121
+ result: resultArr[0],
122
+ frameId,
123
+ }]);
124
+ });
125
+ });
126
+ }
127
+ return chrome.scripting.executeScript({
128
+ target: {
129
+ tabId,
130
+ frameIds: [frameId],
131
+ },
132
+ world: "MAIN",
133
+ args: [code],
134
+ func: (code) => {
135
+ try {
136
+ return window.eval(code);
137
+ }
138
+ catch (e) {
139
+ // ignore CSP errors
140
+ console.warn('eval error', code, e);
141
+ return;
142
+ }
143
+ },
144
+ });
145
+ }
146
+ async function getTabReports(tabId) {
147
+ const storageKey = `reports-${tabId}`;
148
+ return (await chrome.storage.session.get(storageKey))[storageKey] || {};
149
+ }
150
+ async function updateTabReports(tabId, frameId, msg) {
151
+ const reportsForTab = await getTabReports(tabId);
152
+ reportsForTab[frameId] = msg;
153
+ await chrome.storage.session.set({ [`reports-${tabId}`]: reportsForTab });
154
+ }
155
+ chrome.runtime.onInstalled.addListener(() => {
156
+ loadRules();
157
+ initConfig();
158
+ });
159
+ if (manifestVersion === 2) {
160
+ // always load rules on startup in MV2
161
+ loadRules();
162
+ initConfig();
163
+ }
164
+ chrome.tabs.onRemoved.addListener((tabId) => {
165
+ storageRemove(`detected${tabId}`);
166
+ });
167
+ chrome.runtime.onMessage.addListener(async (msg, sender) => {
168
+ const tabId = sender.tab.id;
169
+ const frameId = sender.frameId;
170
+ const rules = await storageGet("rules");
171
+ const autoconsentConfig = await storageGet('config');
172
+ switch (msg.type) {
173
+ case "init":
174
+ if (frameId === 0) {
175
+ await showOptOutStatus(tabId, 'idle');
176
+ }
177
+ chrome.tabs.sendMessage(tabId, {
178
+ type: "initResp",
179
+ rules,
180
+ config: autoconsentConfig,
181
+ }, {
182
+ frameId,
183
+ });
184
+ break;
185
+ case "eval":
186
+ evalInTab(tabId, frameId, msg.code).then(([result]) => {
187
+ chrome.tabs.sendMessage(tabId, {
188
+ id: msg.id,
189
+ type: "evalResp",
190
+ result: result.result,
191
+ }, {
192
+ frameId,
193
+ });
194
+ });
195
+ break;
196
+ case "popupFound":
197
+ await showOptOutStatus(tabId, "available", msg.cmp);
198
+ storageSet({
199
+ [`detected${tabId}`]: frameId,
200
+ });
201
+ break;
202
+ case "optOutResult":
203
+ case "optInResult":
204
+ if (msg.result) {
205
+ await showOptOutStatus(tabId, "working", msg.cmp);
206
+ if (msg.scheduleSelfTest) {
207
+ await storageSet({
208
+ [`selfTest${tabId}`]: frameId,
209
+ });
210
+ }
211
+ }
212
+ break;
213
+ case "selfTestResult":
214
+ if (msg.result) {
215
+ await showOptOutStatus(tabId, "verified", msg.cmp);
216
+ }
217
+ break;
218
+ case "autoconsentDone": {
219
+ await showOptOutStatus(tabId, "success", msg.cmp);
220
+ // sometimes self-test needs to be done in another frame
221
+ const selfTestKey = `selfTest${tabId}`;
222
+ const selfTestFrameId = (await chrome.storage.local.get(selfTestKey))?.[selfTestKey];
223
+ if (typeof selfTestFrameId === 'number') {
224
+ storageRemove(selfTestKey);
225
+ chrome.tabs.sendMessage(tabId, {
226
+ type: "selfTest",
227
+ }, {
228
+ frameId: selfTestFrameId,
229
+ });
230
+ }
231
+ break;
232
+ }
233
+ case "autoconsentError":
234
+ console.error('Error:', msg.details);
235
+ break;
236
+ case "report":
237
+ if (sender.tab && openDevToolsPanels.has(sender.tab.id)) {
238
+ openDevToolsPanels.get(sender.tab.id).postMessage({
239
+ tabId: sender.tab.id,
240
+ frameId: sender.frameId,
241
+ ...msg,
242
+ });
243
+ }
244
+ updateTabReports(sender.tab.id, sender.frameId, msg);
245
+ break;
246
+ }
247
+ });
248
+ if (manifestVersion === 2) { // MV3 handles this inside the popup
249
+ chrome.pageAction.onClicked.addListener(async (tab) => {
250
+ const tabId = tab.id;
251
+ const detectedKey = `detected${tabId}`;
252
+ const frameId = await storageGet(detectedKey);
253
+ if (typeof frameId === 'number') {
254
+ storageRemove(detectedKey);
255
+ await showOptOutStatus(tabId, "working");
256
+ chrome.tabs.sendMessage(tabId, {
257
+ type: "optOut",
258
+ }, {
259
+ frameId,
260
+ });
261
+ }
262
+ });
263
+ }
264
+ // Communicate with devtools panels
265
+ chrome.runtime.onConnect.addListener(function (devToolsConnection) {
266
+ if (devToolsConnection.name.startsWith('instance-')) {
267
+ // connection from an autoconsent instance - used to detect frame destruction
268
+ const tabId = devToolsConnection.sender?.tab?.id;
269
+ const instanceId = devToolsConnection.name.slice('instance-'.length);
270
+ if (tabId && instanceId) {
271
+ devToolsConnection.onDisconnect.addListener(() => {
272
+ if (openDevToolsPanels.has(tabId)) {
273
+ openDevToolsPanels.get(tabId).postMessage({
274
+ type: 'instanceTerminated',
275
+ tabId,
276
+ instanceId,
277
+ });
278
+ }
279
+ // remove stored frame data
280
+ updateTabReports(tabId, devToolsConnection.sender.frameId, undefined);
281
+ });
282
+ }
283
+ }
284
+ else if (devToolsConnection.name === 'devtools-panel') {
285
+ let tabId = -1;
286
+ // add the listener
287
+ devToolsConnection.onMessage.addListener(async (message) => {
288
+ tabId = message.tabId;
289
+ if (message.type === 'init') {
290
+ // save the message channel for this tab
291
+ openDevToolsPanels.set(tabId, devToolsConnection);
292
+ // dump data cached in bg to the panel
293
+ const reportsForTab = await getTabReports(tabId);
294
+ Object.keys(reportsForTab || {}).forEach((frameId) => {
295
+ devToolsConnection.postMessage({
296
+ tabId,
297
+ frameId,
298
+ ...reportsForTab[parseInt(frameId, 10)]
299
+ });
300
+ });
301
+ }
302
+ });
303
+ devToolsConnection.onDisconnect.addListener(function () {
304
+ openDevToolsPanels.delete(tabId);
305
+ });
306
+ }
307
+ });
308
+ chrome.tabs.onRemoved.addListener((tabId) => {
309
+ chrome.storage.session.remove(`reports-${tabId}`);
310
+ });
311
+
312
+ })();