payrex-js
Version:
CommonJS or ES module for PayRex JS
1 lines • 1.21 kB
JavaScript
let e="https://js.payrexhq.com",r=/^https:\/\/js\.payrexhq\.com$/,t=null,n=null,l=null,o=e=>()=>{e(Error("There is a problem loading PayRex.js"))},d=(e,r)=>()=>{window.Payrex?e(window.Payrex):r(Error("Payrex.js not available"))},a=()=>{let r=document.createElement("script"),t=document.head||document.body;if(r.src=e,!t)throw Error("document.body should not be null. Payrex.js requires a <body> element.");return t.appendChild(r),r},u=()=>{let t=document.querySelectorAll(`script[src^="${e}"]`);for(let e=0;e<t.length;e++){let n=t[e];if(r.test(n.src))return n}return null},c=()=>i()?t:(t=new Promise((e,r)=>{if(s()){e(null);return}if(window.Payrex){e(window.Payrex);return}try{let t=u();t?t&&null!==l&&null!==n&&(t.removeEventListener("load",l),t.removeEventListener("error",n),t.parentNode?.removeChild(t),t=a()):t=a(),l=d(e,r),n=o(r),t.addEventListener("load",l),t.addEventListener("error",n)}catch(e){r(e);return}})).catch(e=>(t=null,Promise.reject(e))),i=()=>null!=t,s=()=>"undefined"==typeof window||"undefined"==typeof document,m=null,h=()=>m||(m=c().catch(e=>(m=null,Promise.reject(e))));Promise.resolve().then(()=>h()).catch(e=>{console.log(e)});let y=(...e)=>h().then(r=>r(e));export{y as loadPayrex};