-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbackground.js
49 lines (48 loc) · 1.76 KB
/
background.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
document.addEventListener("DOMContentLoaded", () => {
let articles = [];
//Fetching the featured articles for the day
fetch(
"https://api.hashnode.com/?query={storiesFeed(type:%20FEATURED){title,author{username,blogHandle,publicationDomain},coverImage,slug,dateFeatured}}"
)
.then(function (response) {
return response.json();
})
.then(function (data) {
articles = data.data.storiesFeed;
setTimeout(() => {
createList(articles);
document.getElementById("loader").style.display = "none";
}, 2000);
})
.catch(function (e) {
console.log(e);
});
//creating list
function createList(articles) {
let list = document.querySelector("#list");
for (let i = 0; i < articles.length; i++) {
let listItem = document.createElement("li");
let imgCover = document.createElement("img");
articles[i].coverImage != ""
? (imgCover.src = articles[i].coverImage)
: (imgCover.src = "images/no-img.png");
let title = document.createElement("h2");
title.appendChild(document.createTextNode(articles[i].title));
let author = document.createElement("h3");
author.appendChild(
document.createTextNode(`by ${articles[i].author.username}`)
);
let anchor = document.createElement("a");
let publicationDomain = articles[i].author.publicationDomain;
anchor.href = publicationDomain
? `https://${publicationDomain}/${articles[i].slug}`
: `https://${articles[i].author.blogHandle}.hashnode.dev/${articles[i].slug}`;
anchor.target = "_blank";
anchor.appendChild(imgCover);
anchor.appendChild(title);
listItem.appendChild(anchor);
listItem.appendChild(author);
list.appendChild(listItem);
}
}
});