PageSourceSearch

https://www.xn--rgvt7o95i1ni.com/js/weather.js

js xn--rgvt7o95i1ni.com collected 2026-10-10 00:28:16 UTC 2,105 bytes, 55 lines download raw bytes

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.