-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(puppeteer driver): clean the view-hierarchy from unused data.
- Loading branch information
Showing
4 changed files
with
76 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import * as puppeteer from "puppeteer"; | ||
|
||
/** | ||
* Get clean DOM from the page content | ||
* - Removes hidden elements | ||
* - Removes ads, analytics, tracking elements | ||
* - Removes unnecessary attributes | ||
* - Removes empty elements | ||
* @param page | ||
*/ | ||
export default async function getCleanDOM(page: puppeteer.Page) { | ||
return await page.evaluate(() => { | ||
const copiedDocument = document.cloneNode(true) as Document; | ||
|
||
copiedDocument | ||
.querySelectorAll('[hidden], [aria-hidden="true"]') | ||
.forEach((el) => el.remove()); | ||
|
||
const removeSelectors = [ | ||
"script", | ||
"style", | ||
"link", | ||
"meta", | ||
"noscript", | ||
"iframe", | ||
'[class*="ads"]', | ||
'[id*="ads"]', | ||
'[class*="analytics"]', | ||
'[class*="tracking"]', | ||
"footer", | ||
"header", | ||
"nav", | ||
"path", | ||
"aside", | ||
]; | ||
|
||
const allowedAttributes = [ | ||
"src", | ||
"href", | ||
"alt", | ||
"title", | ||
"aria-label", | ||
"aria-labelledby", | ||
"aria-describedby", | ||
"aria-hidden", | ||
"role", | ||
"class", | ||
"id", | ||
"data-*", | ||
]; | ||
|
||
copiedDocument.querySelectorAll("*").forEach((el) => { | ||
Array.from(el.attributes).forEach((attr) => { | ||
if (!allowedAttributes.includes(attr.name)) { | ||
el.removeAttribute(attr.name); | ||
} | ||
}); | ||
|
||
if (!el.innerHTML.trim()) { | ||
el.remove(); | ||
} | ||
}); | ||
|
||
removeSelectors.forEach((selector) => { | ||
copiedDocument.querySelectorAll(selector).forEach((el) => el.remove()); | ||
}); | ||
|
||
const mainContent = copiedDocument.body.innerHTML; | ||
return mainContent.replace(/\s+/g, " ").trim(); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters