forked from saiganeshposhith/Random-news-extension
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
41 lines (38 loc) · 1.26 KB
/
script.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
const apiKey = "1b3e311c99e24e7aa88eba3d1cfeaea4";
const container = document.querySelector(".card-container");
let response, articles;
function displayArticle(element) {
container.innerHTML=""
let div = document.createElement("div");
div.classList.add("card");
let imgdiv = document.createElement("div");
imgdiv.classList.add("img");
let img = document.createElement("img");
img.src = element.urlToImage;
img.alt= element.title
imgdiv.append(img);
let tit = document.createElement("h2");
tit.classList.add("title");
tit.innerText = element.title;
let p = document.createElement("p");
p.classList.add("desc");
p.innerText = element.description;
div.appendChild(imgdiv);
div.appendChild(tit);
div.appendChild(p);
container.append(div);
div.addEventListener('click',() => {
open(element.url,'_blank')
})
}
document.getElementById("news").addEventListener("click", async () => {
try {
response = await fetch(
`https://newsapi.org/v2/top-headlines?country=in&pageSize=100&apiKey=${apiKey}`
);
articles = await response.json();
displayArticle(articles.articles[Math.ceil(Math.random() * articles.totalResults)]);
} catch (err) {
console.log(err);
}
});