UNPKG

aquaculture-chatbot-widget

Version:

A customizable chatbot widget for aquaculture consultation

33 lines (32 loc) 1.51 kB
var o = Object.defineProperty; var h = (e, i, t) => i in e ? o(e, i, { enumerable: !0, configurable: !0, writable: !0, value: t }) : e[i] = t; var s = (e, i, t) => h(e, typeof i != "symbol" ? i + "" : i, t); class r { constructor(i = {}) { s(this, "config"); s(this, "iframe"); this.config = i, this.iframe = document.createElement("iframe"), this.setupIframe(); } setupIframe() { var i, t; this.iframe.style.position = "fixed", this.iframe.style.bottom = ((i = this.config.position) == null ? void 0 : i.bottom) || "20px", this.iframe.style.right = ((t = this.config.position) == null ? void 0 : t.right) || "20px", this.iframe.style.border = "none", this.iframe.style.width = "350px", this.iframe.style.height = "500px", this.iframe.style.maxHeight = "80vh", this.iframe.style.zIndex = "1000", this.iframe.style.display = "block", this.iframe.style.backgroundColor = "transparent", this.iframe.src = "YOUR_DEPLOYED_WIDGET_URL", window.addEventListener("message", this.handleMessage.bind(this)); } handleMessage(i) { var t; if (i.origin === new URL(this.iframe.src).origin) { const { type: a } = i.data; switch (a) { case "CHATBOT_READY": (t = this.iframe.contentWindow) == null || t.postMessage({ type: "CONFIG", data: this.config }, "*"); break; } } } mount(i) { i ? i.appendChild(this.iframe) : document.body.appendChild(this.iframe); } unmount() { this.iframe.remove(); } } window.ChatbotWidget = r;