-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
89 lines (71 loc) · 2.14 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
var Input = document.getElementById("input")
Input.addEventListener("keypress",e=>{
if (e.key=="Enter"){
reload()
}
})
function reload(){
var text = "technology sports education"
if (Input.value != ""){
text = Input.value
}
console.log(text)
fetch(`https://contextualwebsearch-websearch-v1.p.rapidapi.com/api/search/NewsSearchAPI?q=${text.trim().replace(" ","%20")}&pageNumber=1&pageSize=30&autoCorrect=true&safeSearch=false&withThumbnails=true&fromPublishedDate=null&toPublishedDate=null`, {
"method": "GET",
"headers": {
"x-rapidapi-key": "af93dfbf1fmsh11afdd00a127d49p1fee0fjsneaa96fe41718",
"x-rapidapi-host": "contextualwebsearch-websearch-v1.p.rapidapi.com"
}
})
.then(response => response.json())
.then(data => {
data = data.value
var div1 = document.getElementById("primary")
var div2 = document.getElementById("secondary")
var div3 = document.getElementById("tertiary")
div1.innerHTML=""
div2.innerHTML=""
div3.innerHTML=""
var i=0
data.forEach(value => {
if (i%3 == 0){
div1.innerHTML+= `
<div class="card mb-3">
<img src="${value.image.url}" class="card-img-top" alt="">
<div class="card-body">
<h5 class="card-title">${value.title}</h5>
<p class="card-text">${value.description}</p>
<a href="${value.url}" class="btn btn-primary">Read More</a>
</div>
</div>`
}
else if (i%3 == 1){
div2.innerHTML+= `
<div class="card mb-3">
<img src="${value.image.url}" class="card-img-top" alt="">
<div class="card-body">
<h5 class="card-title">${value.title}</h5>
<p class="card-text">${value.description}</p>
<a href="${value.url}" class="btn btn-primary">Read More</a>
</div>
</div>`
}
else {
div3.innerHTML+= `
<div class="card mb-3">
<img src="${value.image.url}" class="card-img-top" alt="">
<div class="card-body">
<h5 class="card-title">${value.title}</h5>
<p class="card-text">${value.description}</p>
<a href="${value.url}" class="btn btn-primary">Read More</a>
</div>
</div>`
}
i++
});
})
.catch(err => {
console.error(err);
});
}
reload()