-
Notifications
You must be signed in to change notification settings - Fork 1
/
comm.min.js
2 lines (2 loc) · 2.02 KB
/
comm.min.js
1
2
/* comm.js @ https://www.github.com/jas-/comm.js */
(function(e,t){"use strict";var n=n||function(t,n){var r={async:true,binary:false,data:false,headers:{},interval:3600,method:"get",timeout:10,url:""};var i=i||{init:function(e,t){s.handle(e,t)}};var s=s||{online:function(){return navigator.onLine},handle:function(e,t){if(!this.online()){var n=setInterval(function(e,t){this.mode(e,t)},e.interval);clearInterval(n)}else{this.mode(e,t)}},mode:function(e,t){var n=new RegExp(document.location.href);if(/msie/i.test(navigator.userAgent)&&/^(http|https):\/\//i.test(e.url)&&!n.test(e.url)&&/msie (\d+)/i.exec(navigator.userAgent)[1]>10){return this.xdr(e,t)}if(/^(ws|wss):\/\//i.test(e.url)){return this.websocket(e,t)}return this.ajax(e,t)},websocket:function(e,t){var n=new WebSocket(e.url);n.onopen=function(){try{n.send(e.data)}catch(r){t(r)}};n.onmessage=function(e){n.close();t(null,e.data)}},xdr:function(t,n){var r=false;if(!e.XDomainRequest)n("XDomainRequest object not found");var i=new XDomainRequest;i.timeout=t.timeout;i.open(t.method,t.url);i.onsuccess=function(e){r=e};i.onerror=function(e){n(e)};i.send(t.data);n(null,r)},ajax:function(e,t){function i(n){t(e.errcallback(n.status))}function s(){var e=n.status;if(n.readyState==4&&(e>=200&&e<=299||e===304)){t(null,n.responseText)}}function u(e){if(o.parse(e.data)){n.setRequestHeader("Content-Type","application/json");e.data=JSON.stringify(e.data)}if(Object.keys(e).length>0){for(var t in e.headers){n.setRequestHeader(t,e.headers[t])}}if(!r.test(e.url))n.withCredentials=true}var n=new XMLHttpRequest,r=new RegExp(document.location.href);n.onreadystatechange=s;n.open(e.method,e.url,e.async);u(e);if(e.binary){n.sendAsBinary(e.data)}else{n.send(e.data)}}};var o=o||{merge:function(e,t){e=e||{};for(var n in e){if(e.hasOwnProperty(n)){t[n]=/object/.test(typeof e[n])?this.merge(t[n],e[n]):e[n]}t[n]=e[n]}return t},parse:function(e){var t=false;try{/object/.test(e)?JSON.parse(e):JSON.stringify(e)}catch(n){t=true}return t}};var u=function(){n=n||t;var e=o.merge(t,r);i.init(e,n)}()};e.comm=n})(window);