payrex-js
Version:
CommonJS or ES module for PayRex JS
1 lines • 1.33 kB
JavaScript
Object.defineProperty(module.exports,"loadPayrex",{get:function(){return h},set:void 0,enumerable:!0,configurable:!0});const e="https://js.payrexhq.com",r=/^https:\/\/js\.payrexhq\.com$/;let t=null,n=null,o=null;const l=e=>()=>{e(Error("There is a problem loading PayRex.js"))},d=(e,r)=>()=>{window.Payrex?e(window.Payrex):r(Error("Payrex.js not available"))},u=()=>{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},c=()=>{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},s=()=>a()?t:(t=new Promise((e,r)=>{if(i()){e(null);return}if(window.Payrex){e(window.Payrex);return}try{let t=c();t?t&&null!==o&&null!==n&&(t.removeEventListener("load",o),t.removeEventListener("error",n),t.parentNode?.removeChild(t),t=u()):t=u(),o=d(e,r),n=l(r),t.addEventListener("load",o),t.addEventListener("error",n)}catch(e){r(e);return}})).catch(e=>(t=null,Promise.reject(e))),a=()=>null!=t,i=()=>"undefined"==typeof window||"undefined"==typeof document;let m=null;const y=()=>m||(m=s().catch(e=>(m=null,Promise.reject(e))));Promise.resolve().then(()=>y()).catch(e=>{console.log(e)});const h=(...e)=>y().then(r=>r(e));