-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
66 lines (53 loc) · 1.85 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
63
64
65
66
async function postData() {
let response = await fetch('http://127.0.0.1:5000/get_news', {
method: 'POST',
cache: 'default',
body: JSON.stringify({"current_hour": new Date().toISOString()}),
headers: {
"Content-type": "application/json; charset=UTF-8"
}
})
return response.json()
}
function processAPIResponse(newsData) {
// console.log(newsData);
const headline = newsData[0].news_headline;
const newsLink = newsData[0].news_link;
const publisher = newsData[0].news_publisher;
const publisherElement = document.getElementById('publisherElement');
publisherElement.innerHTML = publisher;
const headlineElement = document.getElementById('headlineElement');
headlineElement.innerHTML = headline;
headlineElement.setAttribute('href', newsLink);
document.getElementById('newsIDElement').innerHTML =
newsData[0].id + 1 + '. ';
// adding further news headlines
var len = newsData.length;
console.log(len);
len = Math.min(len, 3);
for (var i = 1; i < len; i++) {
let newsBlock = document.createElement('div');
let np = document.createElement('h3');
let node = document.createTextNode(newsData[i].news_publisher);
np.appendChild(node);
np.classList.add('finCLI-Chrome-Publisher');
let nh = document.createElement('a');
let nh_text = document.createTextNode(
newsData[i].id + 1 + '. ' + newsData[i].news_headline
);
nh.classList.add('finCLI-Chrome-Headline');
let nh_link = newsData[i].news_link;
// console.log(typeof nh_link)
nh.href = nh_link;
nh.appendChild(nh_text);
newsBlock.append(np);
newsBlock.append(nh);
let element = document.getElementById('IN-News');
element.appendChild(newsBlock);
}
}
postData()
.then((res) => {
if (res.status === 'success') processAPIResponse(res.data.india);
})
.catch((Error) => console.log(Error));