-
Notifications
You must be signed in to change notification settings - Fork 1
/
search.js
237 lines (188 loc) · 6.24 KB
/
search.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
// https://newsapi.org/v2/top-headlines?country=us&category=business&apiKey=e1d001bb4e184ec5b1cabd0da5447752
import { navbar } from "./components/navbar.js";
document.getElementById("navbar").innerHTML = navbar();
import { newsSlider } from "./components/navbar.js";
document.getElementById("news_slider").innerHTML = newsSlider();
import { sidebar } from "./components/navbar.js";
document.getElementById("sidebar").innerHTML = sidebar();
import { rightSidebar } from "./components/navbar.js";
document.getElementById("right_sidebar").innerHTML = rightSidebar();
import { footer } from "./components/navbar.js";
document.getElementById("footer").innerHTML = footer();
let query = document.getElementById("query");
query.disabled = true;
let searchbtn = document.getElementById("SearchButton");
searchbtn.disabled = true;
searchbtn.style.backgroundColor = "#ffde2f";
searchbtn.style.border = "none";
searchbtn.innerText = "";
// Adding some style and enabling the button and search input
document.querySelector("#searchNews").addEventListener("click", () => {
query.disabled = false;
query.style.backgroundColor = "White";
query.placeholder = "Search News Here";
query.style.padding = "15px";
document.getElementById("SearchButton").style.padding = "12px";
document.querySelector("#searchNews").disabled = true;
searchbtn.innerText = "Search";
searchbtn.disabled = false;
searchbtn.style.border = "black";
searchbtn.style.cursor = "pointer";
});
// adding event to search button to fetch the data from api
// let Search = document.getElementById("SearchButton");
// Search.addEventListener("load", () => {
// SearchNews();
// });
window.onload = function () {
SearchNews();
};
// fetching data from api but works only for two keyWords "tesla" and "twitter"
async function SearchNews() {
let query = JSON.parse(localStorage.getItem("query"));
try {
let res = await fetch(`https://masai-api.herokuapp.com/news?q=${query}`);
let data = await res.json();
console.log(data.articles);
displayData(data.articles);
} catch (error) {
console.log(error);
}
}
// Showing Fetch data to container div you can append data to your respective div
const displayData = (data) => {
let container = document.getElementById("indian-news");
container.innerHTML = null;
data.forEach((ele) => {
let div = document.createElement("div");
let image = document.createElement("img");
image.src = ele.urlToImage;
let title = document.createElement("h4");
title.innerText = ele.title;
div.append(image, title);
container.append(div);
});
};
let poster = [
"Three new credit card rules coming into effect from Oct.",
"Ankita Bhandari murder case to be tried in fast-track court; Uttarakhand CM announces Rs 25 lakh compensation for family",
"Gandhis still keen on Gehlot contesting party prez poll...",
"Amid F-16 strain, US talks of India-Pakistan dialogue",
"Centre bans PFI for 5 years under anti-terror law UAPA ...",
];
let id;
function Start() {
let container = document.getElementById("breaking_news");
let i = 0;
let p = document.createElement("p");
let id = setInterval(function () {
if (i === 5) {
i = 0;
}
p.innerText = poster[i];
container.append(p);
i++;
}, 2000);
}
function Stop() {
clearInterval(id);
}
Start();
const arr = [
{
url: "https://cdn.dnaindia.com/sites/default/files/styles/half/public/2022/09/28/2542170-stations-tb-ani.jpg",
title: "Centre gives sneak peek at Indian Railway Stations of the...",
},
{
url: "https://cdn.dnaindia.com/sites/default/files/styles/half/public/2022/09/17/2539410-pm1.jpg",
title: "PM Narendra Modis Birthday: 5 schemes launched by Prime Mini...",
},
{
url: "https://cdn.dnaindia.com/sites/default/files/styles/half/public/2022/09/16/2539086-2532577-leopard.jpg",
title: "Fascinating facts about all 8 big cats...",
},
{
url: "https://cdn.dnaindia.com/sites/default/files/styles/half/public/2022/09/14/2538609-chenab-bridge.jpg",
title: "Chenab Bridge: Indian Railways shares stunning pictures of the...",
},
{
url: "https://cdn.dnaindia.com/sites/default/files/styles/half/public/2022/09/17/2539361-cheetah.jpg",
title: "In Pics: PM Modi releases 8 cheetahs from Namibia at Kuno Na...",
},
];
const slideShow = () => {
let count = 0;
let div = document.getElementById("1");
let img = document.createElement("img");
img.src = arr[0].url;
let p = document.createElement("p");
p.innerText = arr[0].title;
div.append(img, p);
count = count + 1;
setInterval(function () {
if (count == 4) {
count = 0;
}
img.src = arr[count].url;
p.innerText = arr[count].title;
count++;
div.append(img, p);
}, 4000);
};
slideShow();
const slideShow2 = () => {
let count = 1;
let div = document.getElementById("2");
let img = document.createElement("img");
img.src = arr[1].url;
let p = document.createElement("p");
p.innerText = arr[1].title;
div.append(img, p);
count = count + 1;
setInterval(function () {
if (count == 5) {
count = 1;
}
img.src = arr[count].url;
p.innerText = arr[count].title;
count++;
div.append(img, p);
}, 4000);
};
slideShow2();
// https://newsapi.org/v2/top-headlines?country=in&category=&apiKey=e1d001bb4e184ec5b1cabd0da5447752
let getData = async () => {
let res = await fetch(
`https://newsapi.org/v2/top-headlines?country=in&category=&apiKey=e1d001bb4e184ec5b1cabd0da5447752`
);
let data = await res.json();
let actual_data = data.articles;
console.log("data", actual_data);
append(actual_data);
};
getData();
const append = (result) => {
let container = document.getElementById("indian-news");
result.forEach((el) => {
if (el.urlToImage !== null) {
let div = document.createElement("div");
let img = document.createElement("img");
img.src = el.urlToImage;
let p = document.createElement("p");
p.innerText = el.title;
// let data ={
// id:el.url
// }
div.addEventListener("click", function () {
Click(el);
});
div.append(img, p);
container.append(div);
}
});
};
function Click(el) {
localStorage.setItem("india", JSON.stringify(el));
console.log(el);
window.location.href = "./details.html";
}