From 5ba1bf55600443ca6cdad448aa629ffbce7c4e22 Mon Sep 17 00:00:00 2001 From: Ben Harris Date: Sun, 11 Dec 2022 12:17:54 +0000 Subject: [PATCH] js: Tolerate the absence of various UI elements from the HTML To make things more consistent, the static buttons now all have their own variables. --- emcclib.js | 12 +++++++----- emccpre.js | 27 +++++++++++++++++---------- 2 files changed, 24 insertions(+), 15 deletions(-) diff --git a/emcclib.js b/emcclib.js index 676c429..07cbf3b 100644 --- a/emcclib.js +++ b/emcclib.js @@ -62,9 +62,11 @@ mergeInto(LibraryManager.library, { * provides neither presets nor configurability. */ js_remove_type_dropdown: function() { + if (gametypelist === null) return; var gametypeitem = gametypelist.closest("li"); if (gametypeitem === null) return; gametypeitem.parentNode.removeChild(gametypeitem); + gametypelist = null; }, /* @@ -74,11 +76,11 @@ mergeInto(LibraryManager.library, { * time if the game doesn't support an in-game solve function. */ js_remove_solve_button: function() { - var solvebutton = document.getElementById("solve"); - if (solvebutton === null) return; - var solveitem = solvebutton.closest("li"); - if (solveitem === null) return; - solveitem.parentNode.removeChild(solveitem); + if (solve_button === null) return; + var solve_item = solve_button.closest("li"); + if (solve_item === null) return; + solve_item.parentNode.removeChild(solve_item); + solve_button = null; }, /* diff --git a/emccpre.js b/emccpre.js index aa215ac..cc7090a 100644 --- a/emccpre.js +++ b/emccpre.js @@ -146,9 +146,16 @@ var menuform = document.getElementById("gamemenu"); var permalink_seed = document.getElementById("permalink-seed"); var permalink_desc = document.getElementById("permalink-desc"); -// The undo and redo buttons. Used by js_enable_undo_redo(). +// The various buttons. Undo and redo are used by js_enable_undo_redo(). +var specific_button = document.getElementById("specific"); +var random_button = document.getElementById("random"); +var new_button = document.getElementById("new"); +var restart_button = document.getElementById("restart"); var undo_button = document.getElementById("undo"); var redo_button = document.getElementById("redo"); +var solve_button = document.getElementById("solve"); +var save_button = document.getElementById("save"); +var load_button = document.getElementById("load"); // A div element enclosing both the puzzle and its status bar, used // for positioning the resize handle. @@ -359,34 +366,34 @@ function initPuzzle() { command = Module.cwrap('command', 'void', ['number']); // Event handlers for buttons and things, which call command(). - document.getElementById("specific").onclick = function(event) { + if (specific_button) specific_button.onclick = function(event) { // Ensure we don't accidentally process these events when a // dialog is actually active, e.g. because the button still // has keyboard focus if (dlg_dimmer === null) command(0); }; - document.getElementById("random").onclick = function(event) { + if (random_button) random_button.onclick = function(event) { if (dlg_dimmer === null) command(1); }; - document.getElementById("new").onclick = function(event) { + if (new_button) new_button.onclick = function(event) { if (dlg_dimmer === null) command(5); }; - document.getElementById("restart").onclick = function(event) { + if (restart_button) restart_button.onclick = function(event) { if (dlg_dimmer === null) command(6); }; - undo_button.onclick = function(event) { + if (undo_button) undo_button.onclick = function(event) { if (dlg_dimmer === null) command(7); }; - redo_button.onclick = function(event) { + if (redo_button) redo_button.onclick = function(event) { if (dlg_dimmer === null) command(8); }; - document.getElementById("solve").onclick = function(event) { + if (solve_button) solve_button.onclick = function(event) { if (dlg_dimmer === null) command(9); }; @@ -396,7 +403,7 @@ function initPuzzle() { var free_save_file = Module.cwrap('free_save_file', 'void', ['number']); var load_game = Module.cwrap('load_game', 'void', ['string', 'number']); - document.getElementById("save").onclick = function(event) { + if (save_button) save_button.onclick = function(event) { if (dlg_dimmer === null) { var savefile_ptr = get_save_file(); var savefile_text = UTF8ToString(savefile_ptr); @@ -418,7 +425,7 @@ function initPuzzle() { } }; - document.getElementById("load").onclick = function(event) { + if (load_button) load_button.onclick = function(event) { if (dlg_dimmer === null) { var input = document.createElement("input"); input.type = "file";