UNPKG

simple-load-script

Version:

Very simple promise based script and JSONP

2 lines (1 loc) 845 B
const e={url:"",attrs:{},inBody:!1,insertInto:null,removeScript:!1};function r(t){return Array.isArray(t)?Promise.all(t.map(r)):new Promise(((r,o)=>{if(!("object"==typeof t&&t.url||"string"==typeof t))return console.log({config:t}),void o(new Error("Object with url or url string needed"));const n=Object.assign({},e,"string"==typeof t?{url:t}:t),{url:i,attrs:s,inBody:d,insertInto:c,removeScript:l}=n,u=document.createElement("script"),a=c?document.querySelector(c):d?document.body:document.head;if(s&&"object"==typeof s)for(const e of Object.keys(s))u.setAttribute(e,s[e]);null!=a?(u.addEventListener("load",(()=>{l&&a.removeChild(u),r(l?void 0:u)})),u.addEventListener("error",(()=>{l&&a.removeChild(u),o(new Error("Loading script error"))})),u.src=i,a.appendChild(u)):o(new Error("No DOM element to append script"))}))}export{r as default};