-
Notifications
You must be signed in to change notification settings - Fork 0
/
searcher.html
57 lines (57 loc) · 1.59 KB
/
searcher.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<title>Hello, news!</title>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width,initial-scale=1" />
<script>
function load (url) {
return new Promise(async function (resolve, reject) {
const res = await fetch(url)
resolve(res.json())
})
}
</script>
</head>
<body>
<fieldset>
<legend>Enter your data</legend>
Query:
<input type="text" id="queryGet" />
NewsApi Key:
<input type="text" id="apiKeyGet" />
<button type = "button" id="btnGet" onclick="getNews()">Submit</button>
</fieldset>
<div>
<h1>News</h1>
<div id="news"></div>
</div>
<script>
function getNews(){
const query = document.getElementById("queryGet").value;
const apiKey = document.getElementById("apiKeyGet").value;
const endpoint = new URL('https://newsapi.org/v2/everything?apiKey='+apiKey+'&q='+query);
const data = load(endpoint);
data.then((myData) => {
let string,sentence='<ol>';
const news = document.getElementById("news");
for(const entry of myData.articles){
sentence += '<li>';
string = entry.title.split(' ').map((word)=>{
if(word.toLowerCase().includes(query.toLowerCase())){
return '<b>'+word.trim()+'</b>';
}
return word;
});
for(let i in string){
sentence+=(string[i].trim()+' ');
}
sentence+='</li>';
}
sentence+='</ol>';
news.innerHTML+=sentence;
});
}
</script>
</body>
</html>