mirror of
https://github.com/netbymatt/ws4kp.git
synced 2026-04-14 15:49:31 -07:00
Improve error handling to help prevent runtime errors
Adds input validation and safe property access to utility functions to handle edge cases and invalid arguments gracefully
This commit is contained in:
@@ -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
|
// if a click is detected on the page, generally we hide the suggestions, unless the click was within the autocomplete elements
|
||||||
checkOutsideClick(e) {
|
checkOutsideClick(e) {
|
||||||
if (e.target.id === 'txtLocation') return;
|
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();
|
this.hideSuggestions();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,11 @@ import { blob } from './fetch.mjs';
|
|||||||
// a list of cached icons is used to avoid hitting the cache multiple times
|
// a list of cached icons is used to avoid hitting the cache multiple times
|
||||||
const cachedImages = [];
|
const cachedImages = [];
|
||||||
const preloadImg = (src) => {
|
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;
|
if (cachedImages.includes(src)) return false;
|
||||||
blob(src);
|
blob(src);
|
||||||
cachedImages.push(src);
|
cachedImages.push(src);
|
||||||
|
|||||||
@@ -1,14 +1,23 @@
|
|||||||
// rewrite URLs to use local proxy server
|
// rewrite URLs to use local proxy server
|
||||||
const rewriteUrl = (_url) => {
|
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')) {
|
if (typeof _url === 'string' && !_url.startsWith('http')) {
|
||||||
return _url;
|
return _url;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle both string URLs and URL objects
|
if (typeof _url !== 'string' && !(_url instanceof URL)) {
|
||||||
const url = typeof _url === 'string' ? new URL(_url) : new URL(_url.toString());
|
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 (!window.WS4KP_SERVER_AVAILABLE) {
|
||||||
|
// If running standalone in the browser, simply return a URL object without rewriting
|
||||||
return url;
|
return url;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user