Fixes #1337 - show explicit guidance when opened from file:// - route browser boot through a classic script gate - sanitize malformed generated app module before execution - trim duplicated footer junk and add regression tests
50 lines
1.3 KiB
JavaScript
50 lines
1.3 KiB
JavaScript
function setText(node, text) {
|
|
if (node) node.textContent = text;
|
|
}
|
|
|
|
function setHtml(node, html) {
|
|
if (node) node.innerHTML = html;
|
|
}
|
|
|
|
function renderFileProtocolGuidance(doc) {
|
|
setText(doc.querySelector('.loader-subtitle'), 'Serve this world over HTTP to initialize Three.js.');
|
|
const bootMessage = doc.getElementById('boot-message');
|
|
if (bootMessage) {
|
|
bootMessage.style.display = 'block';
|
|
setHtml(
|
|
bootMessage,
|
|
[
|
|
'<strong>Three.js modules cannot boot from <code>file://</code>.</strong>',
|
|
'Serve the Nexus over HTTP, for example:',
|
|
'<code>python3 -m http.server 8888</code>',
|
|
].join('<br>')
|
|
);
|
|
}
|
|
}
|
|
|
|
function injectModuleBootstrap(doc, src = './bootstrap.mjs') {
|
|
const script = doc.createElement('script');
|
|
script.type = 'module';
|
|
script.src = src;
|
|
doc.body.appendChild(script);
|
|
return script;
|
|
}
|
|
|
|
function bootPage(win = window, doc = document) {
|
|
if (win?.location?.protocol === 'file:') {
|
|
renderFileProtocolGuidance(doc);
|
|
return { mode: 'file' };
|
|
}
|
|
|
|
injectModuleBootstrap(doc);
|
|
return { mode: 'module' };
|
|
}
|
|
|
|
if (typeof window !== 'undefined' && typeof document !== 'undefined') {
|
|
bootPage(window, document);
|
|
}
|
|
|
|
if (typeof module !== 'undefined') {
|
|
module.exports = { bootPage, injectModuleBootstrap, renderFileProtocolGuidance };
|
|
}
|