const o={0:{label:"晴"},1:{label:"多云"},2:{label:"阴"},3:{label:"阴"},45:{label:"雾"},48:{label:"雾凇"},51:{label:"小毛毛雨"},53:{label:"毛毛雨"},55:{label:"大毛毛雨"},61:{label:"小雨"},63:{label:"中雨"},65:{label:"大雨"},71:{label:"小雪"},73:{label:"中雪"},75:{label:"大雪"},80:{label:"阵雨"},81:{label:"中阵雨"},82:{label:"大阵雨"},95:{label:"雷暴"},96:{label:"雷暴冰雹"},99:{label:"强雷暴冰雹"}};async function s(t,r){const n=`https://api.open-meteo.com/v1/forecast?latitude=${t}&longitude=${r}&current=temperature_2m,apparent_temperature,weather_code,wind_speed_10m&daily=temperature_2m_max,temperature_2m_min&timezone=auto&forecast_days=1`,e=await fetch(n);if(!e.ok)throw new Error(`Weather API ${e.status}`);const a=await e.json(),l=a.current?.weather_code??0;return{temperature:Math.round(a.current?.temperature_2m??0),apparentTemp:Math.round(a.current?.apparent_temperature??0),weatherCode:l,weatherLabel:o[l]?.label??"未知",windSpeed:Math.round(a.current?.wind_speed_10m??0),dailyMax:Math.round(a.daily?.temperature_2m_max?.[0]??0),dailyMin:Math.round(a.daily?.temperature_2m_min?.[0]??0)}}async function i(t,r){if(typeof chrome>"u"||!chrome.runtime?.sendMessage)return s(t,r);try{return await chrome.runtime.sendMessage({type:"weather",lat:t,lng:r})}catch{return null}}async function u(){const t=document.getElementById("weather-display");if(!t)return;let r=39.9042,n=116.4074;try{const e=await new Promise((a,l)=>{navigator.geolocation||l(new Error("no geolocation")),navigator.geolocation.getCurrentPosition(a,l,{timeout:3e3})});r=e.coords.latitude,n=e.coords.longitude}catch{}try{const e=await i(r,n);e?t.innerHTML=`<span>${e.weatherLabel}</span><strong>${e.temperature}°</strong><span style="font-size:12px;">${e.dailyMin}°~${e.dailyMax}°</span>`:t.innerHTML="<span>天气不可用</span>"}catch{t.innerHTML="<span>天气不可用</span>"}}u();
