|
1 | 1 | // ==UserScript== |
2 | 2 | // @name MusicBrainz: Artwork Uploader Turbo |
3 | 3 | // @namespace https://musicbrainz.org/user/chaban |
4 | | -// @version 3.3.1 |
| 4 | +// @version 3.3.2 |
5 | 5 | // @tag ai-created |
6 | 6 | // @description Allows for multiple artwork images to be uploaded simultaneously and recursively upload directories. |
7 | 7 | // @author chaban |
|
203 | 203 | }, |
204 | 204 |
|
205 | 205 | async _handleDirectorySelection(event) { |
206 | | - const files = Array.from(event.target.files); |
207 | | - if (files.length === 0) { |
208 | | - ArtworkUploaderTurbo.logger.warn('No files found in selected directory.'); |
209 | | - return; |
210 | | - } |
| 206 | + try { |
| 207 | + const files = Array.from(event.target.files); |
| 208 | + if (files.length === 0) { |
| 209 | + ArtworkUploaderTurbo.logger.warn('No files found in selected directory.'); |
| 210 | + return; |
| 211 | + } |
211 | 212 |
|
212 | | - if (ArtworkUploaderTurbo.state.upvm?.addFile) { |
213 | 213 | const validationPromises = files.map(file => |
214 | 214 | ArtworkUploaderTurbo.toNativePromise(MB.Art.validate_file(file)) |
215 | 215 | .then(() => ({ file, valid: true })) |
|
226 | 226 | ArtworkUploaderTurbo.logger.log(`Ignoring ${files.length - validFiles.length} invalid files.`); |
227 | 227 | } |
228 | 228 |
|
229 | | - validFiles.forEach(file => ArtworkUploaderTurbo.state.upvm.addFile(file)); |
230 | | - |
231 | | - const formName = window.__MB__.$c.action.name.replace(/_/g, '-'); |
232 | | - document.querySelector(`#${formName}-submit`).disabled = false; |
233 | | - } else { |
234 | | - ArtworkUploaderTurbo.logger.error("Could not access the captured UploadProcessViewModel."); |
| 229 | + if (validFiles.length > 0) { |
| 230 | + const dt = new DataTransfer(); |
| 231 | + validFiles.forEach(file => dt.items.add(file)); |
| 232 | + |
| 233 | + const $input = $('input.add-files'); |
| 234 | + if ($input.length) { |
| 235 | + $input[0].files = dt.files; |
| 236 | + $input.trigger('change'); |
| 237 | + } else { |
| 238 | + const fileInput = document.querySelector('input.add-files'); |
| 239 | + if (fileInput) { |
| 240 | + fileInput.files = dt.files; |
| 241 | + fileInput.dispatchEvent(new Event('change', { bubbles: true })); |
| 242 | + } else { |
| 243 | + ArtworkUploaderTurbo.logger.error("Could not find page's input.add-files element."); |
| 244 | + } |
| 245 | + } |
| 246 | + } |
| 247 | + } catch (error) { |
| 248 | + ArtworkUploaderTurbo.logger.error("Failed to process directory selection:", error); |
| 249 | + } finally { |
| 250 | + event.target.value = ''; |
235 | 251 | } |
236 | | - |
237 | | - event.target.value = ''; |
238 | 252 | }, |
239 | 253 | }, |
240 | 254 |
|
|
437 | 451 | if (window.MB?.Art?.add_art_submit && window.MB?.Art?.UploadProcessViewModel && window.__MB__?.$c && window.$) { |
438 | 452 | clearInterval(checkMB); |
439 | 453 |
|
440 | | - const originalVM = MB.Art.UploadProcessViewModel; |
441 | | - MB.Art.UploadProcessViewModel = function (...args) { |
442 | | - const instance = new originalVM(...args); |
443 | | - ArtworkUploaderTurbo.state.upvm = instance; |
444 | | - ArtworkUploaderTurbo.logger.log('Successfully captured UploadProcessViewModel instance.'); |
445 | | - return instance; |
446 | | - }; |
447 | | - |
448 | 454 | this.UI.init(); |
449 | 455 | this.Uploader.init(); |
450 | 456 | this.DirectoryUploader.init(); |
|
0 commit comments