From 8ba95b23a3fee93f2235eda2809002437f02a521 Mon Sep 17 00:00:00 2001 From: PseudoPilot <40791399+pseudopilot@users.noreply.github.com> Date: Wed, 8 May 2024 19:47:18 +0100 Subject: [PATCH] fix(playground/runner): fix external script loading in playground (#11017) * fix(playground/runner): fix external script loading in playground * refactor mapping Co-authored-by: Claas Augner <495429+caugner@users.noreply.github.com> * add comment to make clear Co-authored-by: Claas Augner <495429+caugner@users.noreply.github.com> * fix code formatting * update upproach for handling scripts * code refactoring Co-authored-by: Claas Augner <495429+caugner@users.noreply.github.com> --------- Co-authored-by: Claas Augner <495429+caugner@users.noreply.github.com> Co-authored-by: Florian Dieminger --- client/public/runner.html | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/client/public/runner.html b/client/public/runner.html index a6b75d798221..6b534bdc60f1 100644 --- a/client/public/runner.html +++ b/client/public/runner.html @@ -104,7 +104,7 @@ } let initialized = false; - function init(state) { + async function init(state) { if (initialized) { return; } @@ -117,6 +117,13 @@ 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); @@ -129,10 +136,10 @@ initialized = true; } - window.addEventListener("message", (event) => { + window.addEventListener("message", async (event) => { const e = event.data; if (e.typ === "init") { - init(e.state); + await init(e.state); } if (e.typ === "reload") { window.location.reload();