-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
767 lines (657 loc) · 33.4 KB
/
Copy pathindex.html
File metadata and controls
767 lines (657 loc) · 33.4 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
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
<!DOCTYPE html>
<html lang="en" class="dark">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>GitHub Directory Downloader</title>
<!-- Tailwind CSS -->
<script src="https://cdn.tailwindcss.com"></script>
<!-- Lucide Icons -->
<script src="https://unpkg.com/lucide@latest"></script>
<!-- JSZip for zipping files -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.10.1/jszip.min.js"></script>
<style>
/* Base Dark Theme Body */
body {
background-color: #020617; /* slate-950 */
color: #f8fafc; /* slate-50 */
font-family: ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}
/* Glassmorphism Utilities */
.glass-panel {
background: rgba(30, 41, 59, 0.45); /* slate-800 with opacity */
backdrop-filter: blur(16px);
-webkit-backdrop-filter: blur(16px);
border: 1px solid rgba(255, 255, 255, 0.1);
box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.5);
}
.glass-input {
background: rgba(15, 23, 42, 0.5); /* slate-900 with opacity */
border: 1px solid rgba(255, 255, 255, 0.15);
color: #f8fafc;
transition: all 0.3s ease;
}
.glass-input:focus {
background: rgba(15, 23, 42, 0.8);
border-color: #3b82f6; /* blue-500 */
box-shadow: 0 0 0 4px rgba(59, 130, 246, 0.15);
outline: none;
}
.glass-button {
background: linear-gradient(135deg, rgba(37, 99, 235, 0.9) 0%, rgba(29, 78, 216, 0.9) 100%);
border: 1px solid rgba(255, 255, 255, 0.2);
transition: all 0.2s ease;
box-shadow: 0 4px 15px rgba(37, 99, 235, 0.3);
}
.glass-button:hover:not(:disabled) {
background: linear-gradient(135deg, rgba(59, 130, 246, 0.9) 0%, rgba(37, 99, 235, 0.9) 100%);
transform: translateY(-1px);
box-shadow: 0 6px 20px rgba(37, 99, 235, 0.4);
}
.glass-button:active:not(:disabled) {
transform: translateY(1px);
}
.glass-button:disabled {
background: rgba(51, 65, 85, 0.5); /* slate-700 */
color: rgba(148, 163, 184, 0.7); /* slate-400 */
cursor: not-allowed;
border-color: rgba(255, 255, 255, 0.05);
box-shadow: none;
}
/* Animated Background Orbs */
.orb {
position: absolute;
border-radius: 50%;
filter: blur(80px);
z-index: -1;
animation: float 10s infinite ease-in-out alternate;
}
.orb-1 {
width: 400px;
height: 400px;
background: rgba(56, 189, 248, 0.3); /* sky-400 */
top: -100px;
left: -100px;
}
.orb-2 {
width: 500px;
height: 500px;
background: rgba(99, 102, 241, 0.2); /* indigo-500 */
bottom: -150px;
right: -100px;
animation-delay: -5s;
}
@keyframes float {
0% { transform: translate(0, 0) scale(1); }
100% { transform: translate(30px, 50px) scale(1.1); }
}
/* Custom Scrollbar */
::-webkit-scrollbar {
width: 8px;
}
::-webkit-scrollbar-track {
background: transparent;
}
::-webkit-scrollbar-thumb {
background: rgba(255, 255, 255, 0.2);
border-radius: 4px;
}
::-webkit-scrollbar-thumb:hover {
background: rgba(255, 255, 255, 0.3);
}
/* Utility */
.hidden { display: none !important; }
/* Custom Tree Checkbox */
.glass-checkbox {
appearance: none;
width: 1.125rem;
height: 1.125rem;
border: 1px solid rgba(255,255,255,0.2);
border-radius: 0.25rem;
background: rgba(15, 23, 42, 0.5);
outline: none;
cursor: pointer;
position: relative;
transition: all 0.2s;
flex-shrink: 0;
}
.glass-checkbox:checked {
background: #3b82f6; /* blue-500 */
border-color: #3b82f6;
}
.glass-checkbox:checked::after {
content: '';
position: absolute;
top: 2px; left: 6px;
width: 4px; height: 9px;
border: solid white;
border-width: 0 2px 2px 0;
transform: rotate(45deg);
}
.glass-checkbox:indeterminate {
background: rgba(59, 130, 246, 0.5);
border-color: #3b82f6;
}
.glass-checkbox:indeterminate::after {
content: '';
position: absolute;
top: 7px; left: 3px;
width: 10px; height: 2px;
background: white;
border-radius: 1px;
}
</style>
</head>
<body class="min-h-screen flex flex-col items-center justify-center p-4 relative overflow-hidden">
<!-- Ambient Background Effects -->
<div class="orb orb-1"></div>
<div class="orb orb-2"></div>
<!-- Main Container -->
<div class="glass-panel w-full max-w-2xl rounded-3xl overflow-hidden relative z-10">
<!-- Header -->
<div class="px-8 py-10 text-center relative border-b border-white/10 bg-slate-900/20">
<div class="flex flex-col items-center">
<div class="bg-blue-500/20 p-4 rounded-2xl mb-5 ring-1 ring-blue-500/30 backdrop-blur-md shadow-[0_0_30px_rgba(59,130,246,0.3)]">
<i data-lucide="folder-down" class="w-10 h-10 text-blue-400"></i>
</div>
<h1 class="text-3xl font-bold text-white mb-3 tracking-tight">GitHub Directory Downloader</h1>
<p class="text-slate-400 text-sm max-w-md leading-relaxed">
Download any specific folder from a GitHub repository as a ZIP file, preserving its exact internal structure.
</p>
</div>
</div>
<!-- Content -->
<div class="p-8">
<form id="download-form" class="space-y-6">
<!-- URL Input -->
<div>
<label for="url-input" class="block text-sm font-medium text-slate-300 mb-2">
GitHub Directory URL
</label>
<div class="relative">
<div class="absolute inset-y-0 left-0 pl-4 flex items-center pointer-events-none">
<i data-lucide="github" class="w-5 h-5 text-slate-400"></i>
</div>
<input
id="url-input"
type="url"
placeholder="https://github.com/facebook/react/tree/main/packages/react"
class="glass-input block w-full pl-12 pr-4 py-3.5 rounded-xl text-sm placeholder-slate-500"
required
/>
</div>
</div>
<!-- Advanced Options Toggle -->
<button type="button" id="toggle-token-btn" class="flex items-center gap-1.5 text-sm text-slate-400 hover:text-slate-200 transition-colors focus:outline-none">
<i data-lucide="settings" class="w-4 h-4"></i>
<span>Advanced: Personal Access Token</span>
<i data-lucide="chevron-down" id="token-chevron" class="w-4 h-4 transition-transform duration-200"></i>
</button>
<!-- Token Input Container -->
<div id="token-container" class="hidden">
<label for="token-input" class="block text-sm font-medium text-slate-300 mb-2 flex justify-between items-center">
<span>Personal Access Token <span class="text-slate-500 text-xs">(Optional)</span></span>
<a href="https://github.com/settings/tokens/new?description=Git+Loader+Token&scopes=repo" target="_blank" class="text-blue-400 hover:text-blue-300 text-xs flex items-center gap-1 transition-colors">
<i data-lucide="external-link" class="w-3 h-3"></i> Get Token
</a>
</label>
<div class="relative">
<div class="absolute inset-y-0 left-0 pl-4 flex items-center pointer-events-none">
<i data-lucide="key" class="w-5 h-5 text-slate-400"></i>
</div>
<input
id="token-input"
type="password"
placeholder="ghp_xxxxxxxxxxxxxxxxxxxx"
class="glass-input block w-full pl-12 pr-4 py-3.5 rounded-xl text-sm placeholder-slate-500"
/>
</div>
<p class="text-xs text-slate-500 mt-2">Bypass rate limits & access private repositories. Token remains in your browser.</p>
</div>
<!-- Error Message Panel -->
<div id="error-panel" class="hidden bg-red-500/10 border border-red-500/20 p-4 rounded-xl flex items-start gap-3 backdrop-blur-sm">
<i data-lucide="alert-circle" class="w-5 h-5 text-red-400 shrink-0 mt-0.5"></i>
<p id="error-text" class="text-sm text-red-200"></p>
</div>
<!-- Success Message Panel -->
<div id="success-panel" class="hidden bg-emerald-500/10 border border-emerald-500/20 p-4 rounded-xl flex items-center gap-3 backdrop-blur-sm">
<i data-lucide="check-circle" class="w-5 h-5 text-emerald-400 shrink-0"></i>
<p class="text-sm font-medium text-emerald-200">Download complete! The ZIP file has been saved.</p>
</div>
<!-- Progress Panel -->
<div id="progress-panel" class="hidden bg-slate-900/40 border border-white/5 p-5 rounded-xl backdrop-blur-sm shadow-inner">
<div class="flex items-center justify-between mb-3 text-sm font-medium text-slate-300">
<div class="flex items-center gap-2">
<i data-lucide="loader-2" class="w-4 h-4 animate-spin text-blue-400"></i>
<span id="status-text">Scanning directory...</span>
</div>
<span id="progress-percentage" class="text-blue-400 font-bold tracking-wide">0%</span>
</div>
<!-- Progress Bar -->
<div class="h-2 w-full bg-slate-800 rounded-full overflow-hidden shadow-inner ring-1 ring-white/5">
<div
id="progress-fill"
class="h-full bg-blue-500 transition-all duration-300 ease-out shadow-[0_0_10px_rgba(59,130,246,0.8)]"
style="width: 0%"
></div>
</div>
</div>
<!-- Submit Button -->
<button
id="submit-btn"
type="submit"
class="glass-button w-full flex items-center justify-center gap-2 py-3.5 px-4 rounded-xl text-sm font-semibold text-white mt-4"
>
<i data-lucide="search" class="w-5 h-5 btn-icon"></i>
<i data-lucide="loader-2" class="w-5 h-5 animate-spin hidden loading-icon"></i>
<span id="btn-text">Fetch Directory</span>
</button>
</form>
<!-- Tree Panel (New) -->
<div id="tree-panel" class="hidden mt-6 space-y-4 animate-fade-in">
<div class="flex items-center justify-between px-2 text-sm text-slate-300">
<label class="flex items-center gap-2 cursor-pointer group select-none">
<input type="checkbox" id="select-all-cb" class="glass-checkbox" checked>
<span class="group-hover:text-white transition-colors font-medium">Select / Deselect All</span>
</label>
<span class="text-slate-400 text-xs bg-slate-800/50 px-2.5 py-1 rounded-md border border-white/5">
Selected: <span id="selected-count" class="text-blue-400 font-bold">0 / 0</span>
</span>
</div>
<div class="bg-slate-900/40 border border-white/5 p-4 rounded-xl backdrop-blur-sm shadow-inner max-h-72 overflow-y-auto relative">
<ul id="tree-container" class="text-sm">
<!-- Tree will be rendered here -->
</ul>
</div>
<div class="flex gap-3 pt-2">
<button
id="reset-btn"
type="button"
class="glass-panel flex-1 flex items-center justify-center gap-2 py-3.5 px-4 rounded-xl text-sm font-semibold text-slate-300 hover:text-white hover:bg-slate-800/80 transition-all"
>
<i data-lucide="rotate-ccw" class="w-4 h-4"></i>
Start Over
</button>
<button
id="download-selected-btn"
type="button"
class="glass-button flex-[2] flex items-center justify-center gap-2 py-3.5 px-4 rounded-xl text-sm font-semibold text-white"
>
<i data-lucide="download" class="w-5 h-5"></i>
Download Selected
</button>
</div>
</div>
<!-- Footer Info -->
<div class="mt-8 pt-6 border-t border-white/10 flex flex-col gap-4 text-slate-400 text-xs">
<div class="flex items-start gap-3">
<i data-lucide="info" class="w-4 h-4 shrink-0 text-slate-500 mt-0.5"></i>
<p class="leading-relaxed">
<strong>How it works:</strong> This tool connects to GitHub's API directly from your browser. Files are downloaded and zipped locally on your device. Ensure the target repository is <strong>public</strong>, or provide a Personal Access Token above.
</p>
</div>
<div class="flex items-start gap-3">
<i data-lucide="book-open" class="w-4 h-4 shrink-0 text-slate-500 mt-0.5"></i>
<p class="leading-relaxed">
<strong>Curious about the tech?</strong> Check out the <a href="info.html" class="text-blue-400 hover:text-blue-300 underline underline-offset-2 transition-colors">Interactive Documentation (info.html)</a> to learn more about the features and how it was built.
</p>
</div>
</div>
</div>
</div>
<script>
// Initialize Lucide Icons
lucide.createIcons();
// DOM Elements
const form = document.getElementById('download-form');
const urlInput = document.getElementById('url-input');
const tokenInput = document.getElementById('token-input');
const toggleTokenBtn = document.getElementById('toggle-token-btn');
const tokenContainer = document.getElementById('token-container');
const tokenChevron = document.getElementById('token-chevron');
const submitBtn = document.getElementById('submit-btn');
const btnText = document.getElementById('btn-text');
const btnIcon = document.querySelector('.btn-icon');
const loadingIcon = document.querySelector('.loading-icon');
const errorPanel = document.getElementById('error-panel');
const errorText = document.getElementById('error-text');
const successPanel = document.getElementById('success-panel');
const progressPanel = document.getElementById('progress-panel');
const statusText = document.getElementById('status-text');
const progressPercentage = document.getElementById('progress-percentage');
const progressFill = document.getElementById('progress-fill');
// New DOM Elements for Tree
const treePanel = document.getElementById('tree-panel');
const treeContainer = document.getElementById('tree-container');
const selectAllCb = document.getElementById('select-all-cb');
const selectedCount = document.getElementById('selected-count');
const downloadSelectedBtn = document.getElementById('download-selected-btn');
const resetBtn = document.getElementById('reset-btn');
// State variables
let progress = { current: 0, total: 0 };
let currentFetchedFiles = [];
let currentParsedUrl = null;
// Helper: Parse GitHub URL
const parseGithubUrl = (url) => {
try {
url = url.trim();
const regex = /^https?:\/\/github\.com\/([^\/]+)\/([^\/]+)\/tree\/([^\/]+)\/(.+)$/;
const match = url.match(regex);
if (!match) return null;
return {
owner: match[1],
repo: match[2],
branch: decodeURIComponent(match[3]),
path: decodeURIComponent(match[4].replace(/\/$/, ''))
};
} catch (e) {
return null;
}
};
// UI Helpers
const resetUI = () => {
errorPanel.classList.add('hidden');
successPanel.classList.add('hidden');
progressPanel.classList.add('hidden');
treePanel.classList.add('hidden');
progressFill.style.width = '0%';
progressPercentage.textContent = '0%';
statusText.textContent = '';
submitBtn.classList.remove('hidden');
urlInput.disabled = false;
};
const setLoading = (isLoading) => {
urlInput.disabled = isLoading;
submitBtn.disabled = isLoading;
if (isLoading) {
btnText.textContent = 'Scanning...';
btnIcon.classList.add('hidden');
loadingIcon.classList.remove('hidden');
} else {
btnText.textContent = 'Fetch Directory';
btnIcon.classList.remove('hidden');
loadingIcon.classList.add('hidden');
}
};
const showError = (message) => {
resetUI();
errorText.textContent = message;
errorPanel.classList.remove('hidden');
setLoading(false);
};
const updateProgress = () => {
if (progress.total === 0) return;
const percent = Math.round((progress.current / progress.total) * 100);
progressFill.style.width = `${percent}%`;
progressPercentage.textContent = `${percent}%`;
statusText.textContent = `Fetching files (${progress.current}/${progress.total})...`;
};
// Toggle Token Visibility
toggleTokenBtn.addEventListener('click', () => {
tokenContainer.classList.toggle('hidden');
tokenChevron.classList.toggle('rotate-180');
});
// Checkbox Cascade Logic
const updateTreeSelections = () => {
const allFiles = document.querySelectorAll('.file-checkbox[data-type="file"]');
const checkedFiles = Array.from(allFiles).filter(cb => cb.checked).length;
selectedCount.textContent = `${checkedFiles} / ${allFiles.length}`;
downloadSelectedBtn.disabled = checkedFiles === 0;
const allChecked = checkedFiles === allFiles.length && allFiles.length > 0;
const someChecked = checkedFiles > 0;
selectAllCb.checked = allChecked;
selectAllCb.indeterminate = !allChecked && someChecked;
};
treeContainer.addEventListener('change', (e) => {
if (!e.target.classList.contains('file-checkbox')) return;
const cb = e.target;
const isChecked = cb.checked;
const li = cb.closest('li');
// 1. Cascade Down
const childCbs = li.querySelectorAll('.file-checkbox');
childCbs.forEach(child => {
child.checked = isChecked;
child.indeterminate = false;
});
// 2. Cascade Up
let parentUl = li.parentElement;
while (parentUl && parentUl.classList.contains('tree-children')) {
const parentLi = parentUl.closest('li');
if (!parentLi) break;
const parentCb = parentLi.querySelector(':scope > div > .file-checkbox');
if (!parentCb) break;
const siblingCbs = Array.from(parentUl.querySelectorAll(':scope > li > div > .file-checkbox'));
const allChecked = siblingCbs.every(c => c.checked && !c.indeterminate);
const someChecked = siblingCbs.some(c => c.checked || c.indeterminate);
parentCb.checked = allChecked;
parentCb.indeterminate = !allChecked && someChecked;
parentUl = parentLi.parentElement;
}
updateTreeSelections();
});
selectAllCb.addEventListener('change', (e) => {
const isChecked = e.target.checked;
document.querySelectorAll('.file-checkbox').forEach(cb => {
cb.checked = isChecked;
cb.indeterminate = false;
});
updateTreeSelections();
});
// Tree Generation
const buildTreeData = (files, folderName) => {
const root = { name: folderName, path: 'ROOT', type: 'folder', children: {} };
files.forEach(file => {
const relativePath = file.relativePath;
if (!relativePath) return;
const parts = relativePath.split('/');
let current = root;
parts.forEach((part, index) => {
if (!current.children[part]) {
current.children[part] = {
name: part,
path: parts.slice(0, index + 1).join('/'),
type: index === parts.length - 1 ? 'file' : 'folder',
children: {}
};
}
current = current.children[part];
});
});
return root;
};
const renderTreeDOM = (node) => {
const li = document.createElement('li');
li.className = 'mt-1.5 relative';
const row = document.createElement('div');
row.className = 'flex items-center gap-2.5 py-1.5 px-2 rounded-lg hover:bg-white/10 cursor-pointer text-slate-200 transition-colors group';
const chevronHtml = node.type === 'folder'
? `<i data-lucide="chevron-down" class="w-4 h-4 text-slate-400 group-hover:text-slate-200 transition-colors chevron-icon shrink-0"></i>`
: `<div class="w-4 h-4 shrink-0"></div>`; // Spacer for files
const iconHtml = node.type === 'folder'
? `<i data-lucide="folder" class="w-4 h-4 text-blue-400 shrink-0"></i>`
: `<i data-lucide="file" class="w-4 h-4 text-slate-400 shrink-0"></i>`;
row.innerHTML = `
${chevronHtml}
<input type="checkbox" class="glass-checkbox file-checkbox" data-path="${node.path}" data-type="${node.type}" checked>
${iconHtml}
<span class="truncate select-none">${node.name}</span>
`;
li.appendChild(row);
if (node.type === 'folder') {
const ul = document.createElement('ul');
ul.className = 'pl-5 ml-2 border-l border-white/10 mt-1 tree-children';
// Sort: Folders first, then alphabetically
const sortedChildren = Object.values(node.children).sort((a, b) => {
if (a.type !== b.type) return a.type === 'folder' ? -1 : 1;
return a.name.localeCompare(b.name);
});
sortedChildren.forEach(child => ul.appendChild(renderTreeDOM(child)));
li.appendChild(ul);
// Expand/Collapse logic
const toggleCollapse = (e) => {
if (e.target.type === 'checkbox') return;
e.stopPropagation();
ul.classList.toggle('hidden');
const chevron = row.querySelector('.chevron-icon');
if (chevron) {
chevron.setAttribute('data-lucide', ul.classList.contains('hidden') ? 'chevron-right' : 'chevron-down');
lucide.createIcons({root: row});
}
};
row.addEventListener('click', toggleCollapse);
} else {
row.addEventListener('click', (e) => {
if (e.target.type === 'checkbox') return;
const cb = row.querySelector('.file-checkbox');
cb.checked = !cb.checked;
cb.dispatchEvent(new Event('change', { bubbles: true }));
});
}
return li;
};
// Concurrency Execution
const executeConcurrently = async (items, asyncFn, limit) => {
const results = [];
const executing = [];
for (const item of items) {
const p = Promise.resolve().then(() => asyncFn(item));
results.push(p);
if (limit <= items.length) {
const e = p.then(() => executing.splice(executing.indexOf(e), 1));
executing.push(e);
if (executing.length >= limit) await Promise.race(executing);
}
}
return Promise.all(results);
};
// Step 1: Fetch Repository Map
form.addEventListener('submit', async (e) => {
e.preventDefault();
const url = urlInput.value;
if (!url) return;
resetUI();
setLoading(true);
currentParsedUrl = parseGithubUrl(url);
if (!currentParsedUrl) {
showError("Invalid URL format. Please provide a link to a specific GitHub directory.");
return;
}
const { owner, repo, branch, path } = currentParsedUrl;
try {
// Auth headers if token is provided
const token = tokenInput.value.trim();
const headers = token ? { 'Authorization': `Bearer ${token}` } : {};
// 1. Traverse path step-by-step to get target folder SHA to avoid repo-wide truncation limits
let currentSha = branch;
const parts = path.split('/');
for (let i = 0; i < parts.length; i++) {
const part = parts[i];
statusText.textContent = `Locating: ${part}...`;
const res = await fetch(`https://api.github.com/repos/${owner}/${repo}/git/trees/${currentSha}`, { headers });
if (!res.ok) {
if (res.status === 404) throw new Error(`Could not find folder: ${part}`);
if (res.status === 403) throw new Error("GitHub API rate limit exceeded.");
throw new Error(`Failed to access folder: ${part}`);
}
const data = await res.json();
const target = data.tree.find(item => item.path === part);
if (!target) throw new Error(`Folder "${part}" not found.`);
if (target.type !== 'tree') throw new Error(`"${part}" is a file, not a directory.`);
currentSha = target.sha;
}
// 2. Fetch ONLY the target directory recursively
statusText.textContent = 'Scanning directory structure...';
const treeUrl = `https://api.github.com/repos/${owner}/${repo}/git/trees/${currentSha}?recursive=1`;
const treeRes = await fetch(treeUrl, { headers });
if (!treeRes.ok) throw new Error("Failed to fetch directory contents.");
const treeData = await treeRes.json();
currentFetchedFiles = treeData.tree
.filter(item => item.type === 'blob')
.map(item => ({
...item,
relativePath: item.path,
fullPath: `${path}/${item.path}`
}));
if (currentFetchedFiles.length === 0) throw new Error("No files found in the specified directory.");
// Build and Render Tree
const folderName = path.split('/').pop();
const treeDataHierarchy = buildTreeData(currentFetchedFiles, folderName);
treeContainer.innerHTML = '';
treeContainer.appendChild(renderTreeDOM(treeDataHierarchy));
lucide.createIcons({root: treeContainer});
// Show Tree UI
submitBtn.classList.add('hidden');
treePanel.classList.remove('hidden');
selectAllCb.checked = true;
updateTreeSelections();
} catch (err) {
showError(err.message || "An unexpected error occurred.");
} finally {
setLoading(false);
}
});
// Step 2: Download Selected Files
downloadSelectedBtn.addEventListener('click', async () => {
const checkedFileNodes = Array.from(document.querySelectorAll('.file-checkbox[data-type="file"]:checked'));
if (checkedFileNodes.length === 0) return;
const pathsToDownload = new Set(checkedFileNodes.map(cb => cb.dataset.path));
const filesToDownload = currentFetchedFiles.filter(file => pathsToDownload.has(file.relativePath));
const { owner, repo, branch, path } = currentParsedUrl;
treePanel.classList.add('hidden');
progressPanel.classList.remove('hidden');
progress = { current: 0, total: filesToDownload.length };
updateProgress();
try {
const zip = new JSZip();
const CONCURRENCY_LIMIT = 6;
const token = tokenInput.value.trim();
const headers = token ? { 'Authorization': `Bearer ${token}` } : {};
const downloadFile = async (file) => {
// Encode the file path properly so URLs with spaces and special characters work
const encodedFilePath = file.fullPath.split('/').map(encodeURIComponent).join('/');
const rawUrl = `https://raw.githubusercontent.com/${owner}/${repo}/${branch}/${encodedFilePath}`;
const res = await fetch(rawUrl, { headers });
if (!res.ok) throw new Error(`Failed to download: ${file.relativePath}`);
const blob = await res.blob();
const folderName = path.split('/').pop();
const zipPath = `${folderName}/${file.relativePath}`;
zip.file(zipPath, blob);
progress.current++;
updateProgress();
};
await executeConcurrently(filesToDownload, downloadFile, CONCURRENCY_LIMIT);
statusText.textContent = 'Compressing into ZIP...';
const zipBlob = await zip.generateAsync({ type: 'blob' });
const downloadLink = document.createElement('a');
downloadLink.href = URL.createObjectURL(zipBlob);
downloadLink.download = `${repo}-${path.split('/').pop()}-selected.zip`;
document.body.appendChild(downloadLink);
downloadLink.click();
document.body.removeChild(downloadLink);
URL.revokeObjectURL(downloadLink.href);
// Show Success
resetUI();
successPanel.classList.remove('hidden');
setTimeout(() => successPanel.classList.add('hidden'), 5000);
} catch (err) {
showError(err.message || "An error occurred during download.");
}
});
// Reset Button
resetBtn.addEventListener('click', () => {
resetUI();
urlInput.value = '';
// Deliberately not clearing the token so users don't have to repeatedly re-enter it
urlInput.focus();
});
// Validation UI
urlInput.addEventListener('input', () => {
if (urlInput.value && !parseGithubUrl(urlInput.value)) {
urlInput.style.borderColor = 'rgba(239, 68, 68, 0.5)';
} else {
urlInput.style.borderColor = '';
}
});
</script>
</body>
</html>