UNPKG

@trilitech-umami/umami-embed

Version:

[WIP - not ready for production use] A simple embeddable Umami wallet

70 lines (69 loc) 2.18 kB
const IFRAME_ID = "umami-embed-iframe"; export class UmamiIframe { constructor() { Object.defineProperty(this, "iframe", { enumerable: true, configurable: true, writable: true, value: null }); Object.defineProperty(this, "iframeUrl", { enumerable: true, configurable: true, writable: true, value: "" }); } get isInitialized() { return this.iframe !== null; } init(iframeParent, iframeUrl) { if (!this.isInitialized) { this.iframeUrl = iframeUrl; this.iframe = document.createElement("iframe"); this.iframe.setAttribute("sandbox", "allow-scripts allow-same-origin allow-popups allow-popups-to-escape-sandbox"); this.iframe.src = this.iframeUrl; this.iframe.id = IFRAME_ID; this.iframe.style.position = "fixed"; this.iframe.style.top = "0px"; this.iframe.style.left = "0px"; this.iframe.style.borderRadius = "0px"; this.iframe.style.border = "0"; this.iframe.style.zIndex = "99999"; this.iframe.style.background = "transparent"; this.hide(); iframeParent.appendChild(this.iframe); } else { throw new Error("Umami iFrame is already initialized"); } } destroy() { if (this.iframe) { this.iframe.remove(); this.iframe = null; } } show() { if (this.iframe) { this.iframe.style.width = "100%"; this.iframe.style.height = "100%"; this.iframe.style.display = "block"; } } hide() { if (this.iframe) { this.iframe.style.width = "0"; this.iframe.style.height = "0"; this.iframe.style.display = "none"; } } request(message) { if (this.iframe) { this.iframe.contentWindow?.postMessage(JSON.stringify(message), this.iframeUrl); } else { throw new Error("Umami iFrame is not initialized"); } } }