@@ -15,6 +15,7 @@ document.getElementById('repoForm').addEventListener('submit', async function (e
15
15
16
16
displayDirectoryStructure ( tree ) ;
17
17
document . getElementById ( 'generateTextButton' ) . style . display = 'flex' ;
18
+ document . getElementById ( 'downloadZipButton' ) . style . display = 'flex' ;
18
19
} catch ( error ) {
19
20
outputText . value = `Error fetching repository contents: ${ error . message } \n\n` +
20
21
"Please ensure:\n" +
@@ -52,6 +53,29 @@ document.getElementById('generateTextButton').addEventListener('click', async fu
52
53
}
53
54
} ) ;
54
55
56
+ // Event listener for downloading zip file
57
+ document . getElementById ( 'downloadZipButton' ) . addEventListener ( 'click' , async function ( ) {
58
+ const accessToken = document . getElementById ( 'accessToken' ) . value ;
59
+ const outputText = document . getElementById ( 'outputText' ) ;
60
+ outputText . value = '' ;
61
+
62
+ try {
63
+ const selectedFiles = getSelectedFiles ( ) ;
64
+ if ( selectedFiles . length === 0 ) {
65
+ throw new Error ( 'No files selected' ) ;
66
+ }
67
+ const fileContents = await fetchFileContents ( selectedFiles , accessToken ) ;
68
+ await createAndDownloadZip ( fileContents ) ;
69
+ } catch ( error ) {
70
+ outputText . value = `Error generating zip file: ${ error . message } \n\n` +
71
+ "Please ensure:\n" +
72
+ "1. You have selected at least one file from the directory structure.\n" +
73
+ "2. Your access token (if provided) is valid and has the necessary permissions.\n" +
74
+ "3. You have a stable internet connection.\n" +
75
+ "4. The GitHub API is accessible and functioning normally." ;
76
+ }
77
+ } ) ;
78
+
55
79
// Event listener for copying text to clipboard
56
80
document . getElementById ( 'copyButton' ) . addEventListener ( 'click' , function ( ) {
57
81
const outputText = document . getElementById ( 'outputText' ) ;
@@ -72,7 +96,7 @@ document.getElementById('downloadButton').addEventListener('click', function ()
72
96
const url = URL . createObjectURL ( blob ) ;
73
97
const a = document . createElement ( 'a' ) ;
74
98
a . href = url ;
75
- a . download = 'file .txt' ;
99
+ a . download = 'prompt .txt' ;
76
100
a . click ( ) ;
77
101
URL . revokeObjectURL ( url ) ;
78
102
} ) ;
@@ -485,4 +509,25 @@ function updateInfoIcon(button, tokenInfo) {
485
509
icon . setAttribute ( 'data-lucide' , tokenInfo . classList . contains ( 'hidden' ) ? 'info' : 'x' ) ;
486
510
lucide . createIcons ( ) ;
487
511
}
512
+ }
513
+
514
+ // Create and download zip file
515
+ async function createAndDownloadZip ( fileContents ) {
516
+ const zip = new JSZip ( ) ;
517
+
518
+ fileContents . forEach ( file => {
519
+ // Remove leading slash if present
520
+ const filePath = file . path . startsWith ( '/' ) ? file . path . slice ( 1 ) : file . path ;
521
+ zip . file ( filePath , file . text ) ;
522
+ } ) ;
523
+
524
+ const content = await zip . generateAsync ( { type : "blob" } ) ;
525
+ const url = URL . createObjectURL ( content ) ;
526
+ const a = document . createElement ( 'a' ) ;
527
+ a . href = url ;
528
+ a . download = 'repository_files.zip' ;
529
+ document . body . appendChild ( a ) ;
530
+ a . click ( ) ;
531
+ document . body . removeChild ( a ) ;
532
+ URL . revokeObjectURL ( url ) ;
488
533
}
0 commit comments