-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
43 lines (32 loc) · 1.2 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
// EDIT THIS FILE TO COMPLETE ASSIGNMENT QUESTION 1
const { chromium } = require("playwright");
async function sortHackerNewsArticles() {
// launch browser
const browser = await chromium.launch({ headless: false });
const context = await browser.newContext();
const page = await context.newPage();
// go to Hacker News
await page.goto("https://news.ycombinator.com/newest");
await page.waitForSelector('.athing');
const times = []
while(times.length<100){
const articleTimes = await page.evaluate(async () => {
const articles = document.querySelectorAll('.athing');
return Array.from(articles).map(article => {
const timeElement = article.nextElementSibling.querySelector('.age');
return timeElement ? timeElement.getAttribute('title').split(" ")[0] : null;
})
})
times.push(...articleTimes)
await page.locator('.morelink').click()
}
const isSortedDescending = times.slice(0,100).every((time, index) =>
index === 0 || new Date(time) <= new Date(times[index - 1])
)
console.log('Articles sorted correctly:', isSortedDescending);
await browser.close();
return isSortedDescending;
}
(async () => {
await sortHackerNewsArticles();
})();