From 381534ee97753adad312b35b49bd4cd6f802cad0 Mon Sep 17 00:00:00 2001 From: Nicholas Jitkoff Date: Tue, 27 Dec 2022 23:44:40 -0800 Subject: [PATCH] Start to handle html overwrite, add download sandbox dynamically --- docs/index.js | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/docs/index.js b/docs/index.js index ac9102a..4e2a70e 100644 --- a/docs/index.js +++ b/docs/index.js @@ -352,10 +352,11 @@ let dataContent = durl.rawData; if (!dataURL) return; - iframe.sandbox = "allow-same-origin allow-downloads allow-scripts allow-forms allow-top-navigation allow-popups allow-modals allow-popups-to-escape-sandbox"; + iframe.sandbox = "allow-same-origin allow-scripts allow-forms allow-top-navigation allow-popups allow-modals allow-popups-to-escape-sandbox"; + if (iframe.sandbox.supports('allow-downloads')) iframe.sandbox.add('allow-downloads'); if (isIE && renderMode == "data") renderMode = "frame"; - let overwriteSelf = isWatch; + let overwriteSelf = isWatch && !params.script.endsWith(".html"); console.log("🖋 Rendering mode: " + "\x1B[1m" + renderMode, {url:durl}) @@ -406,14 +407,27 @@ if (params.script.indexOf("/") == -1) params.script = location.origin + '/render/' + params.script + '.js' if (params.overwrite) { - // Overwrite the current page with the script. Dangerous, but required for browsers on apple watch to scroll. - let scriptEl = document.createElement("script") - scriptEl.src = "/render.js" - scriptEl.addEventListener('load', function(e) { - console.log("Loaded script", scriptEl.src); - renderScriptContent(params, "*"); - }); - document.head.appendChild(scriptEl); + + if (params.script.endsWith(".html")) { + fetch(params.script, /*, options */) + .then((response) => response.text()) + .then((html) => { + console.log("html", html) + document.documentElement.innerHTML = html; + }) + .catch((error) => { + console.warn(error); + }); + } else { + // Overwrite the current page with the script. Dangerous, but required for browsers on apple watch to scroll. + let scriptEl = document.createElement("script") + scriptEl.src = "/render.js" + scriptEl.addEventListener('load', function(e) { + console.log("Loaded script", scriptEl.src); + renderScriptContent(params, "*"); + }); + document.head.appendChild(scriptEl); + } } else { // Render in an iframe, either via sandboxed subdomain or a data url (disables storage APIs) iframe.onload = (() => {