-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmutation.js
89 lines (82 loc) · 2.35 KB
/
mutation.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
const mob = (window) => {
let targetNodes = new Set();
const config = { attributes: true, childList: true, subtree: true };
const callback = (mutationsList, observer) => {
for(let mutation of mutationsList) {
if (mutation.type === 'childList') {
if (mutation.addedNodes.length) {
slice(mutation.addedNodes).forEach((node, idx) => {
let zIdx = hasZindex(node);
if (zIdx) {
if (DEBUG) {
console.log(zIdx);
}
if (naiveCookieTextCheck(node)) {
targetNodes.add(node);
if (DEBUG) {
console.log('added node to targetNodes', node);
}
}
}
});
}
}
else if (mutation.type === 'attributes') {
// console.log(`The ${mutation.attributeName} attribute was modified.`);
if (!mutation.target) {
return;
}
let zIdx = hasZindex(mutation.target);
if (zIdx) {
// console.log(mutation.target, zIdx);
if (naiveCookieTextCheck(mutation.target)) {
targetNodes.add(mutation.target);
// console.log('added node to targetNodes', mutation.target);
}
}
} else {
// console.log(mutation.type, mutation);
let zIdx = hasZindex(mutation.target);
if (zIdx) {
if (naiveCookieTextCheck(mutation.target)) {
targetNodes.add(mutation.target);
// console.log('added node to targetNodes', mutation.target, zIdx);
}
}
}
}
};
window.MutationObserver.prototype.getTargetNodes = () => {
return targetNodes;
};
const mObserver = new window.MutationObserver(callback);
mObserver.observe(window.document, config);
return mObserver;
};
const hasZindex = (node) => {
if (!node) {
return false;
}
if (!(node instanceof HTMLElement)) {
return false;
}
if (node.ownerDocument) {
let zIndex = parseInt(
node.ownerDocument.defaultView.getComputedStyle(
node
).zIndex
);
if (zIndex && zIndex > 0) {
return zIndex;
} else {
return false;
}
} else {
return false;
}
};
const naiveCookieTextCheck = (node) => {
return node.textContent.toLowerCase().includes('cookies');
};
// Stop observing:
// mObserver.disconnect();