MCPcopy Index your code
hub / github.com/ONLYOFFICE/document-editor-react / loadScript

Function loadScript

src/utils/loadScript.ts:17–54  ·  view source on GitHub ↗
(url: string, id: string)

Source from the content-addressed store, hash-verified

15*/
16
17const loadScript = async (url: string, id: string) => {
18 return new Promise((resolve, reject) => {
19 try {
20 // If DocsAPI is defined return resolve.
21 if (window.DocsAPI) return resolve(null);
22
23 const existedScript = document.getElementById(id);
24
25 if (existedScript) {
26 // If the script element is found, wait for it to load.
27 let intervalHandler = setInterval(() => {
28 const loading = existedScript.getAttribute("loading");
29 if (loading) {
30 // If the download is not completed, continue to wait.
31 return;
32 } else {
33 // If the download is completed, stop the wait.
34 clearInterval(intervalHandler);
35
36 // If DocsAPI is defined, after loading return resolve.
37 if (window.DocsAPI) return resolve(null);
38
39 // If DocsAPI is not defined, delete the existing script and create a new one.
40 const script = _createScriptTag(id, url, resolve, reject);
41 existedScript.remove();
42 document.body.appendChild(script);
43 }
44 }, 500);
45 } else {
46 // If the script element is not found, create it.
47 const script = _createScriptTag(id, url, resolve, reject);
48 document.body.appendChild(script);
49 }
50 } catch (e) {
51 console.error(e);
52 }
53 });
54};
55
56const _createScriptTag = (id: string, url: string, resolve: (value: unknown) => void, reject: (reason?: any) => void) => {
57 const script = document.createElement("script");

Callers 1

DocumentEditorFunction · 0.85

Calls 1

_createScriptTagFunction · 0.85

Tested by

no test coverage detected