import stringToHTML from "../stringToHTML";
function isSafeShortcutHref(url: string): boolean {
if (typeof url !== "string" || !url.trim()) return false;
try {
const parsed = new URL(url, window.location.href);
return ["http:", "https:", "mailto:"].includes(parsed.protocol);
} catch {
return false;
}
}
export function CreateCustomShortcutDiv(element: any) {
// Creates the stucture and element information for each seperate shortcut
const container = document.getElementById("shortcuts");
if (!container) return;
var shortcut = document.createElement("a");
if (isSafeShortcutHref(element.url)) {
shortcut.setAttribute("href", element.url);
shortcut.setAttribute("target", "_blank");
} else {
shortcut.setAttribute("href", "#");
shortcut.setAttribute("aria-disabled", "true");
}
var shortcutdiv = document.createElement("div");
shortcutdiv.classList.add("shortcut");
shortcutdiv.classList.add("customshortcut");
let image: ChildNode | null = null;
if (typeof element.icon === "string" && element.icon.trim().startsWith("<")) {
image = stringToHTML(element.icon).firstChild;
} else if (typeof element.icon === "string" && element.icon.startsWith("data:image")) {
const img = document.createElement("img");
img.src = element.icon;
img.style.width = "39px";
img.style.height = "39px";
image = img;
} else {
image = stringToHTML(
/* html */`
`,
).firstChild;
}
(image as HTMLElement).classList.add("shortcuticondiv");
var text = document.createElement("p");
text.textContent = element.name;
shortcutdiv.append(image!);
shortcutdiv.append(text);
shortcut.append(shortcutdiv);
container.append(shortcut);
}