-
Notifications
You must be signed in to change notification settings - Fork 0
/
intro.mjs
90 lines (51 loc) · 2.32 KB
/
intro.mjs
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
import { link } from "fs";
import puppeteer from "puppeteer";
import { setTimeout } from "timers/promises";
import PQueue from 'p-queue';
import fs from 'fs';
import path from 'path';
const browser = await puppeteer.launch({ headless: false,
defaultViewport: {width:1920,height:1080},
userDataDir: "temporary"
});
const page = await browser.newPage();
await page.goto("https://www.duckduckgo.com", { waitUntil: "networkidle2" });
const searchBarHandle = await page.waitForSelector('#searchbox_homepage');
await searchBarHandle.type('devconfbd')
await page.keyboard.press('Enter')
const firstResult = await page.waitForSelector('[data-testid="result-title-a"]')
await firstResult.click()
await page.waitForSelector('.sponsors a','.supporter a')
const sponsorlink = await page.evaluate(()=>{
return [...document.querySelectorAll('.sponsors a')].map(a=>a.href)
})
const supporterlink = await page.evaluate(()=>{
return [...document.querySelectorAll('.supporter a')].map(a=>a.href)
})
async function getLinks(link){
const page = await browser.newPage();
await page.goto(link, { waitUntil: "networkidle2" });
const title = await page.title()
const hostname = await page.evaluate(()=> window.location.hostname)
// Ensure 'images' directory exists
const dir = './images';
if (!fs.existsSync(dir)){
fs.mkdirSync(dir);
}
// Save screenshot to 'images' directory
await page.screenshot({path: path.join(dir, `${hostname}.png`)});
const facebookLInk = await page.evaluate(
()=> document.querySelector('a[href*=facebook]')?.href);
const twitterLink = await page.evaluate(()=> document.querySelector('a[href*=twitter]')?.href);
const linkedinLink = await page.evaluate(()=> document.querySelector('a[href*=linkedin]')?.href);
const careerLink = await page.evaluate(()=> document.querySelector('a[href*=career]')?.href);
// await page.$$eval('a[href*=career]',(a)=>a.map(a=>a.href))
console.log({link,title,hostname,facebookLInk,twitterLink,linkedinLink,careerLink})
await page.close()
}
const queue = new PQueue({concurrency: 3})
for(let link of supporterlink){
queue.add(()=>getLinks(link)).catch(console.log)
}
await queue.onEmpty()
await browser.close();