@@ -6,7 +6,8 @@ const windspeed = document.querySelector(".windspeed");
66const btn = document . querySelector ( ".srch-btn" ) ;
77const cityOp = document . querySelector ( ".city-name-output" ) ;
88
9- const apiKey = "6244ab888f079565d5ce1deabddc3f77" ;
9+ // IMPORTANT: Replace "YOUR_API_KEY" with your actual OpenWeatherMap API key."6244ab888f079565d5ce1deabddc3f77"
10+ const apiKey = "YOUR_API_KEY" ;
1011async function getWeatherInfo ( city ) {
1112 try {
1213 console . log ( city ) ;
@@ -15,11 +16,15 @@ async function getWeatherInfo(city) {
1516 }
1617
1718 let res = await fetch (
18- `https://api.openweathermap.org/data/2.5/weather?q=${ city } &units=metric&appid=${ apiKey } `
19+ `https://api.openweathermap.org/data/2.5/weather?q=${ city } &units=metric&appid=6244ab888f079565d5ce1deabddc3f77 `
1920 ) ;
2021 console . log ( res ) ;
2122 if ( ! res . ok ) {
22- throw Error ( "Error occured.Could not find the city " ) ;
23+ if ( res . status === 404 ) {
24+ throw Error ( "City not found" ) ;
25+ } else if ( res . status === 401 ) {
26+ throw Error ( "Access denied.Check your api key " ) ;
27+ }
2328 }
2429 res = await res . json ( ) ;
2530 console . log ( res ) ;
@@ -28,7 +33,6 @@ async function getWeatherInfo(city) {
2833 const { speed } = res . wind ;
2934 const name = res . name ;
3035 const desc = res . weather [ 0 ] . main ;
31- console . log ( res . weather [ 0 ] . description ) ;
3236 assignWeatherDetails ( speed , temp , humidity , feels_like , name , desc ) ;
3337 } catch ( e ) {
3438 document . querySelector ( ".city-name-input" ) . value = "" ;
@@ -50,9 +54,26 @@ function assignWeatherDetails(s, t, h, fl, n, desc) {
5054 humidity . innerHTML = h ;
5155 feels_like . innerHTML = fl + "<sup>o</sup>C" ;
5256 cityOp . innerHTML = n ;
57+ // Use textContent to prevent XSS vulnerabilities
58+ windspeed . textContent = s ;
59+ humidity . textContent = h ;
60+ cityOp . textContent = n ;
61+
62+ temp . innerHTML = "" ;
63+ feels_like . innerHTML = "" ;
64+
65+ temp . textContent = t ;
66+ temp . insertAdjacentHTML ( "beforeend" , "<sup>o</sup>C" ) ;
67+ const descPara = document . createElement ( "p" ) ;
68+ descPara . textContent = desc ;
69+ temp . appendChild ( descPara ) ;
70+
71+ feels_like . textContent = fl ;
72+ feels_like . insertAdjacentHTML ( "beforeend" , "<sup>o</sup>C" ) ;
73+
5374 city . value = "" ;
5475}
5576
5677window . addEventListener ( "load" , ( ) => {
57- getWeatherInfo ( "kurnool " ) ;
78+ getWeatherInfo ( "London " ) ;
5879} ) ;
0 commit comments