function initUuidGenerator() { const statusEl = document.getElementById("uuidStatus"); const countEl = document.getElementById("uuidCount"); const uppercaseEl = document.getElementById("uuidUppercase"); const noHyphenEl = document.getElementById("uuidNoHyphen"); const outputEl = document.getElementById("uuidOutput"); const metaEl = document.getElementById("uuidMeta"); const generateBtn = document.getElementById("uuidGenerateBtn"); const copyBtn = document.getElementById("uuidCopyBtn"); const downloadBtn = document.getElementById("uuidDownloadBtn"); const clearBtn = document.getElementById("uuidClearBtn"); if (!outputEl || !generateBtn) return; function setStatus(text, isError) { if (!statusEl) return; statusEl.textContent = text; statusEl.classList.toggle("status-error", !!isError); statusEl.classList.toggle("status-success", !isError); } function updateUiState() { const rows = outputEl.value.trim() ? outputEl.value.trim().split("\n") : []; if (metaEl) metaEl.textContent = `Count: ${rows.length}`; if (copyBtn) copyBtn.disabled = rows.length === 0; if (downloadBtn) downloadBtn.disabled = rows.length === 0; if (clearBtn) clearBtn.disabled = rows.length === 0; } function createUuid() { if (window.crypto && typeof window.crypto.randomUUID === "function") { return window.crypto.randomUUID(); } // Fallback for older browsers. return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (char) => { const rand = Math.random() * 16 | 0; const value = char === "x" ? rand : (rand & 0x3 | 0x8); return value.toString(16); }); } function applyFormat(uuid) { let value = uuid; if (noHyphenEl && noHyphenEl.checked) value = value.replace(/-/g, ""); if (uppercaseEl && uppercaseEl.checked) value = value.toUpperCase(); return value; } function generateAction() { const rawCount = Number(countEl?.value || 1); const count = Math.min(1000, Math.max(1, Number.isFinite(rawCount) ? rawCount : 1)); if (countEl) countEl.value = String(count); const items = []; for (let i = 0; i < count; i += 1) { items.push(applyFormat(createUuid())); } outputEl.value = items.join("\n"); setStatus(`Generated ${count} UUID${count > 1 ? "s" : ""}.`); updateUiState(); } async function copyAction() { if (!outputEl.value.trim()) return; await navigator.clipboard.writeText(outputEl.value); const label = copyBtn.textContent; copyBtn.textContent = "Copied"; setStatus("UUID list copied."); setTimeout(() => { copyBtn.textContent = label; }, 1200); } function downloadAction() { if (!outputEl.value.trim()) return; const blob = new Blob([outputEl.value], { type: "text/plain;charset=utf-8" }); const link = document.createElement("a"); link.href = URL.createObjectURL(blob); link.download = "uuid-list.txt"; link.click(); URL.revokeObjectURL(link.href); setStatus("UUID list downloaded."); } function clearAction() { outputEl.value = ""; setStatus("Configure options and generate UUIDs."); updateUiState(); } generateBtn.addEventListener("click", generateAction); if (copyBtn) copyBtn.addEventListener("click", copyAction); if (downloadBtn) downloadBtn.addEventListener("click", downloadAction); if (clearBtn) clearBtn.addEventListener("click", clearAction); updateUiState(); } if (document.readyState === "loading") { document.addEventListener("DOMContentLoaded", initUuidGenerator); } else { initUuidGenerator(); }