export function escapeHtml(value) {
	return String(value).replace(/[&<>"']/g, function (c) {
		return { "&": "&amp;", "<": "&lt;", ">": "&gt;", '"': "&quot;", "'": "&#39;" }[c];
	});
}

export function whenReady(scriptId, globalName, callback) {
	const ready = () => typeof globalThis[globalName] !== "undefined";
	if (ready()) {
		callback();
		return;
	}
	const script = document.getElementById(scriptId);
	if (!script) {
		callback();
		return;
	}
	const run = () => {
		if (ready()) callback();
	};
	script.addEventListener("load", () => {
		script.setAttribute("data-loaded", "1");
		run();
	});
	if (script.getAttribute("data-loaded")) run();
}