Jeroen De Meerleer
3305c26750
- Refactored the `initEasterEgg` function to fetch easter egg strings from a JSON file instead of hardcoding them. - Removed the unused `eastereggstrings` array. - Updated event listener to use arrow function syntax (`evt`) instead of `event`. - Added error handling for failed fetch requests.
56 lines
1.8 KiB
JavaScript
56 lines
1.8 KiB
JavaScript
import '/assets/scss/page.scss';
|
|
|
|
let eastereggsurl = '/assets/js/eastereggstrings.json'
|
|
document.addEventListener("readystatechange", event => {
|
|
if(event.target.readyState === 'complete') {
|
|
initExternalLinks();
|
|
initEasterEgg();
|
|
}
|
|
});
|
|
|
|
|
|
function initExternalLinks() {
|
|
document.querySelectorAll('a').forEach(elem => {
|
|
if(isExternalURL(elem.getAttribute('href'))) {
|
|
elem.setAttribute('target', '_blank');
|
|
}
|
|
})
|
|
}
|
|
|
|
function initEasterEgg() {
|
|
document.body.addEventListener('click', evt => {
|
|
fetch(eastereggsurl)
|
|
.then((response) => {
|
|
if (!response.ok) {
|
|
return;
|
|
}
|
|
return response.json();
|
|
}).then((eastereggstrings) => {
|
|
let randomstring = Math.floor(Math.random() * eastereggstrings.length);
|
|
document.querySelector('.easter-egg').innerHTML = eastereggstrings[randomstring];
|
|
})
|
|
});
|
|
}
|
|
|
|
function isInteractiveElement(element) {
|
|
// Check if the element is an interactive element or triggers an event
|
|
return (
|
|
element.tagName === 'A' ||
|
|
element.tagName === 'BUTTON' ||
|
|
element.tagName === 'INPUT' ||
|
|
element.tagName === 'SELECT' ||
|
|
element.tagName === 'TEXTAREA' ||
|
|
element.getAttribute('onclick') !== null ||
|
|
element.getAttribute('onmousedown') !== null ||
|
|
element.getAttribute('onmouseup') !== null ||
|
|
element.getAttribute('onmouseover') !== null ||
|
|
element.getAttribute('onmouseout') !== null ||
|
|
element.getAttribute('onmousemove') !== null ||
|
|
element.getAttribute('oncontextmenu') !== null
|
|
);
|
|
}
|
|
|
|
const isExternalURL = (url) => {
|
|
if(url.startsWith('/')) return false;
|
|
return new URL(url).origin !== location.origin;
|
|
} |