UNPKG

@sheerid/jslib-nightly

Version:

SheerID JavaScript Library

350 lines (348 loc) • 12 kB
/** * VERSION: 2.230.0-alpha.0 * BUILD_TIMESTAMP: 1778694696091 * BUILD_DATE: Wed May 13 2026 17:51:36 GMT+0000 (Coordinated Universal Time) * BUILD_COMMIT: 7af1c105d5485ac4ed1190c56d492bb4590667a9 */ const b = {}, g = async () => b, _ = () => window.navigator.globalPrivacyControl, c = () => window.NREUM && !_(); let d = 4, r = ""; const u = { info: 1, log: 2, warn: 3, error: 4, silent: 5 }, h = { info: "#26c1db", log: "#09f979", warn: "#f6b13f", error: "#e12046" }, f = "color: white; font-weight: bold; padding: 2px 10px;"; let L = () => ({}); const T = (i) => i && i.stack && i.message, m = (...i) => { d <= 3 && console.warn(`%c${r} warn`, `background: ${h.warn};${f}`, ...i); }, M = (...i) => { d <= 2 && console.log(`%c${r} log`, `background: ${h.log};${f}`, ...i); }, U = (...i) => { d <= 1 && console.log(`%c${r} info`, `background: ${h.info};${f}`, ...i); }, A = async (i, e = "unknown group", t = {}) => { if (d <= 4) { if (!i) { console.error(new Error("An error must be supplied")); return; } let s; typeof i == "string" && (s = new Error(i)), T(i) && (s = i), s || (s = new Error("Unknown error")); let o = { errorMessageGroup: e }; try { o = { ...await g(), ...o }, delete o.jslibVersionActual; } catch (a) { c() ? window.NREUM.noticeError(s, o) : m("Unable to assemble GA error attributes", a); } try { const a = L(); o = { ...o, ...a, ...t }; } catch (a) { c() ? window.NREUM.noticeError(s, o) : m("Unable to assemble useful error attributes", a); } c() && window.NREUM.noticeError(s, o), console.error( `%c${r} error`, `background: ${h.error};${f}`, s, o ); } }, S = (i, e = {}) => { c() && window.NREUM.addPageAction(i, e); }, O = (i, e) => { S("API-calls-timing", { api_call: i, api_response_time: e }); }, E = { error: A, warn: m, log: M, info: U, /** @deprecated use newRelicPageAction() instead */ logAPIResponseTime: O, setLogLevel: (i) => { if (!u.hasOwnProperty(i)) throw new Error(`Unknown logLevel '${i}'`); d = u[i], console.log( `%c${r} log level set to ${i}`, // @ts-ignore `background: ${h[i]};${f}` ), c() || m("Offsite logging not enabled"); }, setPrefix: (i) => { r = i; }, init: (i) => { L = i; } }, y = "installPageUrl", N = "installType"; class P { subscribers = /* @__PURE__ */ new Set(); sub(e) { return this.subscribers.add(e), () => this.subscribers.delete(e); } pub(e) { this.subscribers.forEach((t) => t(e)); } } class R { events = /* @__PURE__ */ new Map(); /** * Registers a listener that will be called when the named event is emitted * @returns An unsubscribe function that will turn off this listener. */ on(e, t) { return this.events.has(e) || this.events.set(e, new P()), this.events.get(e).sub(t); } /** * Registers a listener that will be called a single time when the named event * is emitted * @returns An unsubscribe function that will turn off this listener. */ once(e, t) { const s = this.on(e, (o) => (s(), t(o))); return s; } /** * Removes all event listeners for the given key * @returns void */ off(e) { this.events.delete(e); } /** * Used to emit events to all registered hook listeners * * @private */ emit(e, t) { this.events.has(e) && this.events.get(e).pub(t); } } const C = (i) => { i.forEach((e) => { e.classList.add("fade-in"); }); }, w = (i, e) => { i.classList.remove("fade-in"), window.setTimeout(() => { document.body.style.overflow = "auto", i.parentNode.removeChild(i); }, e); }, I = (i) => { const e = new URL(i); return new URL(window.location.href).searchParams.forEach((s, o) => { e.searchParams.has(o) || e.searchParams.set(o, s); }), e.href; }, B = { INLINE_IFRAME_CONTENT: "sid-inline-iframe", MODAL_WRAPPER: "sid-modal__wrapper", MODAL_IFRAME: "sid-modal__iframe", OVERLAY: "sid-modal__overlay", CLOSE_BUTTON: "sid-modal__close-button", CLOSE_TEXT: "sid-modal__close-text", CLOSE_ICON: "sid-modal__close-icon" }, v = "SheerID Verification Form", n = { CLASS_NAMES: B, DEFAULT_MOBILE_THRESHOLD_WIDTH: 620, MODAL_OPACITY_TRANSITION_PERIOD: 300 }, x = { className: n.CLASS_NAMES.INLINE_IFRAME_CONTENT, title: v }, D = (i) => { if (typeof i != "function") throw new Error(`Expected type "function", but received ${typeof i}`); }; class l extends R { containerElement; verificationUrl; isMobileDevice; iframe; verificationIframeUid; locale; hasLoaded = !1; onLoadEvents = []; onCleanupEvents = []; installType = "cdn_inline_iframe"; constructor(e, t) { super(), l.isValidHttpUrl(t) ? (this.containerElement = e, this.verificationUrl = new URL(I(t)), this.verificationIframeUid = l.createUniqueId()) : E.error("Invalid URL. Provide a proper URL: https://example.com/", "iframe url"); } cleanup() { this.onCleanupEvents.forEach((e) => e()); } static createUniqueId() { return Math.random().toString(36).substr(2, 9); } static isValidHttpUrl(e) { try { const t = new URL(e); return t.protocol === "http:" || t.protocol === "https:"; } catch { return !1; } } createIframe(e) { return this.iframe = document.createElement("iframe"), this.iframe.classList.add(e.className), this.iframe.title = e.title, this.iframe.src = this.getIframeUrl(), this.iframe.allow = "camera *;microphone *;geolocation *;clipboard-read *;clipboard-write *", this.iframe.onload = () => this.onLoad(), this.iframe; } getIframeUrl() { const e = new URL(this.verificationUrl); return e.searchParams.set("verificationIframeUid", this.verificationIframeUid), e.searchParams.set(y, window.location.href), e.searchParams.set(N, this.installType), this.locale && e.searchParams.set("locale", this.locale), e.toString(); } // If iFrame hasn't loaded yet, queue up the callback // otherwise call it immediately addOnLoadEvent(e) { try { D(e); } catch { E.error("Invalid callback provided to iFrame.onLoad()", "iframe addOnLoadEvent"); return; } this.hasLoaded ? e() : this.onLoadEvents.push(e); } onLoad() { this.hasLoaded = !0, this.onLoadEvents.forEach((e) => e()); } /** * Using this to add parent window message listeners gives us * - Event Cleanup for modals * - verification of origin, verificationIframeUid, and message data structure */ addWindowMessageListener(e) { const t = (s) => { this.verificationUrl.origin === s.origin && s.data.verificationIframeUid === this.verificationIframeUid && (s.data.action && s.data.action.type ? e(s.data.action) : s.data.action && s.data.height && e(s.data)); }; window.addEventListener("message", t), this.onCleanupEvents.push(() => window.removeEventListener("message", t)); } addActionListener() { this.addWindowMessageListener((e) => { e.type === "hook" && this.emit(e.hook.name, e.hook.data), (e.action && e.action === "updateHeight" || e.type === "updateHeight") && (this.iframe.scrolling = "no", this.iframe.style.height = `${e.height}px`); }); } setViewModel(e) { const t = { action: "setViewModel", viewModel: e }; this.addOnLoadEvent(() => { this.iframe.contentWindow.postMessage(t, this.verificationUrl.origin); }); } setOptions(e) { const t = { action: "setOptions", options: e }; if (!this.hasLoaded && e.locale) { this.locale = e.locale; const s = this.getIframeUrl(); s !== this.iframe.src && (this.iframe.src = s); } this.addOnLoadEvent(() => { this.iframe.contentWindow.postMessage(t, this.verificationUrl.origin); }); } setInstallType(e) { this.installType = e; } init(e = !0) { e && this.createIframe(x), this.addActionListener(), this.containerElement.appendChild(this.iframe); } } class p { verificationUrl; redirectOnMobile; overlay; wrapper; iframeInstance; on; once; isMobileDevice; closeButtonText; closeButton; stopPropagation; constructor(e, t) { const s = t.mobileThreshold ? t.mobileThreshold : n.DEFAULT_MOBILE_THRESHOLD_WIDTH; this.verificationUrl = I(e), this.redirectOnMobile = t.mobileRedirect || !1, this.isMobileDevice = window.document.body.clientWidth <= Number(s), this.closeButtonText = t.closeButtonText ? t.closeButtonText : "", this.closeButton = null, this.stopPropagation = t.stopPropagation || !1, this.overlay = p.createOverlay(), this.wrapper = this.createWrapper(), this.addFocusListener(); } createCloseBtn() { if (this.closeButton = document.createElement("button"), this.closeButton.classList.add(n.CLASS_NAMES.CLOSE_BUTTON), this.closeButton.setAttribute("aria-label", "close"), this.closeButtonText) { const t = document.createElement("div"); t.classList.add(n.CLASS_NAMES.CLOSE_TEXT), t.innerHTML = this.closeButtonText, this.closeButton.appendChild(t); } const e = document.createElement("span"); return e.classList.add(n.CLASS_NAMES.CLOSE_ICON), e.setAttribute("role", "img"), this.closeButton.appendChild(e), this.closeButton.addEventListener("click", (t) => { t.preventDefault(), this.stopPropagation && t.stopPropagation(), this.closeModal(); }), this.closeButton; } closeModal() { window.removeEventListener("popstate", this.popStateEventHandler); const e = n.MODAL_OPACITY_TRANSITION_PERIOD; w(this.overlay, e), w(this.wrapper, e), this.iframeInstance.cleanup(); } static createOverlay() { const e = document.createElement("div"); return e.classList.add(n.CLASS_NAMES.OVERLAY), e; } createWrapper() { const e = document.createElement("div"); e.classList.add(n.CLASS_NAMES.MODAL_WRAPPER), e.tabIndex = -1, e.setAttribute("role", "dialog"), e.appendChild(this.createCloseBtn()), this.iframeInstance = new l(e, this.verificationUrl), this.iframeInstance.setInstallType("cdn_modal_iframe"), this.on = this.iframeInstance.on.bind(this.iframeInstance), this.once = this.iframeInstance.once.bind(this.iframeInstance); const t = { className: n.CLASS_NAMES.MODAL_IFRAME, title: v }; return this.iframeInstance.createIframe(t), e.appendChild(this.iframeInstance.iframe), e; } popStateEventHandler = () => this.closeModal(); addPopStateListener() { window.addEventListener("popstate", this.popStateEventHandler); } addFocusListener() { this.iframeInstance.addWindowMessageListener((e) => { e.type === "focus" && e.focusOn === "firstElement" && this.closeButton.focus(); }), document.addEventListener("keydown", (e) => { e.key === "Tab" && e.shiftKey && document.activeElement === this.closeButton && (this.iframeInstance.iframe.contentWindow.postMessage({ focusOn: "lastElement" }, "*"), e.preventDefault()); }); } isInLightboxPostMessage(e) { this.iframeInstance.iframe.contentWindow.postMessage({ isInLightBox: e }, "*"); } displayModal() { this.iframeInstance.iframe.addEventListener("load", () => { C([this.overlay, this.wrapper]), document.body.style.overflow = "hidden", this.isInLightboxPostMessage("true"); }), document.body.appendChild(this.overlay), document.body.appendChild(this.wrapper), this.addPopStateListener(); } init() { this.redirectOnMobile && this.isMobileDevice ? window.top.location.href = this.verificationUrl : (this.displayModal(), this.iframeInstance.init(!1)); } setViewModel(e) { this.iframeInstance.setViewModel(e); } setOptions(e) { this.iframeInstance.setOptions(e); } } function $(i, e) { const t = new l(i, e); return t.init(), t; } function k(i, e = {}) { const t = new p(i, e); return t.init(), t; } const H = { loadInModal: k, loadInlineIframe: $ }; export { H as default, k as loadInModal, $ as loadInlineIframe };