-
Notifications
You must be signed in to change notification settings - Fork 0
/
content.js
249 lines (236 loc) · 5.51 KB
/
content.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
const cachedNodes = new Set();
const searchTimeout = 60000;
const batchSize = 50;
const batchTimeout = 50;
let distancePairs = [];
let searchNodes = [];
let cookieNodes = [];
let clickQueue = [];
let observers = [];
let buttonSet = new Set();
let watchedNodes = new Set();
let halt = false;
function yieldToMain() {
return new Promise((resolve) => {
setTimeout(resolve, 0);
});
}
function nodeMentionsCookie(node) {
if (halt) {
return;
}
const allElements = node.querySelectorAll("*:not(script)");
for (let e of allElements) {
if (halt) {
break;
}
if (e.textContent.toLowerCase().includes("cookie") && !cachedNodes.has(e)) {
cookieNodes.push({ node: node, mentionsCookie: e });
cachedNodes.add(e);
}
if (e.shadowRoot) {
searchNodes.push(e.shadowRoot);
}
}
}
function findAcceptButtons(node) {
if (halt) {
return;
}
const buttons = [];
const bs = node.querySelectorAll('button, [role="button"]');
for (let b of bs) {
if (halt) {
break;
}
if (looksLikeAccept(b)) {
buttons.push(b);
}
}
return buttons;
}
const acceptPattern =
/^ *((accept|allow)( +all)?( +cookies)?|(i +)?agree|(accept|agree) +(&|and) +continue) *$/im;
function looksLikeAccept(button) {
const text = button.textContent;
return acceptPattern.test(text);
}
function commonAncestorDistances(root, e, es) {
if (halt) {
return;
}
const distances = [];
const leftAncestors = path(root, e);
if (leftAncestors.length == 0) {
// not in DOM
return;
}
for (let e1 of es) {
if (halt) {
break;
}
let d = commonAncestorDistance(leftAncestors, e, e1);
if (d > -1) {
// not in DOM
distancePairs.push({ element: e1, distance: d });
}
}
}
function commonAncestorDistance(path, e, e1) {
let currentNode = e1;
let distance = 0;
while (true) {
let pe = currentNode.parentNode;
if (pe == null) {
// not in DOM
return -1;
}
const index = path.indexOf(pe);
if (index > -1) {
return distance + index;
}
distance++;
currentNode = pe;
}
return distance;
}
function path(root, e) {
const path = [e];
let currentNode = e;
while (true) {
let pe = currentNode.parentNode;
if (pe == root) {
path.push(root);
break;
} else if (pe == null) {
// not in DOM
return [];
}
path.push(currentNode);
currentNode = pe;
}
return path;
}
async function search() {
if (halt) {
return;
}
for (let i = 0; i < searchNodes.length; i++) {
nodeMentionsCookie(searchNodes[i]);
if (i % batchSize === 0) {
await yieldToMain();
}
}
searchNodes = [];
for (let i = 0; i < cookieNodes.length; i++) {
const m = cookieNodes[i];
buttons = findAcceptButtons(m.node);
commonAncestorDistances(m.node, m.mentionsCookie, buttons);
if (i % batchSize === 0) {
await yieldToMain();
}
}
cookieNodes = [];
distancePairs.sort((a, b) => a.distance - b.distance);
for (let p of distancePairs) {
if (!buttonSet.has(p.element)) {
buttonSet.add(p.element);
p.clicks = 0;
clickQueue.push(p);
}
}
clickQueue.sort((a, b) => a.distance - b.distance);
distancePairs = [];
}
async function click() {
if (clickQueue.length === 0) {
return;
}
const clickTarget = clickQueue[0];
if (!clickTarget.element.parentNode) {
console.log("accept-cookies: button has disappeared, halting");
halt = true;
return;
}
if (clickTarget.clicks < 3) {
console.log(
"accept-cookies: clicking",
clickTarget.clicks,
clickTarget.element,
);
clickTarget.clicks++;
clickTarget.element.click();
return;
} else {
clickQueue.shift();
return click();
}
}
function checkMutation(mutationsList, observer) {
if (halt) {
observer.disconnect();
return;
}
for (let mutation of mutationsList) {
if (mutation.type === "childList") {
mutation.addedNodes.forEach((n) => {
if (n instanceof Element && n.tagName !== "SCRIPT") {
searchNodes.push(n);
if (n.shadowRoot) {
observe(n.shadowRoot);
searchNodes.push(n.shadowRoot);
}
}
});
}
}
}
function observe(targetNode) {
if (watchedNodes.has(targetNode)) {
return;
}
const observer = new MutationObserver(checkMutation);
const observerOptions = {
childList: true,
subtree: true,
};
observer.observe(targetNode, observerOptions);
observers.push(observer);
watchedNodes.add(targetNode);
}
function main() {
console.log("accept-cookies: starting");
document.addEventListener("acceptCookiesAttachShadow", (e) => {
try {
const nodes = document.body.querySelectorAll(e.detail);
nodes.forEach((element) => {
if (element.shadowRoot) {
observe(element.shadowRoot);
searchNodes.push(element.shadowRoot);
}
});
} catch (e) {
console.log("accept-cookies: caught error querying shadowDOM", e);
}
});
window.dispatchEvent(new CustomEvent("acceptCookiesReady"));
const targetNode = document.body;
observe(targetNode);
const timeout = () => {
console.log("accept-cookies: timeout");
halt = true;
observers.forEach((o) => o.disconnect());
};
setTimeout(timeout, searchTimeout);
searchNodes.push(targetNode);
const keepSearching = async () => {
if (halt) {
return;
}
await search();
click();
setTimeout(keepSearching, batchTimeout);
};
keepSearching();
}
addEventListener("DOMContentLoaded", (e) => main());