-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththeme.js
37 lines (32 loc) · 994 Bytes
/
theme.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
window.onload = renderTheme;
function renderTheme() {
isDayTimeAsync().then(isDayTimeResult => {
if (isDayTimeResult)
setDayTheme();
else
setNightTheme();
});
}
function setDayTheme() {
document.body.className = "background-daily";
document.querySelector(".cursor").classList.add("code-daily");
}
function setNightTheme() {
document.body.className = "background-nightly";
document.querySelector(".cursor").classList.add("code-nightly");
}
async function isDayTimeAsync() {
var currentDate = new Date();
try {
const position = await getPositionAsync();
var times = SunCalc.getTimes(currentDate, position.coords.latitude, position.coords.longitude);
return currentDate > times.sunrise && currentDate < times.sunset;
} catch {
return 7 <= currentDate.getHours() && currentDate.getHours() < 20;
}
}
async function getPositionAsync() {
return new Promise((res, rej) => {
navigator.geolocation.getCurrentPosition(res, rej);
});
}