Skip to content

Commit 99a9ec9

Browse files

File tree

4 files changed

+43
-28
lines changed

4 files changed

+43
-28
lines changed

index.html

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,23 @@
3030
"\nline: " + (lineno || "unknown") +
3131
"\ncolumn: " + (colno || "unknown") +
3232
"\nfile: " + (source || "unknown") +
33-
"\ntraceback:\n" + error.stack
33+
"\ntraceback: " + (error == null ? "not available (" + event + " ??)" : error.stack)
3434
);
35+
fetch("/report_error/" + encodeURIComponent(err));
3536
document.documentElement.innerHTML = "Fatal error: please copy and <a href=\"https://github.com/dragoncoder047/langton-music/issues\">report</a>: <pre>" + err + "</pre>";
3637
}
3738
catch(e) {
3839
document.documentElement.innerHTML = "Double fatal error: please copy and <a href=\"https://github.com/dragoncoder047/langton-music/issues\">report</a>: <pre>" + e.stack + "</pre>";
40+
throw e;
3941
}
42+
throw error;
4043
}
4144
</script>
4245
</head>
4346

4447
<body>
4548
<main class="flex-column">
46-
<div class="heading padding">Langton's Ant Music version 3-DEV</div>
49+
<div class="heading padding" id="foohead">Langton's Ant Music version 3-DEV</div>
4750
<div class="flex-row padding">
4851
<div class="expanding" id="statuswrapper"><output id="statusbar"></output></div>
4952
<a href="#editor">Edit</a>&nbsp;&nbsp;-&nbsp;&nbsp;
@@ -622,7 +625,7 @@ <h1>Keyboard Shortcuts</h1>
622625
<script src="js/world.js"></script>
623626
<script src="js/actions.js"></script>
624627
<script src="js/media.js"></script>
625-
<script src="js/main.js"></script>
628+
<script src="js/main_load.js"></script>
626629
</body>
627630

628631
</html>

js/main.js

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,72 +9,76 @@ const TOO_MANY_ANTS = 64;
99
* @param {string} s Selector
1010
* @returns {HTMLElement}
1111
*/
12-
const $ = s => document.querySelector(s);
12+
const safe$ = selector => {
13+
var elem = document.querySelector(selector);
14+
if (!elem) throw new Error("can't find " + selector);
15+
return elem;
16+
};
1317

1418
/**
1519
* @type {HTMLCanvasElement}
1620
*/
17-
const playfield = $('#playfield');
21+
const playfield = safe$('#playfield');
1822
/**
1923
* @type {HTMLButtonElement}
2024
*/
21-
const startStopBtn = $('#startstop');
25+
const startStopBtn = safe$('#startstop');
2226
/**
2327
* @type {HTMLButtonElement}
2428
*/
25-
const stepBtn = $('#step');
29+
const stepBtn = safe$('#step');
2630
/**
2731
* @type {HTMLOutputElement}
2832
*/
29-
const stepCounter = $('#stepnum');
33+
const stepCounter = safe$('#stepnum');
3034
/**
3135
* @type {HTMLInputElement}
3236
*/
33-
const speedSlider = $('#speedslider');
37+
const speedSlider = safe$('#speedslider');
3438
/**
3539
* @type {HTMLInputElement}
3640
*/
37-
const speedBox = $('#speedbox');
41+
const speedBox = safe$('#speedbox');
3842
/**
3943
* @type {HTMLInputElement}
4044
*/
41-
const muteCheckbox = $('#mutecheck');
45+
const muteCheckbox = safe$('#mutecheck');
4246
/**
4347
* @type {HTMLOutputElement}
4448
*/
45-
const antsCounter = $('#antscount');
49+
const antsCounter = safe$('#antscount');
4650
/**
4751
* @type {HTMLButtonElement}
4852
*/
49-
const loadBtn = $('#loadbtn');
53+
const loadBtn = safe$('#loadbtn');
5054
/**
5155
* @type {HTMLButtonElement}
5256
*/
53-
const dumpBtn = $('#dumpbtn');
57+
const dumpBtn = safe$('#dumpbtn');
5458
/**
5559
* @type {HTMLOutputElement}
5660
*/
57-
const statusBar = $('#statusbar');
61+
const statusBar = safe$('#statusbar');
5862
/**
5963
* @type {HTMLButtonElement}
6064
*/
61-
const fitBtn = $('#fit');
65+
const fitBtn = safe$('#fit');
6266
/**
6367
* @type {HTMLInputElement}
6468
*/
65-
const autoFit = $('#autofit');
69+
const autoFit = safe$('#autofit');
6670
/**
6771
* @type {HTMLSelectElement}
6872
*/
69-
const followSelector = $('#follow');
73+
const followSelector = safe$('#follow');
7074
/**
7175
* @type {HTMLSelectElement}
7276
*/
73-
const actionsSelector = $('#actions');
77+
const actionsSelector = safe$('#actions');
7478
/**
7579
* @type {HTMLDivElement}
7680
*/
77-
const debugBar = $('#debugbar');
81+
const debugBar = safe$('#debugbar');
7882

