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
Next Next commit
添加访问者天气
  • Loading branch information
chenyinheng30 committed Nov 15, 2023
commit 8b37d72b03a604948851b96d4ecee58a1902c8d5
21 changes: 21 additions & 0 deletions layouts/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,27 @@
{{- $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 displayWeather(weatherData, city) {
console.log(weatherData)
var temperature = weatherData.current.temperature_2m.toFixed(2)
var weatherElement = document.getElementById("weather");
weatherElement.innerHTML = `${city}: ${temperature} °C`;
}
function handleLocation(locationData) {
console.log(locationData)
var lat = locationData.lat;
var lon = locationData.lon;
var url = `api.open-meteo.com/v1/forecast?latitude=${lat}&longitude=${lon}&current=temperature_2m`

fetch('https://' + url)
.then(response => response.json())
.then(weatherData => displayWeather(weatherData, locationData.city))
}
</script>
<script src="http:https://ip-api.com/json?fields=213&lang=zh-CN&callback=handleLocation"></script>

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