From 4de5d20368d4b2ca4f0851eafa900cd0c9a3c691 Mon Sep 17 00:00:00 2001 From: Ben Harris Date: Mon, 3 Apr 2023 22:11:42 +0100 Subject: [PATCH] js: use the "load" event for loading save files This is in place of the "loadend" event. In Chromium, (and in the specification), "loadend" is triggered not only when the file is loaded but also when loading fails. Obviously when loading fails we don't want to try to parse the (nonexistent) resulting file. Using the "load" event works better, since it's only fired on success, and we can also have an "error" handler to report problems with loading files, albeit with no detail at all. This doesn't seem to make any difference in Firefox, which in my testing fires "load" and "loadend" on success and nothing at all on failure. --- emccpre.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/emccpre.js b/emccpre.js index 837f815..7c68445 100644 --- a/emccpre.js +++ b/emccpre.js @@ -462,7 +462,7 @@ function initPuzzle() { if (input.files.length == 1) { var file = input.files.item(0); var reader = new FileReader(); - reader.addEventListener("loadend", function() { + reader.addEventListener("load", function() { var pos = 0; savefile_read_callback = function(buf, len) { if (pos + len > reader.result.byteLength) @@ -475,6 +475,9 @@ function initPuzzle() { load_game(); savefile_read_callback = null; }); + reader.addEventListener("error", function() { + alert("An error occured while loading the file"); + }); reader.readAsArrayBuffer(file); } });