-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
218 lines (186 loc) · 6.97 KB
/
utils.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
var autoScroll = function (page) {
return page.evaluate(() => {
return new Promise((resolve) => {
var totalHeight = 0;
var distance = 100;
const max_distance = 20000;
var timer = setInterval(() => {
var scrollHeight = document.body.scrollHeight;
window.scrollBy(0, distance);
totalHeight += distance;
if (totalHeight >= scrollHeight || totalHeight >= max_distance) {
clearInterval(timer);
window.scrollTo(0, 0);
requestAnimationFrame(() => {
resolve();
});
}
}, 100);
});
});
};
const checkBodyScroll = (page) => {
return page.evaluate(() => {
return new Promise((resolve) => {
const { position, overflowY } = getComputedStyle(document.body);
if ((position === "fixed" && overflowY === "hidden") || overflowY === "hidden") {
document.body.style.position = "unset";
document.body.style.overflow = "unset";
document.documentElement.style.position = "unset";
document.documentElement.style.overflowY = "scroll";
}
requestAnimationFrame(() => resolve());
});
});
};
const modalKiller = (page) => {
return page.evaluate(() => {
return new Promise((resolve) => {
window.addEventListener("resize", kill);
function kill() {
function notAllMineFullScreen(target) {
const STEP = 10;
const bodyRect = target.getBoundingClientRect();
if (!(bodyRect.width && bodyRect.x >= 0 && bodyRect.y >= 0)) return false;
const centerX = bodyRect.x + bodyRect.width / 2;
const centerY = bodyRect.y + bodyRect.height / 2;
const offsetY = +((centerY - bodyRect.y) / STEP).toFixed(2);
const offsetX = +((centerX - bodyRect.x) / STEP).toFixed(2);
let startY = 0;
const topEls = [];
while (startY <= centerY) {
startY += offsetY;
const pointEl = document.elementFromPoint(centerX, startY);
if (target.contains(pointEl)) {
topEls.push(pointEl);
}
}
let startX = 0;
const leftEls = [];
while (startX <= centerX) {
startX += offsetX;
const pointEl = document.elementFromPoint(startX, centerY);
if (target.contains(pointEl)) {
leftEls.push(pointEl);
}
}
return topEls?.length !== STEP || leftEls?.length !== STEP;
}
function getAllTargetList(positionType = "fixed") {
const all = document.querySelectorAll("*");
const targetList = Array.from(all).filter((i) => {
const { zIndex, position, bottom } = getComputedStyle(i);
return position === positionType && (+zIndex > 0 || bottom === "0px");
});
return targetList;
}
function getTopLevelChildren(positionType = "fixed") {
return Array.from(getAllTargetList(positionType).filter((i) => i.parentElement === document.body));
}
function onCenter(dom) {
const { right, x, width } = dom.getBoundingClientRect();
return right - width === x;
}
function killAbsoluteCenter() {
const absoluteChildren = getTopLevelChildren("absolute");
absoluteChildren.forEach((dom) => {
onCenter(dom) && dom.parentElement.removeChild(dom);
});
}
function killMaxZIndexTop(list) {
if (!list.length) return;
const maxDomRes = { dom: null, zIndex: 0 };
list.forEach((dom) => {
const { zIndex } = getComputedStyle(dom);
if (!maxDomRes.dom) {
Object.assign(maxDomRes, { dom, zIndex });
} else {
maxDomRes.zIndex < zIndex && Object.assign(maxDomRes, { dom, zIndex });
}
});
const { display } = getComputedStyle(maxDomRes.dom);
const { x, y, width, height } = maxDomRes.dom.getBoundingClientRect();
const point = { x: x + width / 2, y: y + height / 2 };
maxDomRes.dom.style.display = "none";
requestAnimationFrame(() => {
if (document.elementFromPoint(point.x, point.y) === document.body) {
maxDomRes.dom.style.display = display;
} else {
document.body.removeChild(maxDomRes.dom);
}
});
}
const OCCUPY_SPACE = 0.95;
const targetList = getAllTargetList();
const topDom = [];
const resDom = [];
targetList.forEach(async (d) => {
const { width, height, left, right, top, bottom, display } = getComputedStyle(d);
const clientWidth = document.documentElement.clientWidth;
const clientHeight = document.documentElement.clientHeight;
const domWidth = width.replace("px", "");
const domHeight = height.replace("px", "");
let side = 0;
if (right === "0px") side++;
if (bottom === "0px") side++;
if (left === "0px") side++;
if (top === "0px") side++;
// const rule0 = onCenter(d);
const rule1 = side >= 2;
const rule2 = Number((domWidth / clientWidth).toFixed(2)) > OCCUPY_SPACE;
const rule3 = top !== "0px";
const rule4 = d.parentElement === document.body;
const rule5 = d.innerText.toLowerCase().includes("cookies");
const rule6 = domWidth >= clientWidth;
const rule7 = domHeight >= clientHeight;
const rule8 = display === "none";
if (rule8) {
resDom.push(d);
} else if (rule4 && rule5) {
resDom.push(d);
} else if ((rule1 || rule2) && rule3) {
resDom.push(d);
} else if (rule1 && rule6 && rule7 && (notAllMineFullScreen(d) || rule4)) {
resDom.push(d);
} else if (rule3) {
const { y: y1 } = d.getBoundingClientRect();
window.scrollBy(0, 10);
requestAnimationFrame(() => {
let { y: y2 } = d.getBoundingClientRect();
y1 === y2 && resDom.push(d);
window.scrollBy(0, -10);
});
} else if (rule4 && !rule3 && !rule7) {
topDom.push(d);
} else {
console.log("unKill", d);
}
});
requestAnimationFrame(() => {
resDom.forEach((dom) => {
const bodyChildren = getTopLevelChildren();
let topChild = null;
bodyChildren.forEach((top) => {
top.contains(dom) && (topChild = top);
});
if (topChild) {
document.body.removeChild(topChild);
} else {
dom.innerHTML = null;
dom.parentElement?.removeChild(dom);
}
});
killAbsoluteCenter();
killMaxZIndexTop(topDom);
resolve();
});
}
kill();
});
});
};
module.exports = {
autoScroll,
modalKiller,
checkBodyScroll,
};