Skip to content

Commit 1f07eb1

Browse files
committed
tidy
1 parent 25d0e75 commit 1f07eb1

File tree

3 files changed

+76
-64
lines changed

3 files changed

+76
-64
lines changed

content.js

+56-47
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
const cachedNodes = new Set();
22
const searchTimeout = 60000;
3-
const batchSize = 50;
4-
const batchTimeout = 50;
3+
const batchSize = 50;
4+
const batchTimeout = 50;
55
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;
1313

14-
function yieldToMain () {
15-
return new Promise(resolve => {
14+
function yieldToMain() {
15+
return new Promise((resolve) => {
1616
setTimeout(resolve, 0);
1717
});
1818
}
@@ -21,13 +21,13 @@ function nodeMentionsCookie(node) {
2121
if (halt) {
2222
return;
2323
}
24-
const allElements = node.querySelectorAll('*:not(script)');
24+
const allElements = node.querySelectorAll("*:not(script)");
2525
for (let e of allElements) {
2626
if (halt) {
2727
break;
2828
}
2929
if (e.textContent.toLowerCase().includes("cookie") && !cachedNodes.has(e)) {
30-
cookieNodes.push({ node:node, mentionsCookie:e });
30+
cookieNodes.push({ node: node, mentionsCookie: e });
3131
cachedNodes.add(e);
3232
}
3333
if (e.shadowRoot) {
@@ -53,7 +53,8 @@ function findAcceptButtons(node) {
5353
return buttons;
5454
}
5555

56-
const acceptPattern = /^ *((accept|allow)( +all)?( +cookies)?|(i +)?agree|(accept|agree) +(&|and) +continue) *$/im;
56+
const acceptPattern =
57+
/^ *((accept|allow)( +all)?( +cookies)?|(i +)?agree|(accept|agree) +(&|and) +continue) *$/im;
5758

5859
function looksLikeAccept(button) {
5960
const text = button.textContent;
@@ -66,16 +67,18 @@ function commonAncestorDistances(root, e, es) {
6667
}
6768
const distances = [];
6869
const leftAncestors = path(root, e);
69-
if (leftAncestors.length == 0) { // not in DOM
70+
if (leftAncestors.length == 0) {
71+
// not in DOM
7072
return;
7173
}
7274
for (let e1 of es) {
7375
if (halt) {
7476
break;
7577
}
7678
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 });
7982
}
8083
}
8184
}
@@ -85,7 +88,8 @@ function commonAncestorDistance(path, e, e1) {
8588
let distance = 0;
8689
while (true) {
8790
let pe = currentNode.parentNode;
88-
if (pe == null) { // not in DOM
91+
if (pe == null) {
92+
// not in DOM
8993
return -1;
9094
}
9195
const index = path.indexOf(pe);
@@ -106,7 +110,8 @@ function path(root, e) {
106110
if (pe == root) {
107111
path.push(root);
108112
break;
109-
} else if (pe == null) { // not in DOM
113+
} else if (pe == null) {
114+
// not in DOM
110115
return [];
111116
}
112117
path.push(currentNode);
@@ -135,15 +140,15 @@ async function search() {
135140
}
136141
}
137142
cookieNodes = [];
138-
distancePairs.sort((a,b) => a.distance - b.distance);
143+
distancePairs.sort((a, b) => a.distance - b.distance);
139144
for (let p of distancePairs) {
140145
if (!buttonSet.has(p.element)) {
141146
buttonSet.add(p.element);
142147
p.clicks = 0;
143148
clickQueue.push(p);
144149
}
145150
}
146-
clickQueue.sort((a,b) => a.distance - b.distance);
151+
clickQueue.sort((a, b) => a.distance - b.distance);
147152
distancePairs = [];
148153
}
149154

@@ -158,7 +163,11 @@ async function click() {
158163
return;
159164
}
160165
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+
);
162171
clickTarget.clicks++;
163172
clickTarget.element.click();
164173
return;
@@ -169,61 +178,61 @@ async function click() {
169178
}
170179

171180
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);
185193
}
186-
});
187-
}
194+
}
195+
});
188196
}
189197
}
198+
}
190199

191200
function observe(targetNode) {
192201
if (watchedNodes.has(targetNode)) {
193202
return;
194203
}
195204
const observer = new MutationObserver(checkMutation);
196205
const observerOptions = {
197-
childList: true,
198-
subtree: true
199-
};
206+
childList: true,
207+
subtree: true,
208+
};
200209
observer.observe(targetNode, observerOptions);
201210
observers.push(observer);
202211
watchedNodes.add(targetNode);
203212
}
204213

