-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
101 lines (84 loc) · 2.99 KB
/
script.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
document.addEventListener("DOMContentLoaded", () => {
const fileInput = document.getElementById("fileInput");
const fileInfo = document.getElementById("fileInfo");
const downloadList = document.getElementById("downloadList");
const fileLabel = document.querySelector(".file-label");
function formatFileSize(bytes) {
if (bytes === 0) return "0 B";
const k = 1024;
const sizes = ["B", "KB", "MB", "GB"];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return `${(bytes / Math.pow(k, i)).toFixed(1)} ${sizes[i]}`;
}
function handleFiles(files) {
if (!files || files.length === 0) return;
// Clear previous downloads
downloadList.innerHTML = "";
// Start downloads
Array.from(files).forEach((file) => {
const blobUrl = URL.createObjectURL(file);
const link = document.createElement("a");
link.href = blobUrl;
link.download = file.name;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
URL.revokeObjectURL(blobUrl);
});
// Display summary
const totalSize = Array.from(files).reduce(
(acc, file) => acc + file.size,
0
);
fileInfo.textContent = `Downloaded ${files.length} file${
files.length === 1 ? "" : "s"
} (${formatFileSize(totalSize)} total)`;
// Update UI for each file
Array.from(files).forEach((file) => {
const downloadItem = document.createElement("div");
downloadItem.className = "download-item";
const nameSpan = document.createElement("span");
nameSpan.className = "download-item-name";
nameSpan.textContent = file.name;
const sizeSpan = document.createElement("span");
sizeSpan.className = "download-item-size";
sizeSpan.textContent = formatFileSize(file.size);
downloadItem.appendChild(nameSpan);
downloadItem.appendChild(sizeSpan);
downloadList.insertBefore(downloadItem, downloadList.firstChild);
});
fileInput.value = ""; // Reset file input
}
function preventDefaults(e) {
e.preventDefault();
e.stopPropagation();
}
// Handle file input change
fileInput.addEventListener("change", (e) => {
handleFiles(e.target.files);
});
// Make file input keyboard accessible
fileLabel.addEventListener("keydown", (e) => {
if (e.key === "Enter" || e.key === " ") {
e.preventDefault();
fileInput.click();
}
});
// Prevent default drag behaviors
["dragenter", "dragover", "dragleave", "drop"].forEach((eventName) => {
document.body.addEventListener(eventName, preventDefaults, false);
});
// Handle drag and drop
document.body.addEventListener("dragenter", () => {
document.body.classList.add("dragging");
});
document.body.addEventListener("dragleave", (e) => {
if (e.target === document.body) {
document.body.classList.remove("dragging");
}
});
document.body.addEventListener("drop", (e) => {
document.body.classList.remove("dragging");
handleFiles(e.dataTransfer.files);
});
});