From f67a3ffb1993b7ab879722f29d41dffac45efd80 Mon Sep 17 00:00:00 2001 From: Kuba Orlik Date: Thu, 4 Jul 2024 15:36:29 +0200 Subject: [PATCH] Fix error when no scripts in custom html --- src/postponeScript.ts | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/src/postponeScript.ts b/src/postponeScript.ts index 000f029..57e3637 100644 --- a/src/postponeScript.ts +++ b/src/postponeScript.ts @@ -60,13 +60,13 @@ so, for a simple `console.log('hello')` script that needs scripts */ - - - -export function postponeScript(idsOfScriptsToAwait: string[], script_content: string) { - return `{const loaded = ${JSON.stringify( - Object.fromEntries(idsOfScriptsToAwait.map(id => [id, false])) - )}; +export function postponeScript( + idsOfScriptsToAwait: string[], + script_content: string +) { + return `{const loaded = ${JSON.stringify( + Object.fromEntries(idsOfScriptsToAwait.map(id => [id, false])) + )}; let called = false; const call_if_ready = () => { @@ -76,11 +76,13 @@ export function postponeScript(idsOfScriptsToAwait: string[], script_content: st } }; - ${idsOfScriptsToAwait.map( - id => `document.addEventListener( + ${idsOfScriptsToAwait + .map( + id => `document.addEventListener( "loaded-${id}", ()=>{ loaded["${id}"] = true; call_if_ready() - }).join(";")` - )}}`; + })` + ) + .join(';')}}` }