1
1
const cachedNodes = new Set ( ) ;
2
2
const searchTimeout = 60000 ;
3
- const batchSize = 50 ;
4
- const batchTimeout = 50 ;
3
+ const batchSize = 50 ;
4
+ const batchTimeout = 50 ;
5
5
let distancePairs = [ ] ;
6
- let searchNodes = [ ] ;
7
- let cookieNodes = [ ] ;
8
- let clickQueue = [ ] ;
9
- let observers = [ ] ;
10
- let buttonSet = new Set ( ) ;
11
- let watchedNodes = new Set ( ) ;
12
- let halt = false ;
6
+ let searchNodes = [ ] ;
7
+ let cookieNodes = [ ] ;
8
+ let clickQueue = [ ] ;
9
+ let observers = [ ] ;
10
+ let buttonSet = new Set ( ) ;
11
+ let watchedNodes = new Set ( ) ;
12
+ let halt = false ;
13
13
14
- function yieldToMain ( ) {
15
- return new Promise ( resolve => {
14
+ function yieldToMain ( ) {
15
+ return new Promise ( ( resolve ) => {
16
16
setTimeout ( resolve , 0 ) ;
17
17
} ) ;
18
18
}
@@ -21,13 +21,13 @@ function nodeMentionsCookie(node) {
21
21
if ( halt ) {
22
22
return ;
23
23
}
24
- const allElements = node . querySelectorAll ( ' *:not(script)' ) ;
24
+ const allElements = node . querySelectorAll ( " *:not(script)" ) ;
25
25
for ( let e of allElements ) {
26
26
if ( halt ) {
27
27
break ;
28
28
}
29
29
if ( e . textContent . toLowerCase ( ) . includes ( "cookie" ) && ! cachedNodes . has ( e ) ) {
30
- cookieNodes . push ( { node :node , mentionsCookie :e } ) ;
30
+ cookieNodes . push ( { node : node , mentionsCookie : e } ) ;
31
31
cachedNodes . add ( e ) ;
32
32
}
33
33
if ( e . shadowRoot ) {
@@ -53,7 +53,8 @@ function findAcceptButtons(node) {
53
53
return buttons ;
54
54
}
55
55
56
- const acceptPattern = / ^ * ( ( a c c e p t | a l l o w ) ( + a l l ) ? ( + c o o k i e s ) ? | ( i + ) ? a g r e e | ( a c c e p t | a g r e e ) + ( & | a n d ) + c o n t i n u e ) * $ / im;
56
+ const acceptPattern =
57
+ / ^ * ( ( a c c e p t | a l l o w ) ( + a l l ) ? ( + c o o k i e s ) ? | ( i + ) ? a g r e e | ( a c c e p t | a g r e e ) + ( & | a n d ) + c o n t i n u e ) * $ / im;
57
58
58
59
function looksLikeAccept ( button ) {
59
60
const text = button . textContent ;
@@ -66,16 +67,18 @@ function commonAncestorDistances(root, e, es) {
66
67
}
67
68
const distances = [ ] ;
68
69
const leftAncestors = path ( root , e ) ;
69
- if ( leftAncestors . length == 0 ) { // not in DOM
70
+ if ( leftAncestors . length == 0 ) {
71
+ // not in DOM
70
72
return ;
71
73
}
72
74
for ( let e1 of es ) {
73
75
if ( halt ) {
74
76
break ;
75
77
}
76
78
let d = commonAncestorDistance ( leftAncestors , e , e1 ) ;
77
- if ( d > - 1 ) { // not in DOM
78
- distancePairs . push ( { element :e1 , distance :d } ) ;
79
+ if ( d > - 1 ) {
80
+ // not in DOM
81
+ distancePairs . push ( { element : e1 , distance : d } ) ;
79
82
}
80
83
}
81
84
}
@@ -85,7 +88,8 @@ function commonAncestorDistance(path, e, e1) {
85
88
let distance = 0 ;
86
89
while ( true ) {
87
90
let pe = currentNode . parentNode ;
88
- if ( pe == null ) { // not in DOM
91
+ if ( pe == null ) {
92
+ // not in DOM
89
93
return - 1 ;
90
94
}
91
95
const index = path . indexOf ( pe ) ;
@@ -106,7 +110,8 @@ function path(root, e) {
106
110
if ( pe == root ) {
107
111
path . push ( root ) ;
108
112
break ;
109
- } else if ( pe == null ) { // not in DOM
113
+ } else if ( pe == null ) {
114
+ // not in DOM
110
115
return [ ] ;
111
116
}
112
117
path . push ( currentNode ) ;
@@ -135,15 +140,15 @@ async function search() {
135
140
}
136
141
}
137
142
cookieNodes = [ ] ;
138
- distancePairs . sort ( ( a , b ) => a . distance - b . distance ) ;
143
+ distancePairs . sort ( ( a , b ) => a . distance - b . distance ) ;
139
144
for ( let p of distancePairs ) {
140
145
if ( ! buttonSet . has ( p . element ) ) {
141
146
buttonSet . add ( p . element ) ;
142
147
p . clicks = 0 ;
143
148
clickQueue . push ( p ) ;
144
149
}
145
150
}
146
- clickQueue . sort ( ( a , b ) => a . distance - b . distance ) ;
151
+ clickQueue . sort ( ( a , b ) => a . distance - b . distance ) ;
147
152
distancePairs = [ ] ;
148
153
}
149
154
@@ -158,7 +163,11 @@ async function click() {
158
163
return ;
159
164
}
160
165
if ( clickTarget . clicks < 3 ) {
161
- console . log ( "accept-cookies: clicking" , clickTarget . clicks , clickTarget . element ) ;
166
+ console . log (
167
+ "accept-cookies: clicking" ,
168
+ clickTarget . clicks ,
169
+ clickTarget . element ,
170
+ ) ;
162
171
clickTarget . clicks ++ ;
163
172
clickTarget . element . click ( ) ;
164
173
return ;
@@ -169,61 +178,61 @@ async function click() {
169
178
}
170
179
171
180
function checkMutation ( mutationsList , observer ) {
172
- if ( halt ) {
173
- observer . disconnect ( ) ;
174
- return ;
175
- }
176
- for ( let mutation of mutationsList ) {
177
- if ( mutation . type === 'childList' ) {
178
- mutation . addedNodes . forEach ( n => {
179
- if ( n instanceof Element && n . tagName !== 'SCRIPT' ) {
180
- searchNodes . push ( n ) ;
181
- if ( n . shadowRoot ) {
182
- observe ( n . shadowRoot ) ;
183
- searchNodes . push ( n . shadowRoot ) ;
184
- }
181
+ if ( halt ) {
182
+ observer . disconnect ( ) ;
183
+ return ;
184
+ }
185
+ for ( let mutation of mutationsList ) {
186
+ if ( mutation . type === "childList" ) {
187
+ mutation . addedNodes . forEach ( ( n ) => {
188
+ if ( n instanceof Element && n . tagName !== "SCRIPT" ) {
189
+ searchNodes . push ( n ) ;
190
+ if ( n . shadowRoot ) {
191
+ observe ( n . shadowRoot ) ;
192
+ searchNodes . push ( n . shadowRoot ) ;
185
193
}
186
- } ) ;
187
- }
194
+ }
195
+ } ) ;
188
196
}
189
197
}
198
+ }
190
199
191
200
function observe ( targetNode ) {
192
201
if ( watchedNodes . has ( targetNode ) ) {
193
202
return ;
194
203
}
195
204
const observer = new MutationObserver ( checkMutation ) ;
196
205
const observerOptions = {
197
- childList : true ,
198
- subtree : true
199
- } ;
206
+ childList : true ,
207
+ subtree : true ,
208
+ } ;
200
209
observer . observe ( targetNode , observerOptions ) ;
201
210
observers . push ( observer ) ;
202
211
watchedNodes . add ( targetNode ) ;
203
212
}
204
213
205
214
function main ( ) {
206
- console . log ( ' accept-cookies: starting' ) ;
215
+ console . log ( " accept-cookies: starting" ) ;
207
216
document . addEventListener ( "acceptCookiesAttachShadow" , ( e ) => {
208
217
try {
209
218
const nodes = document . body . querySelectorAll ( e . detail ) ;
210
- nodes . forEach ( element => {
219
+ nodes . forEach ( ( element ) => {
211
220
if ( element . shadowRoot ) {
212
221
observe ( element . shadowRoot ) ;
213
222
searchNodes . push ( element . shadowRoot ) ;
214
223
}
215
224
} ) ;
216
225
} catch ( e ) {
217
- console . log ( ' accept-cookies: caught error querying shadowDOM' , e ) ;
226
+ console . log ( " accept-cookies: caught error querying shadowDOM" , e ) ;
218
227
}
219
228
} ) ;
220
- window . dispatchEvent ( new CustomEvent ( ' acceptCookiesReady' ) ) ;
229
+ window . dispatchEvent ( new CustomEvent ( " acceptCookiesReady" ) ) ;
221
230
const targetNode = document . body ;
222
231
observe ( targetNode ) ;
223
232
const timeout = ( ) => {
224
- console . log ( ' accept-cookies: timeout' ) ;
233
+ console . log ( " accept-cookies: timeout" ) ;
225
234
halt = true ;
226
- observers . forEach ( o => o . disconnect ( ) ) ;
235
+ observers . forEach ( ( o ) => o . disconnect ( ) ) ;
227
236
} ;
228
237
setTimeout ( timeout , searchTimeout ) ;
229
238
searchNodes . push ( targetNode ) ;
@@ -233,7 +242,7 @@ function main() {
233
242
}
234
243
await search ( ) ;
235
244
click ( ) ;
236
- setTimeout ( keepSearching , batchTimeout ) ;
245
+ setTimeout ( keepSearching , batchTimeout ) ;
237
246
} ;
238
247
keepSearching ( ) ;
239
248
}
0 commit comments