diff --git a/src/utils.ts b/src/utils.ts index 5f3795b4..7e23050f 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -164,4 +164,27 @@ export async function loadTemplates(url: string): Promise { let templates = await result.text(); templates = templates.replace(//g, ""); return templates; -} \ No newline at end of file +} + +const loadedScripts: { [key: string]: Promise } = {}; + +export function loadJS(url: string): Promise { + if (url in loadedScripts) { + return loadedScripts[url]; + } + const promise: Promise = new Promise(function(resolve, reject) { + const script = document.createElement("script"); + script.type = "text/javascript"; + script.src = url; + script.onload = function() { + resolve(); + }; + script.onerror = function() { + reject(`Error loading file '${url}'`); + }; + const head = document.head || document.getElementsByTagName("head")[0]; + head.appendChild(script); + }); + loadedScripts[url] = promise; + return promise; +}