lynnesbian.space/assets/script.js

74 lines
1.9 KiB
JavaScript
Raw Normal View History

// @license magnet:?xt=urn:btih:5305d91886084f776adcf57509a648432709a7c7&dn=x11.txt X11 License
2020-03-16 15:15:43 +00:00
var use_local_storage = storage_check();
var html_element = null;
2020-03-16 15:15:43 +00:00
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];
2020-03-16 15:15:43 +00:00
update_page_theme();
2020-03-16 15:15:43 +00:00
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');
2020-03-16 15:15:43 +00:00
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');
};
dgel('show-more').onclick = function () {
this.parentElement.classList.toggle('show');
}
2020-03-16 15:15:43 +00:00
});
// @license-end