document.addEventListener('DOMContentLoaded', () => { const darkModeToggle = document.getElementById('darkModeToggle'); const body = document.body; const celsiusRadio = document.getElementById('celsius'); const fahrenheitRadio = document.getElementById('fahrenheit'); // Apply the cached theme on page load if (localStorage.getItem('darkMode') === 'enabled') { body.classList.add('dark-mode'); darkModeToggle.checked = true; } darkModeToggle.addEventListener('change', () => { if (darkModeToggle.checked) { body.classList.add('dark-mode'); localStorage.setItem('darkMode', 'enabled'); } else { body.classList.remove('dark-mode'); localStorage.setItem('darkMode', 'disabled'); } }); const weatherDisplay = document.getElementById('weather-display'); const citySearchForm = document.getElementById('city-search-form'); const cityInput = document.getElementById('city-input'); const favoriteLocationsList = document.getElementById('favorite-locations-list'); const searchHistoryContainer = document.getElementById('search-history'); const getUnit = () => localStorage.getItem('unit') || 'metric'; const getSearchHistory = () => JSON.parse(localStorage.getItem('searchHistory')) || []; const saveSearchHistory = (city) => { let history = getSearchHistory(); history = history.filter(item => item.toLowerCase() !== city.toLowerCase()); history.unshift(city); if (history.length > 5) { history.pop(); } localStorage.setItem('searchHistory', JSON.stringify(history)); displaySearchHistory(); }; const displaySearchHistory = () => { const history = getSearchHistory(); if (history.length > 0) { let historyHtml = '
Loading weather...
Air Quality: ${getAqiString(data.aqi)}
`; } let forecastHtml = ''; if (data.forecast && data.forecast.length > 0) { forecastHtml += '${dayName}
${day.temp.toFixed(1)}${unitSymbol}
${data.weather[0].description}
Humidity: ${data.main.humidity}%
${aqiHtml} ${forecastHtml}| Time | Temp | Humidity | Description |
|---|---|---|---|
| ${recordDate.toLocaleTimeString()} | ${record.temperature}°C | ${record.humidity}% | ${record.description} |