69 lines
1.7 KiB
JavaScript
69 lines
1.7 KiB
JavaScript
var use_local_storage = storage_check();
|
|
var html_element = null;
|
|
|
|
|
|
function dgel(id) {
|
|
return document.getElementById(id);
|
|
}
|
|
|
|
function storage_check() {
|
|
// based on https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API/Using_the_Web_Storage_API
|
|
try {
|
|
let x = 'test';
|
|
localStorage.setItem(x, x);
|
|
localStorage.removeItem(x);
|
|
return true;
|
|
}
|
|
catch (e) {
|
|
// fall back to cookies
|
|
// i guess this might break if someone has cookies and localstorage disabled but people with first party cookies blocked are used to things breaking anyway ;)
|
|
return false;
|
|
}
|
|
}
|
|
|
|
function theme_get() {
|
|
// true if night theme
|
|
if (use_local_storage) {
|
|
// weakly typed languages. not even once.
|
|
return localStorage.getItem('theme') == 'true';
|
|
} else {
|
|
return document.cookie == 'theme=true';
|
|
}
|
|
}
|
|
|
|
function theme_set(night) {
|
|
if (use_local_storage) {
|
|
localStorage.setItem('theme', night);
|
|
} else {
|
|
document.cookie = `theme=${night}`;
|
|
}
|
|
}
|
|
|
|
function update_page_theme() {
|
|
let night = theme_get();
|
|
|
|
if (night) {
|
|
html_element.classList.add('night');
|
|
} else {
|
|
html_element.classList.remove('night');
|
|
}
|
|
}
|
|
|
|
window.addEventListener('DOMContentLoaded', (event) => {
|
|
html_element = document.getElementsByTagName('html')[0];
|
|
update_page_theme();
|
|
|
|
dgel('theme-control').onclick = function () {
|
|
// only add the 'ready' class upon clicking the button
|
|
// this avoids the page transitioning every time you change the page with night theme on
|
|
html_element.classList.add('ready');
|
|
theme_set(!theme_get());
|
|
update_page_theme();
|
|
};
|
|
|
|
dgel('hamburger').onclick = function () {
|
|
this.classList.toggle('active');
|
|
dgel('header').classList.toggle('active');
|
|
dgel('main').classList.toggle('active');
|
|
}
|
|
});
|