-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbrowser.js
More file actions
1 lines (1 loc) · 4.11 KB
/
Copy pathbrowser.js
File metadata and controls
1 lines (1 loc) · 4.11 KB
1
!function(){var e={camelToKebab:function(e){let t=!1,s="";for(const n of e)n.toUpperCase()==n?(s+=t?n.toLowerCase():"-"+n.toLowerCase(),t=!0):(s+=n,t=!1);return"-"==s[0]?s.slice(1):s}};const{camelToKebab:t}=e;var s=function(e,s,n,o=null,i=null){return new Promise(c=>{var r=new XMLHttpRequest;o=function(e){if(!e)return null;let t=[],s=[],n=[],o=null;const i=JSON.stringify(e,(e,i)=>{if(!e)return t.push(i),i;for(;s.length&&s[0]!==this;)s.shift(),n.pop();return void 0!==i?i instanceof Blob?(o||(o=new FormData),"."==(e=n.join(".")+"."+e)[0]&&(e=e.slice(1)),void o.append(e,i)):(t.find(e=>e===i)||(n.push(e),s.unshift(i),t.push(i)),i):void 0});return o?(o.append("json",i),o):i}(o),i=i?"?"+Object.entries(i).map(e=>e[0]+"="+e[1]).join("&"):"",e.useKebab&&(s=t(s),n=t(n)),r.open(o?"POST":"GET",`${e.secure?"https":"http"}://${e.base}/${s}/${n}${i}`),"string"==typeof o&&r.setRequestHeader("Content-type","application/json");let a,h=localStorage.getItem("session");h&&r.setRequestHeader("Session",h),r.send(o),r.onerror=()=>{c({success:!1,code:r.status,msg:r.statusText||r.response||"NetworkError"})},r.onreadystatechange=()=>{switch(r.readyState){case r.HEADERS_RECEIVED:("application/octet-stream"==(a=r.getResponseHeader("content-type"))||a.startsWith("image/"))&&(r.responseType="blob")}},r.onload=()=>{if((h=r.getResponseHeader("session"))&&localStorage.setItem("session",h),"application/json"==a)try{c({success:200==r.status,code:r.status,msg:JSON.parse(r.response)})}catch(e){c({success:!1,code:-1,msg:e})}else c({success:200==r.status,code:r.status,msg:r.response})}})},n={};const{camelToKebab:o}=e;class i{constructor(e,t){this._events={},this._connection=null,this._settings=e.settings,this.socket=null,this.path=t}connect(e=0){this._socket=new WebSocket(`${this._settings.secure?"wss":"ws"}://${this._settings.base}/${this.path}`),this._connection=new Promise(t=>{this._socket.onopen=()=>{this._socket.send('["session",'+localStorage.getItem("session")+"]"),this._connection=null,t(),this._dispatch("open",[e>0]),e=0}}),this._socket.onmessage=e=>{var t=JSON.parse(e.data);"session"==t[0]&&localStorage.setItem("session",JSON.stringify(t[1])),this._dispatch(t[0],t.slice(1))},this._socket.onclose=t=>{this._socket._disableReconnect||-1!=this._settings.reconnectAttempts&&e==this._settings.reconnectAttempts?(this._dispatch("close",[t.code,t.reason]),this._socket=null):(this._dispatch("reconnect",[e+1]),setTimeout(()=>this.connect(++e),1e3*this._settings.reconnectTimeout))}}_dispatch(e,t){const s=this._events[e];if(s)for(let e=0;e<s.length;e++)s[e].apply(null,t),s[e].once&&s.splice(e,1)}on(e,t){this._events[e]?this._events[e].push(t):this._events[e]=[t]}once(e,t){t.once=!0,this.on(e,t)}off(e,t){const s=this._events[e];if(s){var n=s.findIndex(e=>e===t);~n&&s.splice(n,1)}}emit(){let e=[];for(let t=0;t<arguments.length;t++)e[t]=arguments[t];const t=()=>this._socket.send(JSON.stringify(e));this._connection?this._connection.then(t):t()}close(){this._socket._disableReconnect=!0,this._socket.close(),this._dispatch("close",[0,"ManualClose"]),this._events={}}}n.initSockets=e=>e._sockets={},n.registerSocket=(e,t)=>e._sockets[t]=new i(e,o(t)),n.getSocket=(e,t)=>{const s=e._sockets[t];s._connection||s.connect()};var c;const{initSockets:r,registerSocket:a,getSocket:h}=n;function l(e,t,s,n){void 0===t[s]?e[s]=n:e[s]=t[s]}let u;class p{constructor(e={}){l(this,e,"base",window.location.hostname),l(this,e,"secure",!0),l(this,e,"reconnectAttempts",5),l(this,e,"reconnectTimeout",2.5),l(this,e,"useKebab",!1)}}function d(e){return r(e),e.send=(...t)=>s(e.settings,...t),new Proxy(e,{get:(t,n)=>n in e?e[n]:"socket"==n.toLowerCase()?h(e):new Proxy({},{get:(t,o)=>(t,i)=>s(e.settings,n,o,t,i)})})}u=d({get default(){return u},get __esModule(){return!0},settings:new p,post(e,t,s=!1){var n=document.createElement("form");n.method="POST",n.action=e,s&&(n.target="_blank"),Object.entries(t).forEach((function(e){var t=document.createElement("input");t.name=e[0],t.value=e[1],n.appendChild(t)})),document.body.appendChild(n),n.submit(),n.remove()},instances:{},registerInstance(e,t){const s=d({settings:new p(t)});return this.instances[e]=s,s},registerSocket(e){return a(this,e)}}),c=u,window.API=c}();