abeea commited on
Commit
57a87da
·
verified ·
1 Parent(s): 0e38302

Update script.js

Browse files
Files changed (1) hide show
  1. script.js +14 -31
script.js CHANGED
@@ -1,40 +1,23 @@
1
  const apiKey = "b029ca9ef3e3419e8ea753449c279def";
2
 
3
- // Default news load
4
- window.onload = () => {
5
- fetchNews("latest");
6
- };
7
-
8
- function getNews() {
9
- let query = document.getElementById("search").value;
10
- fetchNews(query);
11
- }
12
-
13
  function fetchNews(query) {
14
  let url = `https://newsapi.org/v2/everything?q=${query}&apiKey=${apiKey}`;
15
 
16
- fetch(url)
17
- .then(res => res.json())
 
 
 
 
 
 
 
 
18
  .then(data => {
 
19
  displayNews(data.articles);
 
 
 
20
  });
21
- }
22
-
23
- function displayNews(articles) {
24
- let container = document.getElementById("news-container");
25
- container.innerHTML = "";
26
-
27
- articles.forEach(article => {
28
- let newsDiv = document.createElement("div");
29
- newsDiv.classList.add("news");
30
-
31
- newsDiv.innerHTML = `
32
- <img src="${article.urlToImage || ''}" />
33
- <h3>${article.title}</h3>
34
- <p>${article.description || ''}</p>
35
- <a href="${article.url}" target="_blank">Read More</a>
36
- `;
37
-
38
- container.appendChild(newsDiv);
39
- });
40
  }
 
1
  const apiKey = "b029ca9ef3e3419e8ea753449c279def";
2
 
 
 
 
 
 
 
 
 
 
 
3
  function fetchNews(query) {
4
  let url = `https://newsapi.org/v2/everything?q=${query}&apiKey=${apiKey}`;
5
 
6
+ // ✅ Proxy lagao
7
+ let proxyUrl = "https://proxy.corsfix.com/?";
8
+
9
+ fetch(proxyUrl + url)
10
+ .then(res => {
11
+ if (!res.ok) {
12
+ throw new Error("HTTP Error: " + res.status);
13
+ }
14
+ return res.json();
15
+ })
16
  .then(data => {
17
+ console.log("DATA:", data);
18
  displayNews(data.articles);
19
+ })
20
+ .catch(err => {
21
+ console.error("ERROR:", err);
22
  });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
  }