UNPKG

@sheerid/jslib-nightly

Version:

SheerID JavaScript Library

403 lines (401 loc) • 14.5 kB
/** * VERSION: 2.130.0-alpha.4 * BUILD_TIMESTAMP: 1759000330881 * BUILD_DATE: Sat Sep 27 2025 19:12:10 GMT+0000 (Coordinated Universal Time) * BUILD_COMMIT: be6ef87ab40e8747994d32363389cf39e36a4326 */ var A = Object.defineProperty; var T = (i, t, e) => t in i ? A(i, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : i[t] = e; var r = (i, t, e) => T(i, typeof t != "symbol" ? t + "" : t, e); const _ = "installPageUrl", M = "installType", y = { 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" }, b = "SheerID Verification Form", c = { CLASS_NAMES: y, DEFAULT_MOBILE_THRESHOLD_WIDTH: 620, MODAL_OPACITY_TRANSITION_PERIOD: 300 }, O = {}, N = async () => O, R = () => window.navigator.globalPrivacyControl, h = () => window.NREUM && !R(); function o(i, t) { try { return i(); } catch { return t; } } let f = 4, l = ""; const L = { info: 1, log: 2, warn: 3, error: 4 }, m = { info: "#26c1db", log: "#09f979", warn: "#f6b13f", error: "#e12046" }, p = "color: white; font-weight: bold; padding: 2px 10px;"; let u; const P = (i) => i && i.stack && i.message, E = (...i) => { f <= 3 && console.warn(`%c${l} warn`, `background: ${m.warn};${p}`, ...i); }, C = (...i) => { f <= 2 && console.log(`%c${l} log`, `background: ${m.log};${p}`, ...i); }, D = (...i) => { f <= 1 && console.log(`%c${l} info`, `background: ${m.info};${p}`, ...i); }, x = async (i, t = "unknown group", e = {}) => { if (f <= 4) { if (!i) { console.error(new Error("An error must be supplied")); return; } let s; typeof i == "string" && (s = new Error(i)), P(i) && (s = i), s || (s = new Error("Unknown error")); let n = { errorMessageGroup: t }; try { n = { ...await N(), ...n }, delete n.jslibVersionActual; } catch (a) { h() ? window.NREUM.noticeError(s, n) : E("Unable to assemble GA error attributes", a); } try { if (u && u.getState) { const a = u.getState(), I = o(() => a.verificationResponse.errorIds); n = { ...n, programId: o(() => a.programId), isLoading: o(() => a.isLoading), isErrored: o(() => a.isErrored), errorIdsFromVerRsp: Array.isArray(I) ? I.join(", ") : void 0, verificationId: o(() => a.verificationResponse.verificationId), currentStep: o(() => a.verificationResponse.currentStep), locale: o(() => a.programTheme.intl.locale), isTestMode: o(() => a.programTheme.isTestMode), openOrgSearchEnabled: o(() => a.programTheme.openOrgSearchEnabled), jslibVerActual: o(() => "2.130.0-alpha.4", "?"), ...e }; } } catch (a) { h() ? window.NREUM.noticeError(s, n) : E("Unable to assemble useful error attributes", a); } h() && window.NREUM.noticeError(s, n), console.error( `%c${l} error`, `background: ${m.error};${p}`, s, n ); } }, B = (i, t = {}) => { h() && window.NREUM.addPageAction(i, t); }, $ = (i, t) => { B("API-calls-timing", { api_call: i, api_response_time: t }); }, g = { error: x, warn: E, log: C, info: D, /** @deprecated use newRelicPageAction() instead */ logAPIResponseTime: $, setLogLevel: (i) => { if (!L.hasOwnProperty(i)) throw new Error(`Unknown logLevel '${i}'`); f = L[i], console.log( `%c${l} log level set to ${i}`, `background: ${m[i]};${p}` ), h() || E("Offsite logging not enabled"); }, setPrefix: (i) => { l = i; }, init: (i) => { u = i; } }, W = (i) => { i.forEach((t) => { t.classList.add("fade-in"); }); }, v = (i, t) => { i.classList.remove("fade-in"), window.setTimeout(() => { document.body.style.overflow = "auto", i.parentNode.removeChild(i); }, t); }, U = (i) => { const t = new URL(i); return new URL(window.location.href).searchParams.forEach((s, n) => { t.searchParams.has(n) || t.searchParams.set(n, s); }), t.href; }; class F { constructor() { r(this, "subscribers", /* @__PURE__ */ new Set()); } sub(t) { return this.subscribers.add(t), () => this.subscribers.delete(t); } pub(t) { this.subscribers.forEach((e) => e(t)); } } class k { constructor() { r(this, "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(t, e) { return this.events.has(t) || this.events.set(t, new F()), this.events.get(t).sub(e); } /** * 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(t, e) { const s = this.on(t, (n) => (s(), e(n))); return s; } /** * Removes all event listeners for the given key * @returns void */ off(t) { this.events.delete(t); } /** * Used to emit events to all registered hook listeners * * @private */ emit(t, e) { this.events.has(t) && this.events.get(t).pub(e); } } const S = { className: c.CLASS_NAMES.INLINE_IFRAME_CONTENT, title: b }, H = (i) => { if (typeof i != "function") throw new Error(`Expected type "function", but received ${typeof i}`); }; class d extends k { constructor(e, s) { super(); r(this, "containerElement"); r(this, "verificationUrl"); r(this, "isMobileDevice"); r(this, "iframe"); r(this, "verificationIframeUid"); r(this, "locale"); r(this, "hasLoaded", !1); r(this, "onLoadEvents", []); r(this, "onCleanupEvents", []); r(this, "installType", "cdn_inline_iframe"); d.isValidHttpUrl(s) ? (this.containerElement = e, this.verificationUrl = new URL(U(s)), this.verificationIframeUid = d.createUniqueId()) : g.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 s = new URL(e); return s.protocol === "http:" || s.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(_, window.location.href), e.searchParams.set(M, 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 { H(e); } catch { g.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 s = (n) => { this.verificationUrl.origin === n.origin && n.data.verificationIframeUid === this.verificationIframeUid && (n.data.action && n.data.action.type ? e(n.data.action) : n.data.action && n.data.height && e(n.data)); }; window.addEventListener("message", s), this.onCleanupEvents.push(() => window.removeEventListener("message", s)); } 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 s = { action: "setViewModel", viewModel: e }; this.addOnLoadEvent(() => { this.iframe.contentWindow.postMessage(s, this.verificationUrl.origin); }); } setOptions(e) { const s = { action: "setOptions", options: e }; if (!this.hasLoaded && e.locale) { this.locale = e.locale; const n = this.getIframeUrl(); n !== this.iframe.src && (this.iframe.src = n); } this.addOnLoadEvent(() => { this.iframe.contentWindow.postMessage(s, this.verificationUrl.origin); }); } setInstallType(e) { this.installType = e; } init(e = !0) { e && this.createIframe(S), this.addActionListener(), this.containerElement.appendChild(this.iframe); } } class V extends d { constructor(t, e) { super(t, e), this.verificationUrl = new URL(this.getAffinityUrlWithUID(e)); } static createUniqueId() { return Math.random().toString(36).substr(2, 9); } getAffinityUrlWithUID(t) { const e = new URL(t); return e.searchParams.set("affinityIframeUid", this.verificationIframeUid), e.toString(); } addWindowMessageListener(t) { const e = (s) => { this.verificationUrl.origin === s.origin && s.data.affinityIframeUid === this.verificationIframeUid && s.data.action && s.data.action.type && t(s.data.action); }; window.addEventListener("message", e), this.onCleanupEvents.push(() => window.removeEventListener("message", e)); } createIframe(t) { return this.iframe = document.createElement("iframe"), this.iframe.classList.add(t.className), this.iframe.title = t.title, this.iframe.src = this.verificationUrl.href, this.iframe.allow = "camera;microphone", this.iframe.onload = () => this.onLoad(), this.iframe; } init() { this.createIframe({ ...S, title: "SheerID Affinity Offers" }), this.addActionListener(), this.containerElement.appendChild(this.iframe); } } class w { constructor(t, e) { r(this, "verificationUrl"); r(this, "redirectOnMobile"); r(this, "overlay"); r(this, "wrapper"); r(this, "iframeInstance"); r(this, "on"); r(this, "once"); r(this, "isMobileDevice"); r(this, "closeButtonText"); r(this, "closeButton"); r(this, "stopPropagation"); r(this, "popStateEventHandler", () => this.closeModal()); const s = e.mobileThreshold ? e.mobileThreshold : c.DEFAULT_MOBILE_THRESHOLD_WIDTH; this.verificationUrl = U(t), this.redirectOnMobile = e.mobileRedirect || !1, this.isMobileDevice = window.document.body.clientWidth <= Number(s), this.closeButtonText = e.closeButtonText ? e.closeButtonText : "", this.closeButton = null, this.stopPropagation = e.stopPropagation || !1, this.overlay = w.createOverlay(), this.wrapper = this.createWrapper(), this.addFocusListener(); } createCloseBtn() { if (this.closeButton = document.createElement("button"), this.closeButton.classList.add(c.CLASS_NAMES.CLOSE_BUTTON), this.closeButton.setAttribute("aria-label", "close"), this.closeButtonText) { const e = document.createElement("div"); e.classList.add(c.CLASS_NAMES.CLOSE_TEXT), e.innerHTML = this.closeButtonText, this.closeButton.appendChild(e); } const t = document.createElement("span"); return t.classList.add(c.CLASS_NAMES.CLOSE_ICON), t.setAttribute("role", "img"), this.closeButton.appendChild(t), this.closeButton.addEventListener("click", (e) => { e.preventDefault(), this.stopPropagation && e.stopPropagation(), this.closeModal(); }), this.closeButton; } closeModal() { window.removeEventListener("popstate", this.popStateEventHandler); const t = c.MODAL_OPACITY_TRANSITION_PERIOD; v(this.overlay, t), v(this.wrapper, t), this.iframeInstance.cleanup(); } static createOverlay() { const t = document.createElement("div"); return t.classList.add(c.CLASS_NAMES.OVERLAY), t; } createWrapper() { const t = document.createElement("div"); t.classList.add(c.CLASS_NAMES.MODAL_WRAPPER), t.tabIndex = -1, t.setAttribute("role", "dialog"), t.appendChild(this.createCloseBtn()), this.iframeInstance = new d(t, 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 e = { className: c.CLASS_NAMES.MODAL_IFRAME, title: b }; return this.iframeInstance.createIframe(e), t.appendChild(this.iframeInstance.iframe), t; } addPopStateListener() { window.addEventListener("popstate", this.popStateEventHandler); } addFocusListener() { this.iframeInstance.addWindowMessageListener((t) => { t.type === "focus" && t.focusOn === "firstElement" && this.closeButton.focus(); }), document.addEventListener("keydown", (t) => { t.key === "Tab" && t.shiftKey && document.activeElement === this.closeButton && (this.iframeInstance.iframe.contentWindow.postMessage({ focusOn: "lastElement" }, "*"), t.preventDefault()); }); } isInLightboxPostMessage(t) { this.iframeInstance.iframe.contentWindow.postMessage({ isInLightBox: t }, "*"); } displayModal() { this.iframeInstance.iframe.addEventListener("load", () => { W([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(t) { this.iframeInstance.setViewModel(t); } setOptions(t) { this.iframeInstance.setOptions(t); } } function Y(i, t) { const e = new d(i, t); return e.init(), e; } function j(i, t = {}) { const e = new w(i, t); return e.init(), e; } function q(i, t) { const e = new V(i, t); return e.init(), e; } const Q = { loadInModal: j, loadInlineIframe: Y }; export { Q as default, q as loadAffinityIFrame, j as loadInModal, Y as loadInlineIframe };