7983
ace.config.set('basePath', 'https://cdn.jsdelivr.net/npm/[email protected]/src-noconflict/');
8084
const textbox = ace.edit('textbox', { mode: 'ace/mode/xml' });
@@ -106,7 +110,7 @@ var world = new World();
106110
/**
107111
* @type {CanvasToolsManager}
108112
*/
109-
var canvasTools = new CanvasToolsManager(playfield, $('#toolselect'), $('#tooloption'), [
113+
var canvasTools = new CanvasToolsManager(playfield, safe$('#toolselect'), safe$('#tooloption'), [
110114
new DragTool(),
111115
new DrawCellsTool(world),
112116
new DrawAntsTool(world, breeder, ants),
@@ -403,8 +407,8 @@ dumpBtn.addEventListener('click', () => actions.trigger('dump'));
403407
*/
404408
function fitace() {
405409
setTimeout(() => {
406-
var rect = $('#textbox').parentElement.getBoundingClientRect();
407-
$('#textbox').setAttribute('style', `width:${rect.width}px;height:${rect.height}px`);
410+
var rect = safe$('#textbox').parentElement.getBoundingClientRect();
411+
safe$('#textbox').setAttribute('style', `width:${rect.width}px;height:${rect.height}px`);
408412
textbox.resize(true);
409413
}, 0);
410414
}
@@ -419,7 +423,7 @@ window.addEventListener('hashchange', () => {
419423
where = '#dumpstatuswrapper';
420424
textbox.setTheme(DARK_MODE ? 'ace/theme/pastel_on_dark' : 'ace/theme/chrome');
421425
}
422-
$(where).append(statusBar);
426+
safe$(where).append(statusBar);
423427
fitace();
424428
});
425429
location.hash = "#"; // Don't have editor open by default
@@ -466,17 +470,18 @@ if ("serviceWorker" in navigator) {
466470
}
467471

468472
// Dev version indicator
473+
const heading = safe$("#foohead");
469474
if (location.host.indexOf('localhost') != -1) {
470475
document.title += ' - localhost version';
471-
$('main .heading').textContent += ' - localhost version';
476+
heading.textContent += ' - localhost version';
472477
}
473478
else if (location.host.indexOf('.github.dev') != -1) {
474479
document.title += ' - codespace version';
475-
$('main .heading').textContent += ' - codespace version';
480+
heading.textContent += ' - codespace version';
476481
}
477482
else if (location.protocol.indexOf('file') != -1) {
478483
document.title += ' - file:// version';
479-
$('main .heading').textContent += ' - file:// version (some features unavailable)';
484+
heading.textContent += ' - file:// version (some features unavailable)';
480485
}
481486
else {
482487
// we are in the full web version

js/main_load.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
window.addEventListener("DOMContentLoaded", () => {
2+
var s = document.createElement("script");
3+
s.src = "js/main.js";
4+
document.body.append(s);
5+
});

serve

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ if [[ "$USER" -eq codespace ]]
33
then
44
echo "Using Flask to serve http://localhost:4443/ -> port forwarding"
55
sudo python3 <<PYTHON
6-
import flask, os
6+
import flask, os, urllib
77
app = flask.Flask("test")
88
@app.route("/")
99
def main():return flask.redirect("/index.html")
1010
@app.route("/<path:file>")
1111
def files(file):return flask.send_from_directory(os.getcwd(), file, max_age=0)
12+
@app.route("/report_error/<string:err>")
13+
def errror(err):print(urllib.parse.unquote(err),end="\n\n");return "",500
1214
app.run("localhost", 4443, ssl_context="adhoc")
1315
PYTHON
1416
else

0 commit comments

Comments
 (0)