Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

merge #382

Closed
wants to merge 13 commits into from
64 changes: 64 additions & 0 deletions layouts/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,70 @@
{{- $posts := .Site.Params.home.posts -}}

<div class="page home{{ if ne $posts.enable false }} posts{{ end }}">
<div id="weather"></div>
<script type="text/javascript">
function getOpenMeteoURL(localData){
const url = 'https://api.open-meteo.com/v1/forecast';
const params = new URLSearchParams();
params.append('latitude', localData.lat);
params.append('longitude', localData.lon);
params.append('current', 'temperature_2m');
return `${url}?${params.toString()}`
}
function getOpenStreetMapURL(localData){
const url = 'https://nominatim.openstreetmap.org/reverse';
const params = new URLSearchParams();
params.append('format', 'json');
params.append('lat', localData.lat);
params.append('lon', localData.lon);
return `${url}?${params.toString()}`
}
function countryCodeEmoji(cc) {
const CC_REGEX = /^[a-z]{2}$/i;
const FLAG_LENGTH = 4;
const OFFSET = 127397;
if (!CC_REGEX.test(cc)) {
const type = typeof cc;
throw new TypeError(
`cc argument must be an ISO 3166-1 alpha-2 string, but got '${
type === 'string' ? cc : type
}' instead.`,
);
}
const codePoints = [...cc.toUpperCase()].map(c => c.codePointAt() + OFFSET);
return String.fromCodePoint(...codePoints);
}
async function handleApi(localData) {
var temperature_2m, city, state, country_code;
try {
// 使用 Promise.all 同时进行两个异步操作
const [weatherResponse, streetResponse] = await Promise.all([
fetch(getOpenMeteoURL(localData)),
fetch(getOpenStreetMapURL(localData))
]);
// 等待解决 Promise 对象并提取数据
const weatherData = await weatherResponse.json();
const streetData = await streetResponse.json();
// 提取所需的数据
temperature_2m = weatherData.current.temperature_2m;
city = streetData.address.city || "";
state = streetData.address.state || "";
country_code = streetData.address.country_code || undefined;
} catch (error) {
console.error("Error fetching data:", error);
}
if (country_code == undefined){
var country_flag = '🏳️';
}else{
var country_flag = countryCodeEmoji(country_code);
}
var visible = `实时气温:${temperature_2m}&deg;C ${city}/${state} ${country_flag}`;
const weatherElement = document.getElementById("weather");
weatherElement.innerHTML = visible;
}
</script>
<script type="application/javascript" src="https://request-cf.tenet.chat/?Callback=handleApi"></script>

{{- /* Profile */ -}}
{{- if ne $profile.enable false -}}
{{- partial "home/profile.html" . -}}
Expand Down
Loading