forked from ossd-s24/weathermen
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbackground.js
30 lines (22 loc) · 857 Bytes
/
background.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
//key= 41b01f731e59ef15c4219f26e163e2f9
const apiURL = 'https://api.openweathermap.org/data/2.5/weather?q=NewYork&appid=41b01f731e59ef15c4219f26e163e2f9&units=metric';
// Function to fetch weather data
function fetchWeather() {
fetch(apiURL)
.then(response => {
if (response.ok) return response.json();
throw new Error('Failed to fetch weather data.');
})
.then(data => {
console.log(data);
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
chrome.tabs.sendMessage(tabs[0].id, {weatherData: data});
});
})
.catch(error => console.error('Error fetching weather:', error));
}
// Listen for an event
chrome.browserAction.onClicked.addListener((tab) => {
fetchWeather();
});
fetchWeather();