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

Function ScriptProxyWrapper

src/utils/scriptProxyWrapper.ts:10–48  ·  view source on GitHub ↗
(fnc: () => void)

Source from the content-addressed store, hash-verified

8}
9
10export function ScriptProxyWrapper(fnc: () => void) {
11 const tag = document.currentScript as HTMLScriptElement;
12 const idAttribute = tag.getAttribute('id')!;
13 const logger = con.m('ScriptProxyWrapper');
14
15 window.addEventListener(
16 'message',
17 event => {
18 if (!(event instanceof MessageEvent)) {
19 return;
20 }
21
22 const eventData: MessageEvent = event;
23
24 if (!eventData.data.eventId || eventData.data.resultId) return;
25
26 const resultId = generateUniqueID();
27 const res = fnc();
28
29 const scriptElement = document.createElement('script');
30 scriptElement.id = resultId;
31 scriptElement.setAttribute(
32 `data-${eventData.data.eventId}`,
33 JSON.stringify({
34 [idAttribute]: res,
35 }),
36 );
37 document.documentElement.appendChild(scriptElement);
38
39 window.postMessage({ eventId: eventData.data.eventId, resultId }, '*');
40 },
41 false,
42 );
43
44 window.postMessage({ uniqueId: tag.getAttribute(`data-${idAttribute}`) }, '*');
45
46 logger.log('Listener Added');
47 tag.remove();
48}

Callers

nothing calls this directly

Calls 2

generateUniqueIDFunction · 0.85
removeMethod · 0.80

Tested by

no test coverage detected