-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathscript.js
62 lines (52 loc) · 2.19 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// https://gnews.io/api/v4/top-headlines?token=a175ac9bf19a19e7e95f50136ebffcbb&country=in&lang=en
//https://newsapi.org/v2/top-headlines?sources=bbc-news&apiKey=efbe6953b5e24dc7857c47ac7e35e650
const ball = document.querySelector(".toggle-ball");
const items = document.querySelectorAll("body,main,.dark,.container,div,.toggle,ul,.footer,.button,.icon-container,.navHai,nav,.social,.hovereffect");
ball.addEventListener("click",()=>{
items.forEach(item=>{
item.classList.toggle("active")
})
ball.classList.toggle("active")
})
function myFunction() {
var element = document.body;
element.classList.toggle("dark-mode");
}
async function getgeolocation() {
let url = "https://ipapi.co/json";
let response = await fetch(url);
let data = await response.json();
var current_location = data.country_code;
var current__lang = data.languages;
document.getElementById("country").innerHTML = data.country_name;
async function getnews() {
let full_url =
"https://gnews.io/api/v4/top-headlines?token=a175ac9bf19a19e7e95f50136ebffcbb&country=" +
current_location.toLowerCase() +
"&lang=" +
current__lang.toLowerCase();
let newsResponse = await fetch(full_url);
let newsData = await newsResponse.json();
if (newsResponse.ok) {
for (var i = 0; i < newsData.articles.length; i++) {
document.getElementById(
"news"
).innerHTML += `<div class="card" style="width: 18rem;">
<img src=${newsData.articles[i].image} class="card-img-top" alt="...">
<div class="card-body">
<p class="card-text">${newsData.articles[i].title}</p>
<a href=${newsData.articles[i].url} class="btn btn-primary">Read full article</a>
</div>
</div>
`;
}
} else {
document.getElementById(
"news"
).innerHTML = `<div class="error-container"><p>Oops!</p><p>Looks like we are having some trouble loading the news.</p><p>We request you to try again later.</p></div>`;
console.log(`Error ${newsResponse.status}: ${newsData.errors[0]}`);
}
}
getnews();
}
getgeolocation();