-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpuppet.js
53 lines (45 loc) · 1.41 KB
/
puppet.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
import puppeteer from "https://deno.land/x/puppeteer/mod.ts";
export async function visit(url) {
console.log(`Visiting ${url}`);
if (!url) {
throw new Error("URL is required");
}
const browser = await puppeteer.launch({
args: [
"--incognito",
"--no-sandbox",
"--disable-setuid-sandbox",
'--user-agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"',
],
headless: true,
});
const page = await browser.newPage();
try {
await page.goto(url, { waitUntil: "networkidle2" });
// Extract text content
const content = await page.evaluate(() => {
function getTextFromElement(element) {
return element.innerText || element.textContent || "";
}
function removeScriptsAndStyles(doc) {
const scripts = doc.querySelectorAll("script, style");
scripts.forEach((script) => script.remove());
}
function appendLinkTexts(doc) {
const links = doc.querySelectorAll("a[href]");
links.forEach((link) => {
const linkText = ` [${link.href}]`;
link.append(linkText);
});
}
removeScriptsAndStyles(document);
appendLinkTexts(document);
return getTextFromElement(document.body).trim();
});
await browser.close();
return content;
} catch (error) {
await browser.close();
throw error;
}
}