diff --git a/server/scripts/modules/autocomplete.mjs b/server/scripts/modules/autocomplete.mjs index 8a23f96..dd51176 100644 --- a/server/scripts/modules/autocomplete.mjs +++ b/server/scripts/modules/autocomplete.mjs @@ -297,7 +297,7 @@ class AutoComplete { // if a click is detected on the page, generally we hide the suggestions, unless the click was within the autocomplete elements checkOutsideClick(e) { if (e.target.id === 'txtLocation') return; - if (e.target?.parentNode?.classList.contains(this.options.containerClass)) return; + if (e.target?.parentNode?.classList?.contains(this.options.containerClass)) return; this.hideSuggestions(); } } diff --git a/server/scripts/modules/utils/image.mjs b/server/scripts/modules/utils/image.mjs index ac6c48a..d70271a 100644 --- a/server/scripts/modules/utils/image.mjs +++ b/server/scripts/modules/utils/image.mjs @@ -5,6 +5,11 @@ import { blob } from './fetch.mjs'; // a list of cached icons is used to avoid hitting the cache multiple times const cachedImages = []; const preloadImg = (src) => { + if (!src || typeof src !== 'string') { + console.warn(`preloadImg expects a URL string, received: '${src}' (${typeof src})`); + return false; + } + if (cachedImages.includes(src)) return false; blob(src); cachedImages.push(src); diff --git a/server/scripts/modules/utils/url-rewrite.mjs b/server/scripts/modules/utils/url-rewrite.mjs index 1661add..08e4ad5 100644 --- a/server/scripts/modules/utils/url-rewrite.mjs +++ b/server/scripts/modules/utils/url-rewrite.mjs @@ -1,14 +1,23 @@ // rewrite URLs to use local proxy server const rewriteUrl = (_url) => { - // Handle relative URLs - return them as-is since they don't need rewriting + if (!_url) { + throw new Error(`rewriteUrl called with invalid argument: '${_url}' (${typeof _url})`); + } + + // Handle relative URLs early: return them as-is since they don't need rewriting if (typeof _url === 'string' && !_url.startsWith('http')) { return _url; } - // Handle both string URLs and URL objects - const url = typeof _url === 'string' ? new URL(_url) : new URL(_url.toString()); + if (typeof _url !== 'string' && !(_url instanceof URL)) { + throw new Error(`rewriteUrl expects a URL string or URL object, received: ${typeof _url}`); + } + + // Convert to URL object (for URL objects, creates a copy to avoid mutating the original) + const url = new URL(_url); if (!window.WS4KP_SERVER_AVAILABLE) { + // If running standalone in the browser, simply return a URL object without rewriting return url; }