// loadinabox.js // // Reads parameters and then dynamically loads JavaScript and style-sheet // files from the location by appending script and style // children to the .. element in the required order. // // Copyright (C) 2011-2023 Mark G Daniel // // 06-OCT-2020 MGD v1.6.0, Allow F1..F12 to be VT220-style function keys // ?queryversion becomes time-based ?query // 14-JUL-2016 MGD allow for (kludge) version specifying query string // 11-JAN-2015 MGD integrate with ACME.JS // 03-SEP-2014 MGD getParameter() try{} // 10-JAN-2012 MGD initial //////////// // functions //////////// // generate an appropiate time period to refresh loaded files // production should be per-day, development per-minute function refreshEvery () { var date = new Date(); var query = '?' + (date.getMonth() * 30) + date.getDate(); // per day // var query = '?' + (date.getDate() * 24) + date.getHours(); // per hour // var query = '?' + (date.getHours() * 60) + date.getMinutes(); // per minute return query; } // get the value from the parent/opener, this window (config), or default // so local page-defined values override config file defined values function getParameter (name,value) { try { // order is significant; opener then parent then window if (opener && typeof opener[name] != 'undefined') window[name] = opener[name]; else if (parent && typeof parent[name] != 'undefined') window[name] = parent[name]; else if (typeof window[name] == 'undefined') window[name] = value; } catch (err) { // order is significant; parent then window if (parent && typeof parent[name] != 'undefined') window[name] = parent[name]; else if (typeof window[name] == 'undefined') window[name] = value; } } /////////////////////////////////////////////////////////////////////////////// /* Dynamically load a file. Multiple files are loaded serially IN-ORDER. Can be JavaScript (.js), style sheet (.css) or other (e.g. .html). Callback is optional. Non-JavaScript, non-style-sheet files must have an associated callback via which the content is delivered. */ if (typeof inabox_LoadFileName == 'undefined') { // do not reinitialise if this script is called multiple times var inabox_LoadFileName = []; var inabox_LoadFileCall = []; var inabox_LoadFileIndex = 0; } function inaboxLoadFile(filename,callback) { if (arguments.length) { inabox_LoadFileName.push(filename); if (typeof callback == 'undefined') callback = null; inabox_LoadFileCall.push(callback); if (inabox_LoadFileIndex < inabox_LoadFileName.length-1) return; } var fname = inabox_LoadFileName[inabox_LoadFileIndex]; // allows for a (kludge) version specifying query string (e.g. "?1.8.2") var ext = fname.split('?')[0].match(/\.[0-9a-z]+$/i); if (ext == '.js' || ext == '.css') { if (ext == '.js') { hndl = document.createElement('script'); hndl.setAttribute('type','text/javascript'); hndl.setAttribute('language','JavaScript'); hndl.setAttribute('src',fname); } else { hndl = document.createElement('link'); hndl.setAttribute("rel", "stylesheet"); hndl.setAttribute('type','text/css'); hndl.setAttribute('href',fname); } hndl.onerror = function() { alert('Failed to load: ' + fname); throw new Error('Failed to load: ' + fname); }; hndl.onload = function() { var callb = inabox_LoadFileCall[inabox_LoadFileIndex]; if (callb) { if (typeof callb == 'string') eval(callb); else callb(hndl.responseText); } if (++inabox_LoadFileIndex < inabox_LoadFileName.length) setTimeout(inaboxLoadFile,1); }; document.getElementsByTagName('head')[0].appendChild(hndl); } else { var hndl = new XMLHttpRequest(); hndl.open("GET",fname); hndl.onreadystatechange = function() { if (hndl.readyState == 4) { if (hndl.status == 200) { var callb = inabox_LoadFileCall[inabox_LoadFileIndex]; if (callb) { if (typeof callb == 'string') { // string must be in the form: (responseText); var responseText = hndl.responseText; eval(callb); } else callb(hndl.responseText); } if (++inabox_LoadFileIndex < inabox_LoadFileName.length) setTimeout(inaboxLoadFile,1); } else { alert('Failed to load: ' + fname); throw new Error('Failed to load: ' + fname); } } } hndl.send(); } } /////////////////////////////////////////////////////////////////////////////// /////////////// // in-line code /////////////// // get any locally configured document base getParameter('DCLinaboxBase','/dclinabox/-/'); // set the default base href dynamically var base = document.createElement('base'); base.href = DCLinaboxBase; document.getElementsByTagName('head')[0].appendChild(base); // check if the VT100 object exists if (document.getElementById('vt100')) { //////////////////// // initiate terminal //////////////////// // any locally specified configuration file getParameter('DCLinaboxConfig',''); // any locally specified style file getParameter('DCLinaboxStyle',''); // if specified load the local configuration JavaScript if (DCLinaboxConfig.length) inaboxLoadFile(DCLinaboxConfig); // load the ShellInABox styles inaboxLoadFile('styles.css' + refreshEvery()); // load the DCLinabox styles inaboxLoadFile('dclinabox.css' + refreshEvery()); // if specified load the DCLinabox customised styles if (DCLinaboxStyle.length) inaboxLoadFile(DCLinaboxStyle + refreshEvery()); // load the virtual keyboard resources inaboxLoadFile('vkb_lkish.css' + refreshEvery()); inaboxLoadFile('vkb_lkish.js' + refreshEvery()); // load the VT100 emulator core JavaScript inaboxLoadFile('vt100.js' + refreshEvery()); // now load the main DCLinabox code inaboxLoadFile('DCLinabox.js' + refreshEvery()); } else { ///////////////////////////// // acme (aLaMode and MonDeSi) ///////////////////////////// // load the terminal HTML infrastructure inaboxLoadFile('DCLinabox.html',function(html) { // replacing the .. of the DCLINABOX.EXE page var html2 = html.split(''); html2 = html2[1].split(''); document.getElementsByTagName('body')[0].innerHTML = html2[0]; // kick off the terminal by loading the JavaScript inaboxLoadFile('loadinabox.js'); }); } ////// // end //////