shithub: puzzles

Download patch

ref: 4de5d20368d4b2ca4f0851eafa900cd0c9a3c691
parent: 36c282aaa92bd42d570cdc76fe3b9e76d8da1ff1
author: Ben Harris <bjh21@bjh21.me.uk>
date: Mon Apr 3 18:11:42 EDT 2023

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.

--- a/emccpre.js
+++ b/emccpre.js
@@ -462,7 +462,7 @@
                 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)
@@ -474,6 +474,9 @@
                         }
                         load_game();
                         savefile_read_callback = null;
+                    });
+                    reader.addEventListener("error", function() {
+                        alert("An error occured while loading the file");
                     });
                     reader.readAsArrayBuffer(file);
                 }