Skip to content

Commit 56a7efc

Browse files
committed
Create script.js
1 parent 7168ecf commit 56a7efc

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

script.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
document.getElementById("generate-btn").addEventListener("click", generateUUIDs);
2+
document.getElementById("download-btn").addEventListener("click", downloadUUIDs);
3+
4+
async function generateUUIDs() {
5+
const count = document.getElementById("uuid-count").value;
6+
const version = document.getElementById("uuid-version").value;
7+
const apiUrl = `https://www.uuidtools.com/api/generate/${version}/count/${count}`;
8+
9+
if (count < 1 || count > 100) {
10+
alert("Please enter a number between 1 and 100.");
11+
return;
12+
}
13+
14+
try {
15+
const response = await fetch(apiUrl);
16+
if (!response.ok) throw new Error("Failed to fetch UUIDs.");
17+
const uuids = await response.json();
18+
19+
displayUUIDs(uuids);
20+
} catch (error) {
21+
alert("Error generating UUIDs. Please try again.");
22+
console.error(error);
23+
}
24+
}
25+
26+
function displayUUIDs(uuids) {
27+
const listContainer = document.getElementById("uuid-list");
28+
listContainer.innerHTML = "";
29+
uuids.forEach(uuid => {
30+
const div = document.createElement("div");
31+
div.textContent = uuid;
32+
listContainer.appendChild(div);
33+
});
34+
}
35+
36+
function downloadUUIDs() {
37+
const listContainer = document.getElementById("uuid-list");
38+
if (listContainer.children.length === 0) {
39+
alert("No UUIDs to download. Generate some first.");
40+
return;
41+
}
42+
43+
let textContent = "";
44+
listContainer.childNodes.forEach(node => {
45+
textContent += node.textContent + "\n";
46+
});
47+
48+
const blob = new Blob([textContent], {
49+
type: "text/plain"
50+
});
51+
const link = document.createElement("a");
52+
link.href = URL.createObjectURL(blob);
53+
link.download = "uuids.txt";
54+
link.click();
55+
}

0 commit comments

Comments
 (0)