@@ -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
7983ace . config . set ( 'basePath' , 'https://cdn.jsdelivr.net/npm/ace-builds@1.10.0/src-noconflict/' ) ;
8084const 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 */
404408function 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} ) ;
425429location . 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" ) ;
469474if ( location . host . indexOf ( 'localhost' ) != - 1 ) {
470475 document . title += ' - localhost version' ;
471- $ ( 'main . heading' ) . textContent += ' - localhost version' ;
476+ heading . textContent += ' - localhost version' ;
472477}
473478else if ( location . host . indexOf ( '.github.dev' ) != - 1 ) {
474479 document . title += ' - codespace version' ;
475- $ ( 'main . heading' ) . textContent += ' - codespace version' ;
480+ heading . textContent += ' - codespace version' ;
476481}
477482else 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}
481486else {
482487 // we are in the full web version
0 commit comments