From 1983d025a4ab805585b76cfba056809bd4fae614 Mon Sep 17 00:00:00 2001 From: Matt Walsh Date: Mon, 24 Mar 2025 22:52:32 -0500 Subject: [PATCH] only load custom.js if present --- server/scripts/custom.sample.js | 15 +++++++++++++-- server/scripts/index.mjs | 13 +++++++++++++ views/index.ejs | 7 +------ 3 files changed, 27 insertions(+), 8 deletions(-) diff --git a/server/scripts/custom.sample.js b/server/scripts/custom.sample.js index 50d0cf9..863a926 100644 --- a/server/scripts/custom.sample.js +++ b/server/scripts/custom.sample.js @@ -2,8 +2,7 @@ // it is intended to allow for customizations that do not get published back to the git repo // for example, changing the logo -// start running after all content is loaded -document.addEventListener('DOMContentLoaded', () => { +const customTask = () => { // get all of the logo images const logos = document.querySelectorAll('.logo img'); // loop through each logo @@ -11,4 +10,16 @@ document.addEventListener('DOMContentLoaded', () => { // change the source elem.src = 'my-custom-logo.gif'; }); +}; + +// start running after all content is loaded, or immediately if page content is already loaded +if (document.readyState === 'loading') { + // Loading hasn't finished yet + document.addEventListener('DOMContentLoaded', customTask); +} else { + // `DOMContentLoaded` has already fired + customTask(); +} +document.addEventListener('DOMContentLoaded', () => { + }); diff --git a/server/scripts/index.mjs b/server/scripts/index.mjs index 0376b47..a0413c1 100644 --- a/server/scripts/index.mjs +++ b/server/scripts/index.mjs @@ -9,6 +9,7 @@ import settings from './modules/settings.mjs'; document.addEventListener('DOMContentLoaded', () => { init(); + getCustomCode(); }); const categories = [ @@ -413,3 +414,15 @@ const fullScreenResizeCheck = () => { // store state of fullscreen element for next change detection fullScreenResizeCheck.wasFull = !!document.fullscreenElement; }; + +const getCustomCode = async () => { + // fetch the custom file and see if it returns a 200 status + const response = await fetch('scripts/custom.js', { method: 'HEAD' }); + if (response.ok) { + // add the script element to the page + const customElem = document.createElement('script'); + customElem.src = 'scripts/custom.js'; + customElem.type = 'text/javascript'; + document.body.append(customElem); + } +}; diff --git a/views/index.ejs b/views/index.ejs index 3f6935b..80bc2f1 100644 --- a/views/index.ejs +++ b/views/index.ejs @@ -1,4 +1,4 @@ - + @@ -20,7 +20,6 @@ - <% } else { %> @@ -45,14 +44,10 @@ - - - - <% } %>