-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
101 lines (101 loc) · 3.04 KB
/
Copy pathscript.js
File metadata and controls
101 lines (101 loc) · 3.04 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
var darkmodeactive = localStorage.getItem("darkmode");
console.log("Dark mode is: " + darkmodeactive);
function labelDark() {
$(".toggle-switch").attr("alt", "Go light");
$(".toggle-switch").attr("title", "Go light");
}
function goDark() {
console.log("Dark mode is active");
labelDark();
$("body").addClass("dark");
refreshFavicon();
}
function stayDark() {
goDark();
localStorage.setItem("darkmode", true);
darkmodeactive = localStorage.getItem("darkmode");
console.log("Dark mode is: " + darkmodeactive + " and it will stay dark");
}
function labelLight() {
$(".toggle-switch").attr("alt", "Go dark");
$(".toggle-switch").attr("title", "Go dark");
}
function goLight() {
console.log("Light mode is active");
labelLight();
$("body").removeClass("dark");
refreshFavicon();
}
function stayLight() {
goLight();
localStorage.setItem("darkmode", false);
darkmodeactive = localStorage.getItem("darkmode");
console.log("Dark mode is: " + darkmodeactive + " and it will stay light");
}
window.matchMedia("(prefers-color-scheme: dark)").addListener(e => e.matches && stayDark());
window.matchMedia("(prefers-color-scheme: light)").addListener(e => e.matches && stayLight());
$(".toggle-switch").click(function() {
if ($("body").hasClass("dark")) {
stayLight();
} else {
stayDark();
}
});
$(".label-light").click(function() {
if ($("body").hasClass("dark")) {
stayLight();
}
});
$(".label-dark").click(function() {
if (!$("body").hasClass("dark")) {
stayDark();
}
});
window.onload=function() {
if (localStorage.darkmode=="true") {
console.log("User manually selected dark mode from a past session");
goDark();
} else if (localStorage.darkmode=="false") {
console.log("User manually selected light mode from a past session");
goLight();
} else {
console.log("User hasn't selected dark or light mode from a past session, dark mode has been served by default and OS-level changes will automatically reflect");
if ($("body").hasClass("dark")) {
labelDark();
} else {
labelLight();
}
}
};
function tempDisableAnim() {
$("*").addClass("disableEasingTemporarily");
setTimeout(function() {
$("*").removeClass("disableEasingTemporarily");
}, 20);
}
setTimeout(function() {
$(".load-flash").css("display","none");
$(".load-flash").css("visibility","hidden");
tempDisableAnim();
}, 20);
$(window).resize(function() {
tempDisableAnim();
setTimeout(function() {
tempDisableAnim();
}, 0);
});
function refreshFavicon() {
if (matchMedia('(prefers-color-scheme: dark)').matches) {
var link = document.querySelector("link[rel*='icon']") || document.createElement('link');
link.type = 'image/x-icon';
link.rel = 'icon';
link.href = 'favicon.ico';
document.getElementsByTagName('head')[0].appendChild(link);
} else {
var link = document.querySelector("link[rel*='icon']") || document.createElement('link');
link.type = 'image/x-icon';
link.rel = 'icon';
link.href = 'favicon.svg';
document.getElementsByTagName('head')[0].appendChild(link);
}
}