-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
33 lines (27 loc) · 935 Bytes
/
server.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
import { DOMParser } from "https://deno.land/x/[email protected]/deno-dom-wasm.ts";
async function scrape(url) {
const response = await fetch(url);
const responseText = await response.text();
return responseText;
}
// taken from https://www.somacon.com/p355.php
function trimString(inputString) { // remove spaces from the beginning and the end
return inputString.replace(/^\s+|\s+$/g,"");
}
let responseText = await scrape("https://www.freecodecamp.org/news/")
let document = new DOMParser().parseFromString(
responseText,
"text/html"
);
let postTitles = document.querySelectorAll("h2.post-card-title");
let postTitlesText = [];
for(let i = 0; i < postTitles.length; i++) {
/*
Titles contain redundant spaces e.g
How to learn Python
First, let's cut that spaces
*/
const postTitle = trimString(postTitles[i].textContent);
postTitlesText.push(postTitle);
}
console.log(postTitlesText);