ChocolateBird/data/html5_file_picker.js

32 lines
1.2 KiB
JavaScript
Raw Normal View History

2023-12-06 18:19:19 +10:30
var _snes_types = "application/vnd.nintendo.snes.rom,.sfc,.smc,.srm";
var _psx_types = "application/x-cd-image,application/x-iso9660-image,.bin,.iso";
var _gba_types = "application/x-gba-rom,.gba";
var _accepted_types = _snes_types+","+_psx_types+","+_gba_types;
var _FilePicker = {};
var _FilePickerData = {};
_FilePicker.upload = function(gd_callback) {
// canceled = true;
var input = document.createElement('INPUT');
input.setAttribute("type", "file");
2023-12-06 18:19:19 +10:30
input.setAttribute("accept", _accepted_types);
input.setAttribute("multiple", "");
input.click();
input.addEventListener('change', event => {
for (const file of event.target.files) {
var reader = new FileReader();
// this.filename = file.name;
// this.file_type = file.type;
reader.readAsArrayBuffer(file);
reader.onloadend = (evt) => {
if (evt.target.readyState == FileReader.DONE) {
// Godot's JavaScriptObject API is very very poorly documented.
// It might be possible to unwrap JavaScriptObject<ArrayBuffer> to PoolByteArray
// without a bytewise for loop, but I can't work it out by trial and error.
_FilePickerData[file.name] = evt.target.result;
gd_callback(file.name, file.type);
}
}
}
});
}