Skip to content

Commit b6a848e

Browse files
committed
fix(mb-artwork-uploader-turbo): use robust DataTransfer dispatch for directory uploading
1 parent 6be9baa commit b6a848e

1 file changed

Lines changed: 29 additions & 23 deletions

File tree

src/MusicBrainz Artwork Uploader Turbo.user.js

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// ==UserScript==
22
// @name MusicBrainz: Artwork Uploader Turbo
33
// @namespace https://musicbrainz.org/user/chaban
4-
// @version 3.3.1
4+
// @version 3.3.2
55
// @tag ai-created
66
// @description Allows for multiple artwork images to be uploaded simultaneously and recursively upload directories.
77
// @author chaban
@@ -203,13 +203,13 @@
203203
},
204204

205205
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+
}
211212

212-
if (ArtworkUploaderTurbo.state.upvm?.addFile) {
213213
const validationPromises = files.map(file =>
214214
ArtworkUploaderTurbo.toNativePromise(MB.Art.validate_file(file))
215215
.then(() => ({ file, valid: true }))
@@ -226,15 +226,29 @@
226226
ArtworkUploaderTurbo.logger.log(`Ignoring ${files.length - validFiles.length} invalid files.`);
227227
}
228228

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 = '';
235251
}
236-
237-
event.target.value = '';
238252
},
239253
},
240254

@@ -437,14 +451,6 @@
437451
if (window.MB?.Art?.add_art_submit && window.MB?.Art?.UploadProcessViewModel && window.__MB__?.$c && window.$) {
438452
clearInterval(checkMB);
439453

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-
448454
this.UI.init();
449455
this.Uploader.init();
450456
this.DirectoryUploader.init();

0 commit comments

Comments
 (0)