( src: string, defer?: boolean, force?: boolean, asModule?: boolean, contentWindow = window as Window )
| 315 | }; |
| 316 | |
| 317 | const 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 | |
| 352 | const loadStyle = ( |
| 353 | href: string, |
no test coverage detected