205214
function main() {
206-
console.log('accept-cookies: starting');
215+
console.log("accept-cookies: starting");
207216
document.addEventListener("acceptCookiesAttachShadow", (e) => {
208217
try {
209218
const nodes = document.body.querySelectorAll(e.detail);
210-
nodes.forEach(element => {
219+
nodes.forEach((element) => {
211220
if (element.shadowRoot) {
212221
observe(element.shadowRoot);
213222
searchNodes.push(element.shadowRoot);
214223
}
215224
});
216225
} catch (e) {
217-
console.log('accept-cookies: caught error querying shadowDOM', e);
226+
console.log("accept-cookies: caught error querying shadowDOM", e);
218227
}
219228
});
220-
window.dispatchEvent(new CustomEvent('acceptCookiesReady'));
229+
window.dispatchEvent(new CustomEvent("acceptCookiesReady"));
221230
const targetNode = document.body;
222231
observe(targetNode);
223232
const timeout = () => {
224-
console.log('accept-cookies: timeout');
233+
console.log("accept-cookies: timeout");
225234
halt = true;
226-
observers.forEach(o => o.disconnect());
235+
observers.forEach((o) => o.disconnect());
227236
};
228237
setTimeout(timeout, searchTimeout);
229238
searchNodes.push(targetNode);
@@ -233,7 +242,7 @@ function main() {
233242
}
234243
await search();
235244
click();
236-
setTimeout(keepSearching,batchTimeout);
245+
setTimeout(keepSearching, batchTimeout);
237246
};
238247
keepSearching();
239248
}

inject.js

+10-8
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,26 @@
1-
(function() {
2-
console.log('accept-cookies: overriding attachShadow');
1+
(function () {
2+
console.log("accept-cookies: overriding attachShadow");
33
const acceptCookiesOrigAttachShadow = Element.prototype.attachShadow;
44
let shadowEvents = [];
55
let seenContentScript = false;
6-
window.addEventListener('acceptCookiesReady', () => {
7-
console.log('accept-cookies: saw ready event');
6+
window.addEventListener("acceptCookiesReady", () => {
7+
console.log("accept-cookies: saw ready event");
88
seenContentScript = true;
9-
shadowEvents.forEach(e => document.dispatchEvent(e));
9+
shadowEvents.forEach((e) => document.dispatchEvent(e));
1010
shadowEvents = [];
1111
});
1212
Element.prototype.attachShadow = function (options = {}) {
1313
options.mode = "open";
1414
const shadowDom = acceptCookiesOrigAttachShadow.call(this, options);
1515
let selector = this.nodeName.toLowerCase();
1616
if (this.id) {
17-
selector += '#' + this.id;
17+
selector += "#" + this.id;
1818
} else if (this.className) {
19-
selector += '.' + this.className.trim().replace(/\s+/g, '.');
19+
selector += "." + this.className.trim().replace(/\s+/g, ".");
2020
}
21-
const attachShadow = new CustomEvent('acceptCookiesAttachShadow', { detail: selector });
21+
const attachShadow = new CustomEvent("acceptCookiesAttachShadow", {
22+
detail: selector,
23+
});
2224
if (seenContentScript) {
2325
document.dispatchEvent(attachShadow);
2426
} else {

service.js

+10-9
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1-
chrome.webNavigation.onCommitted.addListener(details => {
2-
if (details.url.startsWith("http")
3-
&& !details.url.includes("chrome.google.com")
4-
&& !details.url.includes("chromewebstore.google.com")
1+
chrome.webNavigation.onCommitted.addListener((details) => {
2+
if (
3+
details.url.startsWith("http") &&
4+
!details.url.includes("chrome.google.com") &&
5+
!details.url.includes("chromewebstore.google.com")
56
) {
67
chrome.scripting.executeScript({
7-
target:{
8+
target: {
89
tabId: details.tabId,
9-
allFrames : true
10+
allFrames: true,
1011
},
11-
world: 'MAIN',
12-
files: ['inject.js'],
13-
injectImmediately: true
12+
world: "MAIN",
13+
files: ["inject.js"],
14+
injectImmediately: true,
1415
});
1516
}
1617
});

0 commit comments

Comments
 (0)