-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathjsonp.min.js
8 lines (8 loc) · 1.17 KB
/
jsonp.min.js
1
2
3
4
5
6
7
8
/*
Lightweight JSONP fetcher
Copyright 2010-2019 Erik Arenhill. All rights reserved.
BSD Zero Clause License
*/
var JSONP=function(h){function n(c,d){function k(){"function"===typeof e&&(e({url:c,event:Error("Timeout")}),d())}var a=document.createElement("script"),g=!1,b=null;a.src=c;a.async=!0;var e=f.error;"function"===typeof e&&(a.onerror=function(l){clearTimeout(b);b=null;e({url:c,event:l})});a.onload=a.onreadystatechange=function(){g||this.readyState&&"loaded"!==this.readyState&&"complete"!==this.readyState||(g=!0,clearTimeout(b),b=null,a.onload=a.onreadystatechange=null,a&&a.parentNode&&a.parentNode.removeChild(a))};
f.timeout&&(b=setTimeout(k,f.timeout));m||(m=document.getElementsByTagName("head")[0]);m.appendChild(a)}var p=0,m,f={};return{get:function(c,d,k,a){var g=-1===(c||"").indexOf("?")?"?":"&",b;a=a||f.callbackName||"callback";f.callbackName||(a=a+"_jsonp_"+ ++p);d=d||{};for(b in d)d.hasOwnProperty(b)&&(g+=encodeURIComponent(b)+"="+encodeURIComponent(d[b])+"&");var e=!1;h[a]=function(l){e||k(l);try{delete h[a]}catch(q){}h[a]=null};n(c+g+"callback="+a,function(){e=!0});return a},init:function(c){f=c}}}(window);
"undefined"!==typeof module&&(module.exports=JSONP);