cal.pub0.org/packages/embeds/embed-snippet/src/index.ts

56 lines
1.6 KiB
TypeScript

/* eslint-disable @typescript-eslint/ban-ts-comment,prefer-rest-params,prefer-const */
/**
* As we want to keep control on the size of this snippet but we want some portion of it to be still readable.
* So, write the code that you need directly but keep it short.
*/
import { CalWindow } from "@calcom/embed-core";
const WEBAPP_URL =
import.meta.env.EMBED_PUBLIC_WEBAPP_URL || `https://${import.meta.env.EMBED_PUBLIC_VERCEL_URL}`;
const EMBED_LIB_URL = import.meta.env.EMBED_PUBLIC_EMBED_LIB_URL || `${WEBAPP_URL}/embed/embed.js`;
export default function EmbedSnippet(url = EMBED_LIB_URL) {
(function (C: CalWindow, A, L) {
let p = function (a: any, ar: any) {
a.q.push(ar);
};
let d = C.document;
C.Cal =
C.Cal ||
function () {
let cal = C.Cal!;
let ar = arguments;
if (!cal.loaded) {
cal.ns = {};
cal.q = cal.q || [];
//@ts-ignore
d.head.appendChild(d.createElement("script")).src = A;
cal.loaded = true;
}
if (ar[0] === L) {
const api: { (): void; q: any[] } = function () {
p(api, arguments);
};
const namespace = ar[1];
api.q = api.q || [];
typeof namespace === "string" ? (cal.ns![namespace] = api) && p(api, ar) : p(cal, ar);
return;
}
p(cal, ar);
};
})(
window,
//! Replace it with "https://cal.com/embed.js" or the URL where you have embed.js installed
url,
"init"
);
/*! Copying ends here. */
return (window as CalWindow).Cal;
}
export const EmbedSnippetString = EmbedSnippet.toString();