@whop/embedded-components-vanilla-js
Version:
Whop Elements loading utility
1 lines • 5.93 kB
Source Map (JSON)
{"version":3,"sources":["../src/util.ts"],"sourcesContent":["import type { WhopElements, WhopElementsConstructor } from \"./types\";\nimport { ORIGIN, SCRIPT_URL } from \"./url\";\n\nexport type LoadWhopElements = (\n\t...args: ConstructorParameters<WhopElementsConstructor>\n) => Promise<WhopElements | null>;\n\nconst isWhopElementsScriptURL = (url: string): boolean => {\n\tconst result = url.startsWith(ORIGIN) && url.endsWith(\"/elements.js\");\n\treturn result;\n};\n\nexport const findScript = (): HTMLScriptElement | null => {\n\tconst scripts = document.querySelectorAll<HTMLScriptElement>(\n\t\t`script[src^=\"${ORIGIN}\"]`,\n\t);\n\n\tfor (const script of scripts) {\n\t\tif (!script || !isWhopElementsScriptURL(script.src)) {\n\t\t\tcontinue;\n\t\t}\n\t\treturn script;\n\t}\n\treturn null;\n};\n\nconst injectScript = (): HTMLScriptElement => {\n\tconst script = document.createElement(\"script\");\n\tscript.src = SCRIPT_URL;\n\n\tconst headOrBody = document.head || document.body;\n\n\tif (!headOrBody) {\n\t\tthrow new Error(\n\t\t\t\"Expected document.body not to be null. WhopElements requires a <body> element.\",\n\t\t);\n\t}\n\n\theadOrBody.appendChild(script);\n\n\treturn script;\n};\n\nlet whopElementsPromise: Promise<WhopElementsConstructor | null> | null = null;\n\nlet onErrorListener: ((cause?: unknown) => void) | null = null;\nlet onLoadListener: (() => void) | null = null;\n\nconst onError = (reject: (reason?: unknown) => void) => (cause?: unknown) => {\n\treject(new Error(\"Failed to load WhopElements\", { cause }));\n};\n\nconst onLoad =\n\t(\n\t\tresolve: (\n\t\t\tvalue:\n\t\t\t\t| WhopElementsConstructor\n\t\t\t\t| PromiseLike<WhopElementsConstructor | null>\n\t\t\t\t| null,\n\t\t) => void,\n\t\treject: (reason?: unknown) => void,\n\t) =>\n\t() => {\n\t\tif (window.WhopElements) {\n\t\t\tresolve(window.WhopElements);\n\t\t} else {\n\t\t\treject(new Error(\"WhopElements not available\"));\n\t\t}\n\t};\n\nexport const loadScript = (): Promise<WhopElementsConstructor | null> => {\n\tif (whopElementsPromise !== null) {\n\t\treturn whopElementsPromise;\n\t}\n\n\twhopElementsPromise = new Promise((resolve, reject) => {\n\t\tif (typeof window === \"undefined\" || typeof document === \"undefined\") {\n\t\t\tresolve(null);\n\t\t\treturn;\n\t\t}\n\n\t\tif (window.WhopElements) {\n\t\t\tresolve(window.WhopElements);\n\t\t\treturn;\n\t\t}\n\n\t\ttry {\n\t\t\tlet script = findScript();\n\n\t\t\tif (!script) {\n\t\t\t\tscript = injectScript();\n\t\t\t} else if (\n\t\t\t\tscript &&\n\t\t\t\tonLoadListener !== null &&\n\t\t\t\tonErrorListener !== null\n\t\t\t) {\n\t\t\t\t// remove event listeners\n\t\t\t\tscript.removeEventListener(\"load\", onLoadListener);\n\t\t\t\tscript.removeEventListener(\"error\", onErrorListener);\n\n\t\t\t\t// if script exists, but we are reloading due to an error,\n\t\t\t\t// reload script to trigger 'load' event\n\t\t\t\tscript.parentNode?.removeChild(script);\n\t\t\t\tscript = injectScript();\n\t\t\t}\n\n\t\t\tonLoadListener = onLoad(resolve, reject);\n\t\t\tonErrorListener = onError(reject);\n\t\t\tscript.addEventListener(\"load\", onLoadListener);\n\t\t\tscript.addEventListener(\"error\", onErrorListener);\n\t\t} catch (error) {\n\t\t\tif (error instanceof Error) {\n\t\t\t\treject(error);\n\t\t\t} else {\n\t\t\t\treject(new Error(\"Failed to load WhopElements\", { cause: error }));\n\t\t\t}\n\t\t\treturn;\n\t\t}\n\t});\n\n\treturn whopElementsPromise.catch((error) => {\n\t\twhopElementsPromise = null;\n\t\tif (error instanceof Error) {\n\t\t\treturn Promise.reject(error);\n\t\t} else {\n\t\t\treturn Promise.reject(\n\t\t\t\tnew Error(\"Failed to load WhopElements\", { cause: error }),\n\t\t\t);\n\t\t}\n\t});\n};\n\nexport const initWhopElements = (\n\tmaybeWhopElements: WhopElementsConstructor | null,\n\targs: ConstructorParameters<WhopElementsConstructor>,\n): WhopElements | null => {\n\tif (maybeWhopElements === null) {\n\t\treturn null;\n\t}\n\tconst WhopElements = maybeWhopElements;\n\n\treturn new WhopElements(...args);\n};\n"],"mappings":"AACA,SAAS,QAAQ,kBAAkB;AAMnC,MAAM,0BAA0B,CAAC,QAAyB;AACzD,QAAM,SAAS,IAAI,WAAW,MAAM,KAAK,IAAI,SAAS,cAAc;AACpE,SAAO;AACR;AAEO,MAAM,aAAa,MAAgC;AACzD,QAAM,UAAU,SAAS;AAAA,IACxB,gBAAgB,MAAM;AAAA,EACvB;AAEA,aAAW,UAAU,SAAS;AAC7B,QAAI,CAAC,UAAU,CAAC,wBAAwB,OAAO,GAAG,GAAG;AACpD;AAAA,IACD;AACA,WAAO;AAAA,EACR;AACA,SAAO;AACR;AAEA,MAAM,eAAe,MAAyB;AAC7C,QAAM,SAAS,SAAS,cAAc,QAAQ;AAC9C,SAAO,MAAM;AAEb,QAAM,aAAa,SAAS,QAAQ,SAAS;AAE7C,MAAI,CAAC,YAAY;AAChB,UAAM,IAAI;AAAA,MACT;AAAA,IACD;AAAA,EACD;AAEA,aAAW,YAAY,MAAM;AAE7B,SAAO;AACR;AAEA,IAAI,sBAAsE;AAE1E,IAAI,kBAAsD;AAC1D,IAAI,iBAAsC;AAE1C,MAAM,UAAU,CAAC,WAAuC,CAAC,UAAoB;AAC5E,SAAO,IAAI,MAAM,+BAA+B,EAAE,MAAM,CAAC,CAAC;AAC3D;AAEA,MAAM,SACL,CACC,SAMA,WAED,MAAM;AACL,MAAI,OAAO,cAAc;AACxB,YAAQ,OAAO,YAAY;AAAA,EAC5B,OAAO;AACN,WAAO,IAAI,MAAM,4BAA4B,CAAC;AAAA,EAC/C;AACD;AAEM,MAAM,aAAa,MAA+C;AACxE,MAAI,wBAAwB,MAAM;AACjC,WAAO;AAAA,EACR;AAEA,wBAAsB,IAAI,QAAQ,CAAC,SAAS,WAAW;AACtD,QAAI,OAAO,WAAW,eAAe,OAAO,aAAa,a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