UNPKG

@permitio/permit-js

Version:

Permitio is a frontend package that makes it easy to use permit abilities

50 lines 1.99 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.sendTokenToIframe = void 0; const TIME_TIMEOUT = 1200; const MAX_ATTEMPTS = 20; const TIME_INTERVAL = 400; const sendTokenToIframe = (token, elementIframeUrl) => { let tokenSent = false; const iframeRef = document === null || document === void 0 ? void 0 : document.querySelector(`iframe[src="${elementIframeUrl}"]`); if (!iframeRef) { console.info(`Element iframe with ${elementIframeUrl} not found retrying in ${TIME_TIMEOUT}ms`); const timer = setTimeout(() => { (0, exports.sendTokenToIframe)(token, elementIframeUrl); clearTimeout(timer); }, TIME_TIMEOUT); return; } const iframeWindow = iframeRef.contentWindow; if (!iframeWindow) { console.info("Element iframe contentWindow is null"); const timer = setTimeout(() => { (0, exports.sendTokenToIframe)(token, elementIframeUrl); clearTimeout(timer); }, TIME_TIMEOUT); return; } const messageListener = (event) => { if (event.data && event.data.type === "permitTokenReceived") { tokenSent = true; clearInterval(interval); window.removeEventListener("message", messageListener); } }; let attempts = 0; const interval = setInterval(() => { if (tokenSent) { clearInterval(interval); return; } iframeWindow === null || iframeWindow === void 0 ? void 0 : iframeWindow.postMessage({ type: 'permitToken', permitToken: token }, elementIframeUrl); window.addEventListener("message", messageListener); attempts++; if (attempts >= MAX_ATTEMPTS) { clearInterval(interval); console.error("We haven't recognized your element in a while."); } }, TIME_INTERVAL); }; exports.sendTokenToIframe = sendTokenToIframe; //# sourceMappingURL=sendToken.js.map