/g,">").replace(//g,">").replace(/").replace(/\n/g,"").replace(/\t/g," ")}if(typeof a=="boolean"){return a}if(typeof a=="number"){return a}}function ArrayElementMove(a,b,c){a.splice(c,0,a.splice(b,1)[0])}function ObjectToStringEx(e,a){var d="";if(e!=0&&(!e||e==null)){return"(Null)"}if(e instanceof Array){for(var b in e){d+="
"+gap(a)+"Item #"+b+": "+ObjectToStringEx(e[b],a+1)}}else{if(e instanceof Object){for(var b in e){d+="
"+gap(a)+b+" = "+ObjectToStringEx(e[b],a+1)}}else{d+=EscapeHtml(e)}}return d}function ObjectToStringEx2(e,a){var d="";if(e!=0&&(!e||e==null)){return"(Null)"}if(e instanceof Array){for(var b in e){d+="\r\n"+gap2(a)+"Item #"+b+": "+ObjectToStringEx2(e[b],a+1)}}else{if(e instanceof Object){for(var b in e){d+="\r\n"+gap2(a)+b+" = "+ObjectToStringEx2(e[b],a+1)}}else{d+=EscapeHtml(e)}}return d}function gap(a){var d="";for(var b=0;b<(a*4);b++){d+=" "}return d}function gap2(a){var d="";for(var b=0;b<(a*4);b++){d+=" "}return d}function ObjectToString(a){return ObjectToStringEx(a,0)}function ObjectToString2(a){return ObjectToStringEx2(a,0)}function hex2rstr(a){if(typeof a!="string"||a.length==0){return""}var c="",b=(""+a).match(/../g),e;while(e=b.shift()){c+=String.fromCharCode("0x"+e)}return c}function char2hex(a){return(a+256).toString(16).substr(-2).toUpperCase()}function rstr2hex(b){var c="",a;for(a=0;a=200&&i.status<=299}function c(i){try{i.dispatchEvent(new MouseEvent("click"))}catch(g){var h=document.createEvent("MouseEvents");h.initMouseEvent("click",true,true,window,0,0,0,80,20,false,false,false,false,0,null);i.dispatchEvent(h)}}var f=a.saveAs||(typeof window!=="object"||window!==a?function f(){}:"download" in HTMLAnchorElement.prototype?function f(h,i,j){var k=a.URL||a.webkitURL;var g=document.createElement("a");i=i||h.name||"download";g.download=i;g.rel="noopener";if(typeof h==="string"){g.href=h;if(g.origin!==location.origin){d(g.href)?e(h,i,j):c(g,g.target="_blank")}else{c(g)}}else{g.href=k.createObjectURL(h);setTimeout(function(){k.revokeObjectURL(g.href)},40000);setTimeout(function(){c(g)},0)}}:"msSaveOrOpenBlob" in navigator?function f(h,i,j){i=i||h.name||"download";if(typeof h==="string"){if(d(h)){e(h,i,j)}else{var g=document.createElement("a");g.href=h;g.target="_blank";setTimeout(function(){c(g)})}}else{navigator.msSaveOrOpenBlob(b(h,j),i)}}:function f(g,k,l,m){m=m||open("","_blank");if(m){m.document.title=m.document.body.innerText="downloading..."}if(typeof g==="string"){return e(g,k,l)}var h=g.type==="application/octet-stream";var j=/constructor/i.test(a.HTMLElement)||a.safari;var i=/CriOS\/[\d]+/.test(navigator.userAgent);if((i||h&&j)&&typeof FileReader==="object"){var n=new FileReader();n.onloadend=function(){var q=n.result;q=i?q:q.replace(/^data:[^;]*;/,"data:attachment/file;");if(m){m.location.href=q}else{location=q}m=null};n.readAsDataURL(g)}else{var p=a.URL||a.webkitURL;var o=p.createObjectURL(g);if(m){m.location=o}else{location.href=o}m=null;setTimeout(function(){p.revokeObjectURL(o)},40000)}});a.saveAs=f.saveAs=f;if(typeof module!=="undefined"){module.exports=f}});var userInputFocus=0;var args=parseUriArgs();var socket=null;var state=0;var random=Math.random();var webrtcSessions={};var webchannel=null;var localStream=null;var remoteStream=null;var multiWebRtc=true;var userMediaSupport=0;getUserMediaSupport(function(a){userMediaSupport=a});var webrtcconfiguration="{{{webrtconfig}}}";if(webrtcconfiguration==""){webrtcconfiguration=null}else{try{webrtcconfiguration=JSON.parse(decodeURIComponent(webrtcconfiguration))}catch(ex){console.log('Invalid WebRTC config: "'+webrtcconfiguration+'".');webrtcconfiguration=null}}var fileUploads=[];var fileDownloads={};var currentFileUpload=null;var currentFileDownload=null;if(args.title){QH("xtitle"," - "+args.title);document.title=document.title+" - "+args.title}document.addEventListener("dragover",haltEvent,false);document.addEventListener("dragleave",haltEvent,false);document.addEventListener("drop",fileDrop,false);document.onkeyup=function ondockeypress(a){if(state==2){if((a.keyCode==8)&&(userInputFocus==0)){var b=Q("xouttext").value;if(b.length>0){Q("xouttext").value=b.substring(0,b.length-1)}}}if(userInputFocus==0){haltEvent(a);return false}};document.onkeypress=function ondockeypress(a){if(state==2){if(a.keyCode==13){xsend(a)}else{if((userInputFocus==0)&&(a.key.length==1)){Q("xouttext").value=Q("xouttext").value+a.key}}}if(userInputFocus==0){haltEvent(a);return false}};function onUserInputFocus(a){userInputFocus=a}function displayClear(){QH("xmsg","");cancelAllFileTransfers();fileUploads=[],fileDownloads={}}if(!FileReader.prototype.readAsBinaryString){FileReader.prototype.readAsBinaryString=function(b){var a="",d=this,c=new FileReader();c.onload=function(g){var f=new Uint8Array(c.result);for(var h=0;h'+a+"
");Q("xmsg").scrollTop=Q("xmsg").scrollHeight}function displayLocalVideo(a){QV("localVideo",a);adjustVideoWindows()}function displayRemoteVideo(a){QV("remoteVideo",a);adjustVideoWindows()}function adjustVideoWindows(){var a=(QS("remoteVideo")["display"]!="none");QS("localVideo")["top"]=a?"320px":"45px"}function displayRemote(a){QA("xmsg",'");Q("xmsg").scrollTop=Q("xmsg").scrollHeight}function xsend(a){var b=Q("xouttext").value;if(b.length>0){Q("xouttext").value="";QA("xmsg",'");Q("xmsg").scrollTop=Q("xmsg").scrollHeight;send({action:"chat",msg:b})}}function haltEvent(a){if(a.preventDefault){a.preventDefault()}if(a.stopPropagation){a.stopPropagation()}return false}function parseUriArgs(){var a,c={},b=window.document.location.href.split(/[\?&|\=]/);b.splice(0,1);for(d in b){switch(d%2){case 0:a=decodeURIComponent(b[d]);break;case 1:c[a]=decodeURIComponent(b[d]);var d=parseInt(c[a]);if(d==c[a]){c[a]=d}break;default:break}}return c}function updateControls(){QE("sendButton",state==2);QE("clearButton",state==2);QE("xouttext",state==2);QV("fileButton",state==2);QV("camButton",webchannel&&webchannel.ok&&!localStream&&(userMediaSupport==2));QV("micButton",webchannel&&webchannel.ok&&!localStream&&(userMediaSupport>0));QV("hangupButton",webchannel&&webchannel.ok&&localStream)}function startWebRTC(a,b){if((webrtcSessions[0]!=null)&&(multiWebRtc==false)){return webrtcSessions[0]}var c=null;if(typeof RTCPeerConnection!=="undefined"){c=new RTCPeerConnection(webrtcconfiguration)}else{if(typeof webkitRTCPeerConnection!=="undefined"){c=new webkitRTCPeerConnection(webrtcconfiguration)}}if(c==null){return null}c.id=a;c.onicecandidate=function(d){try{if(d.candidate!=null){sendws({action:"webRtcIce",ice:d.candidate,id:this.id})}}catch(f){}};c.oniceconnectionstatechange=function(){if(c&&c.iceConnectionState=="failed"){c.close();if(webrtcSessions[c.id]){delete webrtcSessions[c.id]}}};c.ondatachannel=function(d){webchannel=d.channel;webchannel.onmessage=function(e){processMessage(e.data,2)};webchannel.onopen=function(){webchannel.ok=true;updateControls();sendws({action:"rtcSwitch",v:0})};webchannel.onclose=function(e){if(webchannel&&webchannel.ok){disconnect()}else{hangUpButtonClick(0)}}};c.onnegotiationneeded=function(d){if(c.holdTimer!=null){return}c.holdTimer=setTimeout(function(){c.holdTimer=null;c.createOffer(function(e){c.setLocalDescription(e,function(){sendws({action:"webRtcSdp",sdp:e,id:a})},function(){hangUpButtonClick(a)})},function(){hangUpButtonClick(a)})},20)};c.ontrack=function(d){var e=Q("remoteVideoCanvas");e.srcObject=remoteStream=d.streams[0];e.onloadedmetadata=function(f){e.play()};displayRemoteVideo(true)};if(b==true){webchannel=c.createDataChannel("DataChannel",{});webchannel.onmessage=function(d){processMessage(d.data,2)};webchannel.onopen=function(){webchannel.ok=true;updateControls();sendws({action:"rtcSwitch",v:0})};webchannel.onclose=function(d){if(webchannel&&webchannel.ok){disconnect()}else{hangUpButtonClick(0)}}}webrtcSessions[a]=c;return c}function webRtcHandleOffer(b,a){var c=webrtcSessions[b];if(c){c.setRemoteDescription(new RTCSessionDescription(a),function(){if(a.type=="offer"){c.createAnswer(function(d){c.setLocalDescription(d,function(e,f){try{sendws({action:"webRtcSdp",sdp:d,id:b})}catch(g){}},function(){hangUpButtonClick(b)})},function(){hangUpButtonClick(b)})}},function(){hangUpButtonClick(b)})}}function performWebRtcSwitch(){if(webchannel&&webchannel.ok){sendws({action:"rtcSwitch",v:1});webchannel.xoutBuffer=[]}}function disconnect(){if(state>0){displayControl("Connection closed.")}if(state>1){setTimeout(start,500)}cancelAllFileTransfers();hangUpButtonClick(0,true);hangUpButtonClick(1,true);hangUpButtonClick(2,true);if(socket!=null){socket.close();socket=null}updateControls();state=0}function send(a){if(state!=2){return}if(typeof a=="object"){a=JSON.stringify(a)}if(webchannel&&webchannel.ok){if(webchannel.xoutBuffer!=null){webchannel.xoutBuffer.push(a)}else{webchannel.send(a)}}else{if(socket!=null){try{socket.send(a)}catch(b){}}}}function sendws(a){if(state!=2){return}if(typeof a=="object"){a=JSON.stringify(a)}if(socket!=null){socket.send(a)}}function webRtcIdSwitch(a){if(a==0){return 0}return 3-a}function processMessage(a,d){if(typeof a=="string"){try{a=JSON.parse(a)}catch(b){console.log("Unable to parse",a);return}switch(a.action){case"chat":displayRemote(a.msg);break;case"random":if(random>a.random){startWebRTC(0,true)}break;case"webRtcSdp":if(!webrtcSessions[webRtcIdSwitch(a.id)]){startWebRTC(webRtcIdSwitch(a.id),false)}webRtcHandleOffer(webRtcIdSwitch(a.id),a.sdp);break;case"webRtcIce":var e=webrtcSessions[webRtcIdSwitch(a.id)];if(e){try{e.addIceCandidate(new RTCIceCandidate(a.ice))}catch(b){}}break;case"videoStop":hangUpButtonClick(webRtcIdSwitch(a.id),true);break;case"rtcSwitch":switch(a.v){case 0:performWebRtcSwitch();break;case 1:sendws({action:"rtcSwitch",v:2});break;case 2:for(var c in webchannel.xoutBuffer){webchannel.send(webchannel.xoutBuffer[c])}delete webchannel.xoutBuffer;break;default:console.log("Unknown rtcSwitch value: "+a.action);break}break;case"file":startFileDownload(a);break;case"fileUploadCancel":cancelFileTransfer(a.id);break;case"fileUploadStart":if(fileDownloads[a.id]){currentFileDownload=fileDownloads[a.id];currentFileDownload.data="";changeFileInfo(a.id,2,0);continueFileDownload(a);send({action:"fileUploadAck",id:a.id})}break;case"fileUploadEnd":if(currentFileDownload&&(currentFileDownload.id==a.id)){changeFileInfo(a.id,3,200);currentFileDownload.done=1;currentFileDownload=null;send({action:"fileUploadAck",id:a.id})}currentFileDownload=null;break;case"fileUploadAck":continueFileUpload();break;case"fileData":if(currentFileDownload&&(currentFileDownload.id==a.id)){currentFileDownload.data+=a.data;changeFileInfo(a.id,2,(currentFileDownload.data.length*200/currentFileDownload.size));send({action:"fileUploadAck",id:a.id})}break;default:console.log("Unhandled object data",a);break}}else{console.log("Unhandled data",typeof a,a)}}function fileButtonClick(){var a=Q("uploadFileInput");if(a.getAttribute("eventset")!=1){a.setAttribute("eventset","1");a.addEventListener("change",fileSelect,false)}a.value=null;a.click()}function fileSelect(){if(state!=2){return}var c=Q("uploadFileInput");if(c.files.length>10){displayControl("Limit of 10 file uploads at the same time.")}else{for(var a=0;a0){var b=new FileReader();b.onload=function(d){this.xfile.data=d.target.result;startFileUpload(this.xfile)};b.xfile=c.files[a];b.readAsBinaryString(c.files[a])}}}}function fileDrop(a){haltEvent(a);if((state!=2)||(a.dataTransfer==null)){return}if(a.dataTransfer.files.length>10){displayControl("Limit of 10 file uploads at the same time.")}else{for(var b=0;b0){var c=new FileReader();c.onload=function(d){this.xfile.data=d.target.result;startFileUpload(this.xfile)};c.xfile=a.dataTransfer.files[b];c.readAsBinaryString(a.dataTransfer.files[b])}}}}function startFileUpload(a){if(state!=2){return}a.id=Math.random();fileUploads.push(a);QA("xmsg",'');Q("xmsg").scrollTop=Q("xmsg").scrollHeight;send({action:"file",size:a.size,id:a.id,type:a.type,name:a.name});if(currentFileUpload==null){continueFileUpload()}}function startFileDownload(a){if(state!=2){return}fileDownloads[a.id]=a;QA("xmsg",'');Q("xmsg").scrollTop=Q("xmsg").scrollHeight}function changeFileInfo(b,a,c,d){if(a){Q("FILEUP-ICON-"+b).classList.remove("fileicon");Q("FILEUP-ICON-"+b).classList.remove("fileiconx");Q("FILEUP-ICON-"+b).classList.remove("fileicontransfer");Q("FILEUP-ICON-"+b).classList.remove("fileicondone");Q("FILEUP-ICON-"+b).classList.add(["fileicon","fileiconx","fileicontransfer","fileicondone"][a])}if(c){QS("FILEUP-PROGRESS-"+b)["width"]=c+"px"}if(d){QS("FILEUP-PROGRESS-"+b)["background-color"]=d}}function data2blob(b){var a=new Array(b.length);for(var c=0;c0)){var a=window.location.protocol.replace("http","ws")+"//"+window.location.host+window.location.pathname.substring(0,window.location.pathname.lastIndexOf("/"))+"/meshrelay.ashx?id="+args.id;if((args.auth!=null)&&(args.auth!="")){a+="&auth="+args.auth}socket=new WebSocket(a);socket.onopen=function(){state=1;displayControl("Waiting for other user...")};socket.onerror=function(b){};socket.onclose=function(){disconnect()};socket.onmessage=function(b){if((state<2)&&(typeof b.data=="string")&&((b.data=="c")||(b.data=="cr"))){hangUpButtonClick(0,true);hangUpButtonClick(1,true);hangUpButtonClick(2,true);displayControl("Connected.");state=2;updateControls();sendws({action:"random",random:random});return}if(state==2){processMessage(b.data,1)}}}else{displayControl("Error: No connection key specified.")}}start();function onUnLoad(){for(var b=0;b<3;b++){if(webrtcSessions[b]){webrtcSessions[b].close();delete webrtcSessions[b]}}if(webchannel!=null){try{webchannel.close()}catch(a){}webchannel=null}if(socket!=null){try{socket.close()}catch(a){}socket=null}};