-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
47 lines (43 loc) · 1.38 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/**
* AgnosticRun 0.2.12
*
* A utility function that safely runs a given function
* on a DOM element if the element exists. It logs or warns
* based on customizable debug flags set globally.
*
* Developed by Claudio González (claudio@banshee.pro)
* for Banshee Technologies - https://www.banshee.pro
*
* MIT License
* Copyright (c) 2024 Banshee Technologies. All rights reserved.
*/
let globalDebugLog = false;
let globalDebugWarn = false;
export function agnosticRun({ debugLog = false, debugWarn = false } = {}) {
globalDebugLog = debugLog;
globalDebugWarn = debugWarn;
return function (fn) {
return function (elementId, ...args) {
if (typeof elementId !== "string" || !elementId.trim()) {
if (globalDebugWarn) {
console.error(`AgnosticRun: Invalid elementId "${elementId}". It must be a non-empty string.`);
}
return;
}
const element = document.getElementById(elementId);
if (element) {
if (!element.dataset.checked) {
if (globalDebugLog) {
console.log(`AgnosticRun: "${elementId}" exists. Safe to run.`);
}
element.dataset.checked = "true";
}
return fn(element, ...args);
} else {
if (globalDebugWarn) {
console.warn(`AgnosticRun: Element with ID "${elementId}" does not exist, have you removed it?`);
}
}
};
};
}