1import { APP_CONFIG } from "../config.js"; 2import { readCacheEntry, writeCache } from "./storage.js"; 3 4const WEATHER_CACHE_KEY = "homepage-cf-weather-v2"; 5 6function weatherCodeText(code) { 7 const map = new Map([ 8 [0, "æ´"], [1, "å¤§è´æ´æ"], [2, "å¤äº"], [3, "é´"], 9 [45, "é¾"], [48, "é¾å"], [51, "å°æ¯æ¯é¨"], [53, "æ¯æ¯é¨"], [55, "å¼ºæ¯æ¯é¨"], 10 [61, "å°é¨"], [63, "ä¸é¨"], [65, "大é¨"], [71, "å°éª"], [73, "ä¸éª"], [75, "大éª"], 11 [80, "éµé¨"], [81, "强éµé¨"], [82, "æ´é¨"], [95, "é·æ´"], [96, "é·æ´ä¼´å°é¹"], [99, "å¼ºé·æ´"], 12 ]); 13 return map.get(code) || "天æ°"; 14} 15 16async function updateWeather() { 17 const weather = APP_CONFIG.weather; 18 const maxAgeMs = weather.cacheMinutes * 60_000; 19 const cached = readCacheEntry(WEATHER_CACHE_KEY, maxAgeMs); 20 21 const render = (payload, { stale = false } = {}) => { 22 const current = payload?.current || {}; 23 const temp = Number.isFinite(current.temperature_2m) ? `${Math.round(current.temperature_2m)}°C` : "--"; 24 document.getElementById("weather-value").textContent = `${temp} · ${weatherCodeText(current.weather_code)}`; 25 26 const apparent = Number.isFinite(current.apparent_temperature) 27 ? `${Math.round(current.apparent_temperature)}°C` 28 : "--"; 29 const locationLabel = payload?.location?.label || weather.label; 30 document.getElementById("weather-extra").textContent = 31 `${locationLabel} · 使 ${apparent}${stale ? " · ç¼å" : ""}`; 32 }; 33 34 if (cached) { 35 render(cached.value, { stale: cached.isStale }); 36 if (!cached.isStale) return; 37 } 38 39 try { 40 const response = await fetch("/api/weather", { 41 headers: { Accept: "application/json" }, 42 }); 43 if (!response.ok) throw new Error(`HTTP ${response.status}`); 44 45 const payload = await response.json(); 46 writeCache(WEATHER_CACHE_KEY, payload); 47 render(payload); 48 } catch { 49 if (!cached?.value) { 50 document.getElementById("weather-value").textContent = "--"; 51 document.getElementById("weather-extra").textContent = weather.label; 52 } 53 } 54} 55export { updateWeather };
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.