MCPcopy Index your code
hub / github.com/MALSync/MALSync / injectScript

Method injectScript

src/utils/scriptProxy.ts:53–93  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

51 }
52
53 async injectScript() {
54 let loaded: () => void;
55
56 const uniqueId = generateUniqueID();
57
58 const prom = new Promise((resolve, reject) => {
59 const timeout = setTimeout(() => {
60 reject(new Error('ScriptProxy timed out'));
61 }, 5000);
62 loaded = () => {
63 clearTimeout(timeout);
64 resolve(true);
65 };
66 });
67
68 const callbackFunction = (event: Event) => {
69 if (!(event instanceof MessageEvent)) {
70 return;
71 }
72
73 const eventData: MessageEvent = event;
74
75 if (eventData.data.uniqueId !== uniqueId) return;
76
77 window.removeEventListener('message', callbackFunction);
78 con.m('ScriptProxy').log('Script Loaded');
79 loaded();
80 };
81
82 window.addEventListener('message', callbackFunction, false);
83
84 const scriptElement = document.createElement('script');
85 api.storage.addProxyScriptToTag(scriptElement, this.scriptName);
86 scriptElement.id = this.elementId;
87 scriptElement.setAttribute(`data-${this.elementId}`, uniqueId);
88 document.documentElement.appendChild(scriptElement);
89
90 con.m('ScriptProxy').log('Script Added');
91
92 return prom;
93 }
94}

Callers 4

initFunction · 0.80
initFunction · 0.80
initFunction · 0.80
initFunction · 0.80

Calls 2

generateUniqueIDFunction · 0.90
addProxyScriptToTagMethod · 0.80

Tested by

no test coverage detected