-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
default.js
89 lines (69 loc) · 2.21 KB
/
default.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
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
class DarkModeSwitch {
constructor() {
this.checkboxElem = document.querySelector("#dark-mode-switch > input[type=\"checkbox\"]");
if (this.isEnabled() || this.darkModePreferred()) {
// turning the switch off == dark mode
this.checkboxElem.checked = false;
document.body.classList.add("dark-mode");
}
this.checkboxElem.addEventListener("change", this.toggle.bind(this));
}
isEnabled() { return localStorage.getItem("dark-mode") != null; }
darkModePreferred() {
return window.matchMedia('(prefers-color-scheme: dark)').matches;
}
disable() {
localStorage.removeItem("dark-mode");
document.body.classList.remove("dark-mode");
}
enable() {
localStorage.setItem("dark-mode",true);
document.body.classList.add("dark-mode");
}
toggle(setting) {
if (this.isEnabled()) { this.disable(); }
else { this.enable(); }
}
}
class Digits {
static chars = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
static random() {
return Digits.chars[Math.floor(Math.random() * Digits.chars.length)];
}
}
class HeaderTitle {
constructor() {
this.textElem = document.querySelector("header svg text > tspan");
this.originalValue = this.textElem.textContent;
this.intervalID = null;
this.textElem.addEventListener("mouseenter", this.startRandomizer.bind(this));
this.textElem.addEventListener("mouseleave", this.stopRandomizer.bind(this));
}
set(newTitle) { this.textElem.textContent = newTitle; }
startRandomizer() {
if (this.intervalID == null) {
this.intervalID = setInterval(this.randomize.bind(this), 60);
}
}
randomize() {
this.set(
`P ${Digits.random()} S ${Digits.random()} M ${Digits.random()} D ${Digits.random()} R ${Digits.random()}`
);
}
stopRandomizer() {
if (this.intervalID != null) {
clearInterval(this.intervalID);
this.intervalID = null;
this.reset();
}
}
reset() { this.set("P O S T M O D E R N"); }
}
const ready = (callback) => {
if (document.readyState != "loading") callback();
else document.addEventListener("DOMContentLoaded", callback);
}
ready(() => {
const title = new HeaderTitle();
const darkMode = new DarkModeSwitch();
});