-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
63 lines (54 loc) · 2.14 KB
/
script.js
File metadata and controls
63 lines (54 loc) · 2.14 KB
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
async function fetchFolderContents(repo, branch, path) {
const apiUrl = `https://api.github.com/repos/${repo}/contents/${path}?ref=${branch}`;
const res = await fetch(apiUrl);
if (!res.ok) throw new Error(`Failed to fetch ${apiUrl}`);
return res.json();
}
async function downloadFolder(repo, branch, path) {
const JSZip = window.JSZip;
const zip = new JSZip();
async function addFilesToZip(folder, currentPath) {
for (const file of folder) {
if (file.type === 'file') {
const content = await fetch(file.download_url).then(r => r.blob());
zip.file(currentPath + file.name, content);
} else if (file.type === 'dir') {
const subFolder = await fetchFolderContents(repo, branch, file.path);
await addFilesToZip(subFolder, currentPath + file.name + '/');
}
}
}
const rootFolder = await fetchFolderContents(repo, branch, path);
await addFilesToZip(rootFolder, '');
const blob = await zip.generateAsync({ type: "blob" });
const a = document.createElement("a");
a.href = URL.createObjectURL(blob);
a.download = path.split('/').pop() + ".zip";
a.click();
}
function parseGitHubUrl(url) {
const match = url.match(/github\.com\/([^\/]+\/[^\/]+)\/tree\/([^\/]+)\/(.+)/);
if (!match) throw new Error("Invalid GitHub folder URL");
return {
repo: match[1],
branch: match[2],
path: match[3]
};
}
document.getElementById("downloadBtn").addEventListener("click", async () => {
const url = document.getElementById("folderUrl").value.trim();
const status = document.getElementById("status");
status.textContent = "⏳ Downloading...";
try {
const { repo, branch, path } = parseGitHubUrl(url);
await downloadFolder(repo, branch, path);
status.textContent = "✅ Download complete!";
} catch (e) {
console.error(e);
status.textContent = "❌ " + e.message;
}
});
// Load JSZip
const script = document.createElement("script");
script.src = "https://cdnjs.cloudflare.com/ajax/libs/jszip/3.10.1/jszip.min.js";
document.head.appendChild(script);