Skip to content

Commit 7890919

Browse files
Download zip of selected files
1 parent ba3b51a commit 7890919

File tree

4 files changed

+72
-7
lines changed

4 files changed

+72
-7
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ This web-based tool converts GitHub repository contents into a formatted text fi
1818
- Download generated text
1919
- Support for private repositories
2020
- Browser-based for privacy and security
21+
- Download zip of selected files
2122

2223
This tool runs entirely in the browser, ensuring data security without server-side processing.
2324

2425

2526
## To do
2627

27-
- Download zip of selected files
2828
- Support for local directories
2929

3030
## Contributing

index.html

+12-5
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333

3434
gtag('config', 'G-CYF86LN5WM');
3535
</script>
36+
<script src="jszip.min.js"></script>
3637
</head>
3738
<body class="bg-gray-100 min-h-screen p-4 md:p-8 text-gray-600">
3839
<div class="max-w-4xl mx-auto bg-white rounded-lg shadow-md p-6 relative">
@@ -66,10 +67,16 @@ <h1 class="text-3xl font-bold mb-2 text-center text-gray-600">GitHub to Plain Te
6667
</form>
6768
<div id="extentionCheckboxes"></div>
6869
<div id="directoryStructure" class="mt-6"></div>
69-
<button id="generateTextButton" class="mt-4 bg-green-500 hover:bg-green-600 text-white font-bold py-2 px-4 rounded focus:outline-none focus:shadow-outline hidden flex items-center justify-center">
70-
<i data-lucide="file-text" class="w-5 h-5 mr-2"></i>
71-
Generate Text File
72-
</button>
70+
<div class="mt-4 flex space-x-4">
71+
<button id="generateTextButton" class="flex-1 bg-green-500 hover:bg-green-600 text-white font-bold py-2 px-4 rounded focus:outline-none focus:shadow-outline hidden flex items-center justify-center">
72+
<i data-lucide="file-text" class="w-5 h-5 mr-2"></i>
73+
Generate Text File
74+
</button>
75+
<button id="downloadZipButton" class="flex-1 bg-purple-500 hover:bg-purple-600 text-white font-bold py-2 px-4 rounded focus:outline-none focus:shadow-outline hidden flex items-center justify-center">
76+
<i data-lucide="download" class="w-5 h-5 mr-2"></i>
77+
Download Zip
78+
</button>
79+
</div>
7380
<textarea id="outputText" rows="20" class="mt-4 w-full p-2 border rounded-md font-mono" readonly></textarea>
7481
<div class="mt-4 flex space-x-4">
7582
<button id="copyButton" class="flex-1 bg-indigo-500 hover:bg-indigo-600 text-white font-bold py-2 px-4 rounded focus:outline-none focus:shadow-outline hidden flex items-center justify-center">
@@ -78,7 +85,7 @@ <h1 class="text-3xl font-bold mb-2 text-center text-gray-600">GitHub to Plain Te
7885
</button>
7986
<button id="downloadButton" class="flex-1 bg-purple-500 hover:bg-purple-600 text-white font-bold py-2 px-4 rounded focus:outline-none focus:shadow-outline hidden flex items-center justify-center">
8087
<i data-lucide="download" class="w-5 h-5 mr-2"></i>
81-
Download
88+
Download Text File
8289
</button>
8390
</div>
8491
</div>

jszip.min.js

+13
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

script.js

+46-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ document.getElementById('repoForm').addEventListener('submit', async function (e
1515

1616
displayDirectoryStructure(tree);
1717
document.getElementById('generateTextButton').style.display = 'flex';
18+
document.getElementById('downloadZipButton').style.display = 'flex';
1819
} catch (error) {
1920
outputText.value = `Error fetching repository contents: ${error.message}\n\n` +
2021
"Please ensure:\n" +
@@ -52,6 +53,29 @@ document.getElementById('generateTextButton').addEventListener('click', async fu
5253
}
5354
});
5455

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+
5579
// Event listener for copying text to clipboard
5680
document.getElementById('copyButton').addEventListener('click', function () {
5781
const outputText = document.getElementById('outputText');
@@ -72,7 +96,7 @@ document.getElementById('downloadButton').addEventListener('click', function ()
7296
const url = URL.createObjectURL(blob);
7397
const a = document.createElement('a');
7498
a.href = url;
75-
a.download = 'file.txt';
99+
a.download = 'prompt.txt';
76100
a.click();
77101
URL.revokeObjectURL(url);
78102
});
@@ -485,4 +509,25 @@ function updateInfoIcon(button, tokenInfo) {
485509
icon.setAttribute('data-lucide', tokenInfo.classList.contains('hidden') ? 'info' : 'x');
486510
lucide.createIcons();
487511
}
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);
488533
}

0 commit comments

Comments
 (0)