(function() { const el = document.getElementById('weather-preview'); if (!el) return; function getCachedWeather() { const now = Date.now(); const keys = Object.keys(localStorage).filter(k => k.startsWith('weather:')); if (!keys.length) return null; let newest = null; for (const key of keys) { try { const entry = JSON.parse(localStorage.getItem(key)); if (!entry || entry.expires < now) continue; if (!newest || entry.expires > newest.expires) newest = entry; } catch { } } return newest?.data || null; } function getIcon(iconCode) { const map = { '01d': 'clear-day-black', '01n': 'clear-night-black', '02d': 'partly-cloudy-day-black', '02n': 'partly-cloudy-night-black', '09d': 'light-rain', '09n': 'light-rain', '10d': 'rain-black', '10n': 'rain-black', '11d': 'thunderstorm-black', '11n': 'thunderstorm-black', '13d': 'snow-black', '13n': 'snow-black', '50d': 'fog-black', '50n': 'fog-black', }; return `/wp-content/themes/stvnews/static/images/weather/${ map[iconCode] || 'cloudy-black' }.png`; } function renderWeather(data) { const current = data.hourly?.[0]; const location = localStorage.getItem('weather_favourites') && JSON.parse(localStorage.getItem('weather_favourites'))[0]?.display_name ?.split(',')[0]; if (!current || !location) return renderCTA(); el.innerHTML = `
${location} ${Math.round(current.temp)}°C
${Math.round(current.wind_speed * 2.237)}mph ${Math.round(current.pop * 100)}%
Sponsored by
TUI
`; } function renderCTA() { el.innerHTML = `
Weather where you are
Find your location
Sponsored by
TUI
`; } const weather = getCachedWeather(); weather ? renderWeather(weather) : renderCTA(); })();