1
0
Fork 0

fix prefers-color-scheme check

This commit is contained in:
nanxiaobei 2020-08-24 00:52:29 +08:00
parent 917eff9b63
commit c8204f337b

View file

@ -35,8 +35,12 @@
<!-- Script --> <!-- Script -->
<script> <script>
function setTheme() { function setTheme() {
const time = new Date(); if (window.matchMedia('(prefers-color-scheme: dark)').matches) {
document.body.classList.add('dark');
return;
}
const time = new Date();
const prev = localStorage.getItem('date'); const prev = localStorage.getItem('date');
const date = String(time.getMonth() + 1) + '.' + String(time.getDate()); const date = String(time.getMonth() + 1) + '.' + String(time.getDate());
@ -45,14 +49,14 @@
let sunset; let sunset;
function setBodyClass() { function setBodyClass() {
if (window.matchMedia("(prefers-color-scheme: dark)").matches || now > sunrise && now < sunset) return; if (now > sunrise && now < sunset) return;
document.body.classList.add('dark'); document.body.classList.add('dark');
} }
if (date !== prev) { if (date !== prev) {
fetch('https://api.ipgeolocation.io/astronomy?apiKey=5ed37d85103e4defa5df4c5298ed5215') fetch('https://api.ipgeolocation.io/astronomy?apiKey=5ed37d85103e4defa5df4c5298ed5215')
.then(res => res.json()) .then((res) => res.json())
.then(data => { .then((data) => {
sunrise = data.sunrise.split(':').map(Number); sunrise = data.sunrise.split(':').map(Number);
sunset = data.sunset.split(':').map(Number); sunset = data.sunset.split(':').map(Number);
}) })