-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
removeChild.js
38 lines (34 loc) · 1.14 KB
/
removeChild.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
module.exports = async (page, selectorToRemove) => {
try {
let elements = await page.$$(selectorToRemove);
const lengthBefore = elements.length;
if (lengthBefore === 0) {
console.warn(`Found no element with selector: ${selectorToRemove}`);
return [null, { [selectorToRemove]: null }];
}
console.log(
`${elements.length} element(s) with selector: ${selectorToRemove} will be removed.`
);
if (lengthBefore > 0) {
await page.evaluate(sel => {
const elements = document.querySelectorAll(sel);
for (let el of elements) {
el.parentNode.removeChild(el);
}
}, selectorToRemove);
}
elements = await page.$$(selectorToRemove);
const lengthAfter = elements.length;
const diff = lengthBefore - lengthAfter;
diff !== lengthBefore &&
console.warn(
`Some elements with selector: ${selectorToRemove} has not been removed!`
);
console.log(
`${lengthAfter} element(s) with selector: ${selectorToRemove} on page.`
);
return [null, { [selectorToRemove]: { lengthBefore, lengthAfter } }];
} catch (error) {
return error;
}
};