|
| 1 | +// content.js |
| 2 | + |
1 | 3 | function getAcceptedFileTypes() {
|
2 | 4 | const fileInput = document.querySelector('input[data-testid="file-upload"]');
|
3 | 5 | if (!fileInput) {
|
@@ -46,7 +48,7 @@ function injectFolderUploadButton() {
|
46 | 48 |
|
47 | 49 | const folderUploadButton = document.createElement('button');
|
48 | 50 | folderUploadButton.id = 'claude-folder-upload';
|
49 |
| - folderUploadButton.innerHTML = '<svg xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-folder-up"><path d="M20 20a2 2 0 0 0 2-2V8a2 2 0 0 0-2-2h-7.9a2 2 0 0 1-1.69-.9L9.6 3.9A2 2 0 0 0 7.93 3H4a2 2 0 0 0-2 2v13a2 2 0 0 0 2 2Z"/><path d="M12 10v6"/><path d="m9 13 3-3 3 3"/></svg>'; |
| 51 | + folderUploadButton.innerHTML = '<svg xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-folder-up"><path d="M20 20a2 2 0 0 0 2-2V8a2 2 0 0 0-2-2h-7.9a2 2 0 0 1-1.69-.9L9.6 3.9A2 2 0 0 0 7.93 3H4a2 2 0 0 0-2 2v13a2 2 0 0 0 2 2Z"/><path d="M12 10v6"/><path d="m9 13 3-3 3 3"/></svg>'; |
50 | 52 | folderUploadButton.title = 'Upload Folder';
|
51 | 53 | folderUploadButton.setAttribute('aria-label', 'Upload folder');
|
52 | 54 |
|
@@ -96,25 +98,61 @@ function injectFolderUploadButton() {
|
96 | 98 | }
|
97 | 99 |
|
98 | 100 | try {
|
99 |
| - const dataTransfer = new DataTransfer(); |
100 |
| - |
101 |
| - allowedFiles.forEach(file => { |
102 |
| - try { |
103 |
| - dataTransfer.items.add(file); |
104 |
| - } catch (e) { |
105 |
| - console.warn('Failed to add file:', file.name, e); |
| 101 | + let combinedContent = ''; |
| 102 | + const rootFolderName = allowedFiles[0].webkitRelativePath.split('/')[0]; |
| 103 | + |
| 104 | + if (allowedFiles.length > 5) { |
| 105 | + // Sort files to maintain folder structure |
| 106 | + allowedFiles.sort((a, b) => a.webkitRelativePath.localeCompare(b.webkitRelativePath)); |
| 107 | + |
| 108 | + let currentPath = ''; |
| 109 | + for (const file of allowedFiles) { |
| 110 | + const relativePath = file.webkitRelativePath.slice(rootFolderName.length + 1); |
| 111 | + const folderPath = relativePath.split('/').slice(0, -1).join('/'); |
| 112 | + |
| 113 | + // Only add folder path if it has changed |
| 114 | + if (folderPath !== currentPath) { |
| 115 | + currentPath = folderPath; |
| 116 | + } |
| 117 | + |
| 118 | + // Format the file entry |
| 119 | + combinedContent += `[File]: ${currentPath ? `${currentPath}/` : ''}${file.name}\n`; |
| 120 | + |
| 121 | + if (file.type.startsWith('image/')) { |
| 122 | + combinedContent += '<image_content>\n'; |
| 123 | + } else { |
| 124 | + const content = await file.text(); |
| 125 | + combinedContent += `${content}\n`; |
| 126 | + } |
106 | 127 | }
|
107 |
| - }); |
108 | 128 |
|
109 |
| - originalInput.files = dataTransfer.files; |
110 |
| - originalInput.dispatchEvent(new Event('change', { bubbles: true })); |
| 129 | + const combinedFile = new File([combinedContent.trim()], `${rootFolderName}_combined.txt`, { type: 'text/plain' }); |
| 130 | + |
| 131 | + const dataTransfer = new DataTransfer(); |
| 132 | + dataTransfer.items.add(combinedFile); |
| 133 | + originalInput.files = dataTransfer.files; |
| 134 | + originalInput.dispatchEvent(new Event('change', { bubbles: true })); |
| 135 | + |
| 136 | + showToast(`Successfully combined ${allowedFiles.length} files into one .txt file`); |
| 137 | + } else { |
| 138 | + const dataTransfer = new DataTransfer(); |
| 139 | + allowedFiles.forEach(file => { |
| 140 | + try { |
| 141 | + dataTransfer.items.add(file); |
| 142 | + } catch (e) { |
| 143 | + console.warn('Failed to add file:', file.name, e); |
| 144 | + } |
| 145 | + }); |
| 146 | + |
| 147 | + originalInput.files = dataTransfer.files; |
| 148 | + originalInput.dispatchEvent(new Event('change', { bubbles: true })); |
| 149 | + |
| 150 | + showToast(`Successfully added ${allowedFiles.length} file${allowedFiles.length === 1 ? '' : 's'}`); |
| 151 | + } |
111 | 152 |
|
112 |
| - const fileCount = allowedFiles.length; |
113 |
| - let message = `Successfully added ${fileCount} file${fileCount === 1 ? '' : 's'} from folder`; |
114 | 153 | if (ignoredCount > 0) {
|
115 |
| - message += `. ${ignoredCount} file${ignoredCount === 1 ? '' : 's'} starting with . were ignored for safety.`; |
| 154 | + showToast(`${ignoredCount} file${ignoredCount === 1 ? '' : 's'} starting with . were ignored for safety.`, 5000); |
116 | 155 | }
|
117 |
| - showToast(message); |
118 | 156 | } catch (error) {
|
119 | 157 | console.error('Error processing files:', error);
|
120 | 158 | showToast('An error occurred while processing the files. Please try again with fewer files.');
|
|
0 commit comments