MCPcopy Create free account
hub / github.com/DustinBrett/daedalOS / loadScript

Function loadScript

utils/functions.ts:317–350  ·  view source on GitHub ↗
(
  src: string,
  defer?: boolean,
  force?: boolean,
  asModule?: boolean,
  contentWindow = window as Window
)

Source from the content-addressed store, hash-verified

315};
316
317const loadScript = (
318 src: string,
319 defer?: boolean,
320 force?: boolean,
321 asModule?: boolean,
322 contentWindow = window as Window
323): Promise<Event> =>
324 new Promise((resolve, reject) => {
325 const loadedScripts = [...contentWindow.document.scripts];
326 const currentScript = loadedScripts.find((loadedScript) =>
327 loadedScript.src.endsWith(src)
328 );
329
330 if (currentScript) {
331 if (!force) {
332 resolve(new Event("Already loaded."));
333 return;
334 }
335
336 currentScript.remove();
337 }
338
339 const script = contentWindow.document.createElement("script");
340
341 script.async = false;
342 if (defer) script.defer = true;
343 if (asModule) script.type = "module";
344 script.fetchPriority = "high";
345 script.src = src;
346 script.addEventListener("error", reject, ONE_TIME_PASSIVE_EVENT);
347 script.addEventListener("load", resolve, ONE_TIME_PASSIVE_EVENT);
348
349 contentWindow.document.head.append(script);
350 });
351
352const loadStyle = (
353 href: string,

Callers 1

loadFilesFunction · 0.70

Calls 6

createElementMethod · 0.80
resolveFunction · 0.50
findMethod · 0.45
removeMethod · 0.45
addEventListenerMethod · 0.45
appendMethod · 0.45

Tested by

no test coverage detected