UNPKG

simple-load-script

Version:

Very simple promise based script and JSONP

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