@sumsub/websdk
Version:
SumSub WebSdk
2 lines (1 loc) • 12.8 kB
JavaScript
!function(i,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(i="undefined"!=typeof globalThis?globalThis:i||self).snsWebSdk=t()}(this,function(){"use strict";var i=function(){return i=Object.assign||function(i){for(var t,e=1,o=arguments.length;e<o;e++)for(var n in t=arguments[e])Object.prototype.hasOwnProperty.call(t,n)&&(i[n]=t[n]);return i},i.apply(this,arguments)};function t(i){return i.endsWith("-v2")?2:1}var e=function(){function e(i,t,e,o){var n=this;this.iframe=null,this.iframeId=null,this.sessionId="",this.$container=null,this.initialized=!1,this.scrollPosition=-1,this.scrollElement=null,this.onMessage=function(i){n.onSdkMessage(i)},this.baseUrl=i||this.getCurrentBaseUrl("https://in.sumsub.com",t.accessToken),this.config=t,this.config.packageVersion=this.getVersion(),this.config.origin=location.origin,this.callbacks=e,this.options=o}return e.prototype.getVersion=function(){return"2.6.3"},e.prototype.getCurrentBaseUrl=function(i,t){var e=["_act-sbx-jwt-","_act-jwt-"].find(function(i){return t.startsWith(i)});if(e)for(var o=0,n=t.replace(e,"").replace("-v2","").split(".").map(function(i){try{return atob(i)}catch(t){return i}}).map(function(i){try{return JSON.parse(i)}catch(t){return i}});o<n.length;o++){var s=n[o];if(null==s?void 0:s.url)return s.url}return i},e.prototype.getIframeId=function(){if(2===this.config.version)try{return btoa(this.config.accessToken)}catch(i){return this.config.accessToken}return String(Math.floor(1e8*Math.random()))},e.prototype.launch=function(i){this.options.addViewportTag&&this.addViewportTag(),this.iframe=this.createIframe(i),this.sessionId="",this.iframeId="id_"+this.getIframeId(),this.iframe&&this.config?(this.registerEventListener(),this.iframe.src=this.getIframeSrc()):console.error("Idensic was not initialized since either provided element was not found in the DOM or invalid config")},e.prototype.addViewportTag=function(){var i=document.getElementsByName("viewport");if(!i||0===i.length){var t=document.createElement("meta");t.setAttribute("name","viewport"),t.setAttribute("content","width=device-width,user-scalable=yes"),document.head.appendChild(t)}},e.prototype.createIframe=function(i){var t="string"==typeof i?document.querySelector(i):i;if(!t)return console.error("Provide a valid selector for the iframe container"),null;this.$container=t;var e=document.createElement("iframe");for(e.width="100%",e.scrolling="no",e.allow="camera; microphone; geolocation; clipboard-write",e.setAttribute("frameborder","0");t.firstChild;)t.removeChild(t.firstChild);return t.appendChild(e),e},e.prototype.getIframeSrc=function(){var i="?_="+this.iframeId,t=2===this.config.version?"/websdk":"/idensic",e=this.baseUrl+t+"/websdk.html";return 2===this.config.version&&this.config.theme&&(i+="&theme="+this.config.theme),2===this.config.version&&this.config.customizationName&&(i+="&customizationName="+encodeURIComponent(this.config.customizationName)),2===this.config.version&&this.config.translationName&&(i+="&translationName="+encodeURIComponent(this.config.translationName)),e+i},e.prototype.registerEventListener=function(){window.addEventListener("message",this.onMessage)},e.prototype.onSdkMessage=function(t){var e;if(this.baseUrl===t.origin){var o=t.data;if(o.method&&~o.method.indexOf("idCheck")&&(!this.sessionId||this.sessionId===o.sessionId)&&(null===(e=this.iframe)||void 0===e?void 0:e.contentWindow)){if("idCheck.onReady"==o.method&&o.frameId===this.iframeId){this.sessionId=o.sessionId;var n={options:{adaptIframeHeight:this.options.adaptIframeHeight,enableScrollIntoView:this.options.enableScrollIntoView}};this.iframe.contentWindow.postMessage(i(i({method:"idCheck.init"},this.config),n),"*")}if("idCheck.onInitialized"==o.method&&(this.initialized=!0),"idCheck.onResize"==o.method&&this.options.adaptIframeHeight&&(this.iframe.style.height=o.height+"px"),"idCheck.onSetBorderRadius"!=o.method)if("idCheck.scrollTo"==o.method&&this.options.adaptIframeHeight)this.scrollTo(o.top);else if("idCheck.getScrollPosition"!=o.method)if("idCheck.restoreScrollPosition"!=o.method){var s=o.method;delete o.method,delete o.frameId,delete o.sessionId,"idCheck.onError"===s&&"invalid-token"===o.code?this.callExpirationHandler():"idCheck.onError"===s&&"function"==typeof this.callbacks.onError?this.callbacks.onError(o):"function"==typeof this.callbacks.onMessage&&this.callbacks.onMessage(s,o)}else this.restoreScrollPosition();else this.getScrollPosition();else this.iframe&&o.radius&&"string"==typeof o.radius&&(this.iframe.style.borderRadius=o.radius,this.iframe.style.overflow="hidden")}}},e.prototype.callExpirationHandler=function(){var i=this,t=this.callbacks.expirationHandler;t.legacy?t.handler(function(t){return i.updateAccessTokenOrReinitialize(t)}):t.handler().then(function(t){return i.updateAccessTokenOrReinitialize(t)},function(t){null!=t||(t="Failed to update access token"),t.message&&(t=t.message),"string"!=typeof t&&(t=String(t)),i.updateAccessToken(null,t)})},e.prototype.updateAccessTokenOrReinitialize=function(i){var e=i?t(i):this.config.version;if(i&&this.iframe&&!this.initialized&&this.config.version!=e)return this.sessionId="",this.config.accessToken=i,this.config.version=e,this.baseUrl=this.getCurrentBaseUrl(this.baseUrl,i),this.iframeId="id_"+this.getIframeId(),void(this.iframe.src=this.getIframeSrc());this.updateAccessToken(i)},e.prototype.getScrolledElement=function(){for(var i,t=null===(i=this.iframe)||void 0===i?void 0:i.parentElement;0===(null==t?void 0:t.scrollTop)&&"BODY"!==(null==t?void 0:t.tagName);)t=null==t?void 0:t.parentElement;return t},e.prototype.getScrollPosition=function(){var i,t,e=this.getScrolledElement();this.scrollPosition=(null==e?void 0:e.scrollTop)||-1,this.scrollElement=this.scrollPosition>0&&e?e:null,null===(t=null===(i=this.iframe)||void 0===i?void 0:i.contentWindow)||void 0===t||t.postMessage({method:"idCheck.scrollPositionStored"},"*")},e.prototype.restoreScrollPosition=function(){this.scrollElement&&this.scrollPosition>=0&&(this.scrollElement.scrollTop=this.scrollPosition),this.scrollPosition=-1,this.scrollElement=null},e.prototype.scrollTo=function(i){var t;if(this.options.enableScrollIntoView){var e=this.getScrolledElement();if(0===(null==e?void 0:e.scrollTop)&&"BODY"===(null==e?void 0:e.tagName)){var o=(null===(t=this.iframe)||void 0===t?void 0:t.getBoundingClientRect().top)||0;window.scrollTo({top:o+i,behavior:"smooth"})}else null==e||e.scrollTo({top:i,behavior:"smooth"})}},e.prototype.updateAccessToken=function(i,t){var e,o;null===(o=null===(e=this.iframe)||void 0===e?void 0:e.contentWindow)||void 0===o||o.postMessage({method:"idCheck.updateAccessToken",accessToken:i,error:t},"*")},e.prototype.destroy=function(){for(window.removeEventListener("message",this.onMessage);this.$container&&this.$container.firstChild;)this.$container.removeChild(this.$container.firstChild);this.$container=null},e.prototype.navigateBack=function(){var i,t;null===(t=null===(i=this.iframe)||void 0===i?void 0:i.contentWindow)||void 0===t||t.postMessage({method:"idCheck.callNavigationBack"},"*")},e.prototype.singleStepNext=function(i){var t,e;null===(e=null===(t=this.iframe)||void 0===t?void 0:t.contentWindow)||void 0===e||e.postMessage({method:"idCheck.callSingleStepNext",step:i},"*")},e.prototype.setLanguage=function(i){var t,e;null===(e=null===(t=this.iframe)||void 0===t?void 0:t.contentWindow)||void 0===e||e.postMessage({method:"idCheck.callSetLanguage",language:i},"*")},e.prototype.setTheme=function(i){var t,e;null===(e=null===(t=this.iframe)||void 0===t?void 0:t.contentWindow)||void 0===e||e.postMessage({method:"idCheck.callSetTheme",theme:i},"*")},e}(),o=function(){function i(i,t){if(this.config=null,this.eventHandlers={},this.anyEventHandler=null,this.options={adaptIframeHeight:!0,addViewportTag:!0,enableScrollIntoView:!0},"string"!=typeof i)throw new Error("Access token must be a string");if("function"!=typeof t)throw new Error("updateAccessToken callback is required");this.accessToken=i,this.updateAccessToken=t}return i.prototype.onTestEnv=function(){return this},i.prototype.withBaseUrl=function(i){return this.baseUrl=i,this},i.prototype.withConf=function(i){return this.config=i,this},i.prototype.withOptions=function(i){var t;return i.hasOwnProperty("adaptIframeHeight")&&(this.options.adaptIframeHeight=i.adaptIframeHeight),i.hasOwnProperty("addViewportTag")&&(this.options.addViewportTag=i.addViewportTag),this.options.enableScrollIntoView=null===(t=i.enableScrollIntoView)||void 0===t||t,this},i.prototype.on=function(i,t){return this.eventHandlers[i]=t,this},i.prototype.onMessage=function(i){return this.anyEventHandler=i,this},i.prototype.onNavigationUiControlsStateChanged=function(i){return this.eventHandlers["idCheck.onNavigationUiControlsStateChanged"]=i,this},i.prototype.build=function(){var i,o,n,s,r,a,l,c,d,h,u,f,p,g,v,m=this,w=t(this.accessToken);return new e(this.baseUrl,{version:w,theme:null===(i=this.config)||void 0===i?void 0:i.theme,customizationName:null===(o=this.config)||void 0===o?void 0:o.customizationName,translationName:null===(n=this.config)||void 0===n?void 0:n.translationName,accessToken:this.accessToken,lang:null===(s=this.config)||void 0===s?void 0:s.lang,email:null===(r=this.config)||void 0===r?void 0:r.email,phone:null===(a=this.config)||void 0===a?void 0:a.phone,country:null===(l=this.config)||void 0===l?void 0:l.country,uiConf:null===(c=this.config)||void 0===c?void 0:c.uiConf,i18n:null===(d=this.config)||void 0===d?void 0:d.i18n,documentsByCountries:null===(h=this.config)||void 0===h?void 0:h.documentsByCountries,documentDefinitions:null===(u=this.config)||void 0===u?void 0:u.documentDefinitions,autoSelectDocumentDefinitions:null===(f=this.config)||void 0===f?void 0:f.autoSelectDocumentDefinitions,controlledNavigationBack:null===(p=this.config)||void 0===p?void 0:p.controlledNavigationBack,singleStep:null===(g=this.config)||void 0===g?void 0:g.singleStep,preferredCameras:null===(v=this.config)||void 0===v?void 0:v.preferredCameras},{expirationHandler:{legacy:!1,handler:this.updateAccessToken},onMessage:function(i,t){var e,o=m.eventHandlers[i];o?o(t):null===(e=m.anyEventHandler)||void 0===e||e.call(m,i,t)}},this.options)},i}(),n=function(){function i(i,t){this.debugEnabled=!1,this.options={adaptIframeHeight:!0,addViewportTag:!0,enableScrollIntoView:!0},this.config=null,this.accessToken=null,this.expirationHandler=null,this.baseUrl=i,this.flowName=t}return i.prototype.withAccessToken=function(i,t){if(this.accessToken=i,!t||"function"!=typeof t)throw new Error('Invalid parameter, "expirationHandler" must be a function');return this.expirationHandler=t,this},i.prototype.debug=function(i){return this.debugEnabled=i,this},i.prototype.withOptions=function(i){var t;return i.hasOwnProperty("adaptIframeHeight")&&(this.options.adaptIframeHeight=i.adaptIframeHeight),i.hasOwnProperty("addViewportTag")&&(this.options.addViewportTag=i.addViewportTag),this.options.enableScrollIntoView=null===(t=i.enableScrollIntoView)||void 0===t||t,this},i.prototype.withConf=function(i){return this.config=i,this},i.prototype.build=function(){var i,o,n,s,r,a,l,c,d,h,u,f,p,g,v;if(!this.accessToken||!this.expirationHandler)throw new Error("Configure access token end the expiration handler before");var m=t(this.accessToken);return new e(this.baseUrl,{version:m,theme:null===(i=this.config)||void 0===i?void 0:i.theme,customizationName:null===(o=this.config)||void 0===o?void 0:o.customizationName,translationName:null===(n=this.config)||void 0===n?void 0:n.translationName,accessToken:this.accessToken,flowName:this.flowName,lang:null===(s=this.config)||void 0===s?void 0:s.lang,email:null===(r=this.config)||void 0===r?void 0:r.email,phone:null===(a=this.config)||void 0===a?void 0:a.phone,country:null===(l=this.config)||void 0===l?void 0:l.country,uiConf:null===(c=this.config)||void 0===c?void 0:c.uiConf,i18n:null===(d=this.config)||void 0===d?void 0:d.i18n,documentsByCountries:null===(h=this.config)||void 0===h?void 0:h.documentsByCountries,documentDefinitions:null===(u=this.config)||void 0===u?void 0:u.documentDefinitions,autoSelectDocumentDefinitions:null===(f=this.config)||void 0===f?void 0:f.autoSelectDocumentDefinitions,singleStep:null===(p=this.config)||void 0===p?void 0:p.singleStep},{expirationHandler:{legacy:!0,handler:this.expirationHandler},onMessage:null===(g=this.config)||void 0===g?void 0:g.onMessage,onError:null===(v=this.config)||void 0===v?void 0:v.onError},{adaptIframeHeight:this.options.adaptIframeHeight,addViewportTag:this.options.addViewportTag,enableScrollIntoView:this.options.enableScrollIntoView,debug:this.debugEnabled})},i}();return{Builder:function(i,t){return new n(i,t)},init:function(i,t){return new o(i,t)}}});