Skip to content

Commit

Permalink
fix: resize
Browse files Browse the repository at this point in the history
  • Loading branch information
JunyWuuuu91 committed Jun 30, 2022
1 parent a5c05e4 commit 86acb52
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 147 deletions.
2 changes: 1 addition & 1 deletion src/screenshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ const run = async (urls = [], type = "png") => {
headless: false,
ignoreHTTPSErrors: true,
defaultViewport: null,
dumpio: true,
// dumpio: true,
executablePath: "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome",
});

Expand Down
16 changes: 8 additions & 8 deletions src/test.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
const { run } = require("./screenshot");

const tests = [
"https://www.merckgroup.com.cn/cn-zh/research/science-space/envisioning-tomorrow/scarcity-of-resources/mof.html",
"https://www.globenewswire.com/news-release/2022/04/13/2422077/0/en/Neoleukin-Therapeutics-Announces-Preclinical-Data-for-NL-201-at-American-Association-for-Cancer-Research-AACR-Annual-Meeting.html",
"https://www.biospace.com/article/releases/starton-therapeutics-doses-first-subjects-in-phase-1-clinical-trial-of-star-lld-continuous-delivery-lenalidomide/?s=67",
// "https://www.merckgroup.com.cn/cn-zh/research/science-space/envisioning-tomorrow/scarcity-of-resources/mof.html",
// "https://www.globenewswire.com/news-release/2022/04/13/2422077/0/en/Neoleukin-Therapeutics-Announces-Preclinical-Data-for-NL-201-at-American-Association-for-Cancer-Research-AACR-Annual-Meeting.html",
// "https://www.biospace.com/article/releases/starton-therapeutics-doses-first-subjects-in-phase-1-clinical-trial-of-star-lld-continuous-delivery-lenalidomide/?s=67",
"https://www.dompe.com/en/media/press-releases/domp%C3%A9-investimenti-strategici-e-collaborazione-con-engitix-per-luso-di-exscalate-la-piattaforma-di-intelligenza-artificiale-per-la-drug-discovery",
"https://www.telethon.it/en/stories-and-news/news/from-telethon-foundation/orchard-therapeutics-receives-ec-approval-for-libmeldy/",
"https://eanm22.eanm.org/",
"https://allied.health/pharmaceutical/#pharma-development",
"https://www.psychiatry.org/",
"https://congress.eular.org/",
"https://ehaweb.org/",
// "https://eanm22.eanm.org/",
// "https://allied.health/pharmaceutical/#pharma-development",
// "https://www.psychiatry.org/",
// "https://congress.eular.org/",
// "https://ehaweb.org/",
// "https://www.aan.com/conferences-community/annual-meeting/",
// "https://www.escardio.org/Congresses-&-Events/ESC-Congress",
// "https://www.aasld.org/",
Expand Down
281 changes: 143 additions & 138 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,167 +41,172 @@ const checkBodyScroll = (page) => {
const modalKiller = (page) => {
return page.evaluate(() => {
return new Promise((resolve) => {
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);
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);
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;
}
return topEls?.length !== STEP || leftEls?.length !== STEP;
}

function getTopLevelChildren(positionType = "fixed") {
return Array.from(getAllTargetList(positionType).filter((i) => i.parentElement === document.body));
}
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 onCenter(dom) {
const { right, x, width } = dom.getBoundingClientRect();
return right - width === x;
}
function getTopLevelChildren(positionType = "fixed") {
return Array.from(getAllTargetList(positionType).filter((i) => i.parentElement === document.body));
}

function killAbsoluteCenter() {
const absoluteChildren = getTopLevelChildren("absolute");
absoluteChildren.forEach((dom) => {
onCenter(dom) && dom.parentElement.removeChild(dom);
});
}
function onCenter(dom) {
const { right, x, width } = dom.getBoundingClientRect();
return right - width === x;
}

function killMaxZIndexTop(list) {
if (!list.length) return;
function killAbsoluteCenter() {
const absoluteChildren = getTopLevelChildren("absolute");
absoluteChildren.forEach((dom) => {
onCenter(dom) && dom.parentElement.removeChild(dom);
});
}

const maxDomRes = { dom: null, zIndex: 0 };
list.forEach((dom) => {
const { zIndex } = getComputedStyle(dom);
function killMaxZIndexTop(list) {
if (!list.length) return;

if (!maxDomRes.dom) {
Object.assign(maxDomRes, { dom, zIndex });
} else {
maxDomRes.zIndex < zIndex && Object.assign(maxDomRes, { dom, zIndex });
}
});
const maxDomRes = { dom: null, zIndex: 0 };
list.forEach((dom) => {
const { zIndex } = getComputedStyle(dom);

const { display } = getComputedStyle(maxDomRes.dom);
const { x, y, width, height } = maxDomRes.dom.getBoundingClientRect();
const point = { x: x + width / 2, y: y + height / 2 };
if (!maxDomRes.dom) {
Object.assign(maxDomRes, { dom, zIndex });
} else {
maxDomRes.zIndex < zIndex && Object.assign(maxDomRes, { dom, zIndex });
}
});

maxDomRes.dom.style.display = "none";
const { display } = getComputedStyle(maxDomRes.dom);
const { x, y, width, height } = maxDomRes.dom.getBoundingClientRect();
const point = { x: x + width / 2, y: y + height / 2 };

requestAnimationFrame(() => {
if (document.elementFromPoint(point.x, point.y) === document.body) {
maxDomRes.dom.style.display = display;
} else {
document.body.removeChild(maxDomRes.dom);
}
});
}
maxDomRes.dom.style.display = "none";

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);
if (document.elementFromPoint(point.x, point.y) === document.body) {
maxDomRes.dom.style.display = display;
} else {
document.body.removeChild(maxDomRes.dom);
}
});
} 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);
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 {
dom.innerHTML = null;
dom.parentElement?.removeChild(dom);
console.log("unKill", d);
}
});

killAbsoluteCenter();
killMaxZIndexTop(topDom);
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();
});
resolve();
});
}
kill();
});
});
};
Expand Down

0 comments on commit 86acb52

Please sign in to comment.