-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
308 lines (248 loc) · 8.46 KB
/
index.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
// 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("click", () => {
SearchNews();
});
// fetching data from api but works only for two keyWords "tesla" and "twitter"
const SearchNews = async () => {
let query = document.getElementById("query").value;
localStorage.setItem("query", JSON.stringify(query));
window.location.href = "./search.html";
// 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();
let getData = async () => {
let res = await fetch(
`https://masai-mock-api-2.herokuapp.com/news/top-headlines?country=in`
);
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";
}
const videos = [
{
url: "https://cdn.dnaindia.com/sites/default/files/styles/half/public/2022/10/01/2542891-01thumbnail1801721.jpeg",
title: "Watch: PM Modi launches 5G services in India",
link: "https://www.dnaindia.com/india/video-watch-pm-modi-launches-5g-services-in-india-2989431",
},
{
url: "https://cdn.dnaindia.com/sites/default/files/styles/half/public/2022/10/01/2542831-whatsapp-image-2022-10-01-at-1.23.44-pm-1.jpeg",
title: "DNA | Russia announces absorption of 4 occupied Ukrainian regions",
link: "https://www.dnaindia.com/india/video-dna-russia-announces-absorption-of-4-occupied-ukrainian-regions-2989384",
},
{
url: "https://cdn.dnaindia.com/sites/default/files/styles/half/public/2022/10/01/2542832-whatsapp-image-2022-10-01-at-1.23.44-pm-3.jpeg",
title:
"DNA | What is IAMC? Know how its spreading propaganda against India",
link: "https://www.dnaindia.com/india/video-dna-what-is-iamc-know-how-its-spreading-propaganda-against-india-2989382",
},
{
url: "https://cdn.dnaindia.com/sites/default/files/styles/half/public/2022/10/01/2542882-01thumbnail1801722.jpeg",
title: "WATCH:PM Modi drives car in Sweden from Delhi using 5G",
link: "https://www.dnaindia.com/india/video-watchpm-modi-drives-car-in-sweden-from-delhi-using-5g-2989438",
},
];
const slideShow3 = () => {
let count = 0;
let div = document.getElementById("3");
let img = document.createElement("img");
img.src = videos[0].url;
let p = document.createElement("p");
p.innerText = videos[0].title;
div.append(img, p);
count = count + 1;
setInterval(function () {
if (count == 3) {
count = 0;
}
img.src = videos[count].url;
p.innerText = videos[count].title;
count++;
div.append(img, p);
}, 4000);
};
slideShow3();
const slideShow4 = () => {
let count = 1;
let div = document.getElementById("4");
let img = document.createElement("img");
img.src = videos[1].url;
let p = document.createElement("p");
p.innerText = videos[1].title;
div.append(img, p);
count = count + 1;
setInterval(function () {
if (count == 4) {
count = 1;
}
img.src = videos[count].url;
p.innerText = videos[count].title;
count++;
div.append(img, p);
}, 4000);
};
slideShow4();