-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
76 lines (63 loc) · 2.06 KB
/
index.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
const puppeteer = require("puppeteer");
const fs = require('fs');
const urlToSave = [
]
const visitAndScreenshot = async (page, urlList) => {
// dismiss notification pop-up
await page.goto('YOUR TWITTER PROFILE URL');
await page.waitForTimeout(10000);
await page.keyboard.press("Tab");
await page.keyboard.press("Tab");
await page.keyboard.press("Enter");
for (i = 0; i < urlList.length; i++) {
const theName = urlList[i][0]
const theUrl = urlList[i][1];
try {
await page.goto(theUrl, {
waitUntil: 'networkidle0', timeout: 0
});
const bodyHeight = 1080
await page.setViewport({ width: 800, height: bodyHeight });
await page.setDefaultTimeout(5000)
// See if there's a description and if yes, add it to the enhanced list
const descriptionSelector = '[data-testid="UserDescription"]'
await page.waitForSelector(descriptionSelector);
let elementDescription = await page.$(descriptionSelector);
let textDescription = await page.evaluate(el => el.textContent, elementDescription)
if(textDescription){
urlList[i].push(textDescription)
}
// See if there's a url and if yes, add it to the enhanced list
const urlSelector = '[data-testid="UserUrl"]'
await page.waitForSelector(urlSelector);
let elementUrl = await page.$(urlSelector);
let textUrl = await page.evaluate(el => el.textContent, elementUrl)
if(textUrl){
urlList[i].push(textUrl)
}
// Save a screenshot for a good measure
// await page.screenshot({ path: `./${theName}.jpg` })
}
catch (error) {
console.log(error)
}
}
}
const fetchScreenshots = async (urlList) => {
try {
const browser = await puppeteer.launch({product:'chrome', headless: false});
const page = await browser.newPage();
await page.setViewport({ width: 1920, height: 1080 });
await visitAndScreenshot(page, urlList);
await browser.close();
fs.writeFile('enhancedList.json', JSON.stringify(urlList), (err) => {
if (err) throw err;
console.log('Saved the enhanced list file')
}
)
}
catch (error) {
console.log(error);
}
}
fetchScreenshots(urlToSave);