website/assets/js/page.js

51 lines
1.5 KiB
JavaScript

import Cookies from 'js-cookie';
document.addEventListener("readystatechange", event => {
if(event.target.readyState === 'complete') {
initOverflow();
initExternalLinks();
initNinetiesToggle();
}
});
function initOverflow() {
let body = document.querySelector('body');
if (body.scrollHeight > window.outerHeight) {
body.style.height = 'auto';
}
}
function initNinetiesToggle() {
let nineties = document.querySelector('.nineties-toggle');
let container = document.querySelector('.container');
nineties.addEventListener('click', function() {
let body = document.querySelector('body');
if (body.classList.contains('nineties')) {
body.classList.remove('nineties');
Cookies.remove('nineties');
} else {
body.classList.add('nineties');
Cookies.set('nineties', true, { sameSite: 'strict' });
}
})
if(
nineties.getBoundingClientRect().left < container.getBoundingClientRect().right &&
nineties.getBoundingClientRect().bottom > container.getBoundingClientRect().top
) {
nineties.style.position = 'initial';
nineties.style.textAlign = 'right';
}
}
function initExternalLinks() {
document.querySelectorAll('a').forEach(elem => {
if(isExternalURL(elem.getAttribute('href'))) {
elem.setAttribute('target', '_blank');
}
})
}
const isExternalURL = (url) => new URL(url).origin !== location.origin;