-
Notifications
You must be signed in to change notification settings - Fork 3
/
indexer.js
54 lines (44 loc) · 1.58 KB
/
indexer.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
const { chromium } = require("playwright");
const fs = require("fs");
(async () => {
const browser = await chromium.launch();
const page = await browser.newPage();
await page.goto("https://www.rareskills.io/blog");
let lastScrollHeight = 0;
let currentScrollHeight = await page.evaluate(
() => document.body.scrollHeight
);
while (lastScrollHeight < currentScrollHeight) {
lastScrollHeight = currentScrollHeight;
await page.evaluate("window.scrollTo(0, document.body.scrollHeight)");
await page.waitForTimeout(4000);
currentScrollHeight = await page.evaluate(() => document.body.scrollHeight);
console.log("Scroll height:", currentScrollHeight);
}
const posts = await page.evaluate(() => {
const items = Array.from(
document.querySelectorAll('[data-hook="post-list-item"]')
);
return items.map((item) => {
const titleElement = item.querySelector('[data-hook="post-title"] p');
const descriptionElement = item.querySelector(
'[data-hook="post-description"] div div'
);
const linkElement = item.querySelector("a");
return {
title: titleElement?.innerText,
link: linkElement?.href,
description: descriptionElement?.innerText,
};
});
});
posts.reverse();
let readmeContent = `# Rareskills Blog Posts Index\n\n`;
posts.forEach((post) => {
readmeContent += `## ${post.title} [🔗](${post.link})\n`;
readmeContent += `${post.description}\n\n`;
});
fs.writeFileSync("README.md", readmeContent);
console.log("README created.");
await browser.close();
})();