-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpopup.js
More file actions
46 lines (39 loc) · 1.49 KB
/
popup.js
File metadata and controls
46 lines (39 loc) · 1.49 KB
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
const appKey = "9edd1dcf282ecdbb06df5fd308848b7f";
let searchButton = document.getElementById("search-btn");
let searchInput = document.getElementById("search-txt");
let cityName = document.getElementById("city-name");
let icon = document.getElementById("icon");
let temperature = document.getElementById("temp");
let humidity = document.getElementById("humidity-div");
searchButton.addEventListener("click", findWeatherDetails);
searchInput.addEventListener("keyup", enterPressed);
function enterPressed(event) {
if (event.key === "Enter") {
findWeatherDetails();
}
}
function findWeatherDetails() {
if (searchInput.value === "") {
}else {
let searchLink = "https://api.openweathermap.org/data/2.5/weather?q=" + searchInput.value + "&appid="+appKey;
httpRequestAsync(searchLink, theResponse);
}
}
function theResponse(response) {
let jsonObject = JSON.parse(response);
cityName.innerHTML = jsonObject.name;
icon.src = "http://openweathermap.org/img/w/" + jsonObject.weather[0].icon + ".png";
temperature.innerHTML = parseInt(jsonObject.main.temp - 273) + "°";
humidity.innerHTML = jsonObject.main.humidity + "%";
}
function httpRequestAsync(url, callback)
{
console.log("hello");
var httpRequest = new XMLHttpRequest();
httpRequest.onreadystatechange = () => {
if (httpRequest.readyState == 4 && httpRequest.status == 200)
callback(httpRequest.responseText);
}
httpRequest.open("GET", url, true); // true for asynchronous
httpRequest.send();
}