Skip to content

Commit

Permalink
update upproach for handling scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
pseudopilot committed May 3, 2024
1 parent 3b3d27a commit b3b98d2
Showing 1 changed file with 7 additions and 25 deletions.
32 changes: 7 additions & 25 deletions client/public/runner.html
Original file line number Diff line number Diff line change
Expand Up @@ -54,25 +54,6 @@
window.console = consoleProxy;
window.addEventListener("error", (e) => console.log(e.error));

async function loadExternalScripts(html) {
const dummy = document.createElement("div");
dummy.innerHTML = html;
const scripts = dummy.querySelectorAll("script[src]");

const loaded = [...scripts].map(
(scrpt) =>
new Promise((resolve) => {
const script = document.createElement("script");
script.src = scrpt.src;
script.addEventListener("load", () => resolve());

document.head.appendChild(script);
})
);

await Promise.all(loaded);
}

function setHTML(parent, html) {
const dummy = parent.cloneNode(false);
dummy.innerHTML = html;
Expand All @@ -88,10 +69,6 @@
if (child.nodeType !== Node.ELEMENT_NODE) {
continue;
}
if (child.nodeName === "SCRIPT" && child.src) {
// Script tags with `src` are handled in `loadExternalScripts()`.
continue;
}

const namespaceURI = child.namespaceURI;

Expand Down Expand Up @@ -137,11 +114,16 @@
style.textContent = state.css;
document.head.appendChild(style);

await loadExternalScripts(state.html);

document.body.innerHTML = "";
setHTML(document.body, state.html);

for (const script of document.querySelectorAll("script[src]")) {
await new Promise((resolve) => {
script.addEventListener("load", () => resolve());
script.addEventListener("error", () => resolve());
});
}

const script = document.createElement("script");
script.textContent = state.js;
document.body.appendChild(script);
Expand Down

0 comments on commit b3b98d2

Please sign in to comment.