MeshCentral/public/scripts/amt-terminal-0.0.2-min.js
2024-07-20 10:34:18 -07:00

1 line
12 KiB
JavaScript

var CreateAmtRemoteTerminal=function(e,r){var l,m={},f=(m.DivId=e,m.DivElement=document.getElementById(e),m.protocol=1,r.protocol&&(m.protocol=r.protocol),m.terminalEmulation=1,m.fxEmulation=0,m.lineFeed="\r\n",m.debugmode=0,m.width=80,m.height=25,m.heightLock=0,["000000","BB0000","00BB00","BBBB00","0000BB","BB00BB","00BBBB","BBBBBB","555555","FF5555","55FF55","FFFF55","5555FF","FF55FF","55FFFF","FFFFFF"]),g=0,u=7,w=0,C=!0,S=0,T=0,c=0,s=0,d=0,b=[],k=0,v=0,p=[],y=[],n=!1,K=!0,D=!1,B=[],F="";m.title=null,m.onTitleChange=null,m.Start=function(){},m.Init=function(e,r){m.width=e||80,m.height=r||25;for(var t=0;t<m.height;t++){y[t]=[],p[t]=[];for(var i=0;i<m.width;i++)y[t][i]=" ",p[t][i]=448}m.TermInit(),m.TermDraw()},m.xxStateChange=function(e){},m.ProcessData=function(r){if(2==m.debugmode&&console.log("TRecv("+r.length+"): "+rstr2hex(r)),0==m.terminalEmulation)try{r=decode_utf8(F+r)}catch(e){return void(F+=r)}F="",null!=m.capture&&(m.capture+=r);for(var e=r,t=0;t<e.length;t++){o=a=h=n=i=void 0;var i=String.fromCharCode(e.charCodeAt(t)),n=e.charCodeAt(t);switch(d){case 0:27===n?(d=1,b=[],v=k=0):E(i);break;case 1:switch(i){case"[":d=2;break;case"(":d=4;break;case")":d=5;break;case"]":d=6;break;case"=":D=!0,d=0;break;case">":D=!1,d=0;break;case"7":c=S,s=T,d=0;break;case"8":S=c,T=s,d=0;break;case"M":for(var h=l[1];h>=l[0]+1;h--)for(var a=0;a<m.width;a++)y[h][a]=y[h-1][a],p[h][a]=p[h-1][a];for(h=l[0]+1-1;h>l[0]-1;h--)for(a=0;a<m.width;a++)y[h][a]=" ",p[h][a]=448;d=0;break;default:console.log("unknown terminal short code",i),d=0}break;case 2:"0"<=i&&i<="9"?b[k]?b[k]=10*b[k]+(i-"0"):b[k]=i-"0":";"==i?k++:"?"==i?v=1:(b[0]||(b[0]=0),function(e,r,t,i){if(1==i)switch(e){case"l":25==r[0]&&(K=!1);break;case"h":25==r[0]&&(K=!0)}else if(0==i){var n,h;switch(e){case"c":m.TermResetScreen();break;case"A":1==t&&(0==r[0]?T--:T-=r[0],T<0)&&(T=0);break;case"B":1==t&&(0==r[0]?T++:T+=r[0],T>m.height)&&(T=m.height);break;case"C":1==t&&(0==r[0]?S++:S+=r[0],S>m.width)&&(S=m.width);break;case"D":1==t&&(0==r[0]?S--:S-=r[0],S<0)&&(S=0);break;case"d":1==t&&(T=(T=r[0]-1)>m.height?m.height:T)<0&&(T=0);break;case"G":1==t&&(S=(S=r[0]-1)<0?0:S)>m.width-1&&(S=m.width-1);break;case"P":var a=1;for(1==t&&(a=r[0]),n=S;n<m.width-a;n++)y[T][n]=y[T][n+a],p[T][n]=p[T][n+a];for(n=m.width-a;n<m.width;n++)y[T][n]=" ",p[T][n]=448;break;case"L":var o=1;for(0==(o=1==t?r[0]:o)&&(o=1),c=l[1];T+o<=c;c--)y[c]=y[c-o],p[c]=p[c-o];for(c=T;c<T+o;c++)for(y[c]=[],p[c]=[],a=0;a<m.width;a++)y[c][a]=" ",p[c][a]=448;break;case"J":if(1==t&&2==r[0])m.TermClear((w<<12)+(u<<6)),T=S=0,B=[];else if(0==t||1==t&&0==r[0])for(L(),n=T+1;n<m.height;n++)P(n);else if(1==t&&1==r[0])for(L(),n=0;n<T-1;n++)P(n);break;case"H":S=2==t?(r[0]<1&&(r[0]=1),r[1]<1&&(r[1]=1),r[0]>m.height&&(r[0]=m.height),r[1]>m.width&&(r[1]=m.width),T=r[0]-1,r[1]-1):T=0;break;case"m":for(n=0;n<t;n++)r[n]&&0!=r[n]?1==r[n]?u<8&&(u+=8):2==r[n]||22==r[n]?8<=u&&(u-=8):7==r[n]?g=2:27==r[n]?g=0:30<=r[n]&&r[n]<=37?(h=8<=u,u=r[n]-30,h&&u<=8&&(u+=8)):40<=r[n]&&r[n]<=47?w=r[n]-40:90<=r[n]&&r[n]<=99?u=r[n]-82:100<=r[n]&&r[n]<=109&&(w=r[n]-92):(u=7,g=w=0);break;case"K":0!=t&&(1!=t||r[0]&&0!=r[0])?1==t&&(1==r[0]?function(){for(var e=(u<<6)+(w<<12)+g,r=0;r<S;r++)y[T][r]=" ",p[T][r]=e}():2==r[0]&&P(T)):L();break;case"h":C=!0;break;case"l":C=!1;break;case"r":(l=2==t?[r[0]-1,r[1]-1]:l)[0]<0&&(l[0]=0),l[0]>m.height-1&&(l[0]=m.height-1),l[1]<0&&(l[1]=0),l[1]>m.height-1&&(l[1]=m.height-1),l[1]<l[0]&&(l[0]=l[1]);break;case"S":a=1;1==t&&(a=r[0]);for(var c=l[0];c<=l[1]-a;c++)for(var f=0;f<m.width;f++)y[c][f]=y[c+a][f],p[c][f]=p[c+a][f];for(c=l[1]-a+1;c<l[1];c++)for(f=0;f<m.width;f++)y[c][f]=" ",p[c][f]=448;break;case"M":a=1;1==t&&(a=r[0]);for(c=T;c<=l[1]-a;c++)for(f=0;f<m.width;f++)y[c][f]=y[c+a][f],p[c][f]=p[c+a][f];for(c=l[1]-a+1;c<l[1];c++)for(f=0;f<m.width;f++)y[c][f]=" ",p[c][f]=448;break;case"T":a=1;1==t&&(a=r[0]);for(c=l[1];c>l[0]+a;c--)for(f=0;f<m.width;f++)y[c][f]=y[c-a][f],p[c][f]=p[c-a][f];for(c=l[0]+a;c>l[0];c--)for(f=0;f<m.width;f++)y[c][f]=" ",p[c][f]=448;break;case"X":var a=1,s=S,d=T;for(1==t&&(a=r[0]);0<a&&d<m.height;)y[d][s]=" ",a--,++s>=m.width&&(s=0,d++);break;default:console.log("Unknown terminal code",e,r,i)}}}(i,b,k+1,v),d=0);break;case 4:case 5:d=0;break;case 6:var o=i.charCodeAt(0);";"==i?k++:7==o?(function(e){var r;0!=e.length&&(0==(r=parseInt(e[0]))||2==r)&&1<e.length&&"?"!=e[1]&&m.onTitleChange&&m.onTitleChange(m,m.title=e[1])}(b),d=0):b[k]?b[k]+=i:b[k]=i}}m.TermDraw()},m.ProcessVt100String=function(e){for(var r=0;r<e.length;r++)E(String.fromCharCode(e.charCodeAt(r)))};var h=[199,252,233,226,228,224,229,231,234,235,232,239,238,236,196,197,201,230,198,244,246,242,251,249,255,214,220,162,163,165,8359,402,225,237,243,250,241,209,170,218,191,8976,172,189,188,161,171,187,9619,9618,9617,9474,9508,9569,9570,9558,9557,9571,9553,9559,9565,9564,9563,9488,9492,9524,9516,9500,9472,9532,9566,9567,9562,9556,9577,9574,9568,9552,9580,9575,9576,9572,9573,9576,9560,9554,9555,9579,9578,9496,9484,9608,9604,9611,9616,9600,945,223,915,960,931,963,181,964,966,952,8486,948,8734,248,949,8719,8801,177,8805,8806,8992,8993,247,8776,176,8226,183,8730,8319,178,8718,160],a=[199,252,233,226,228,224,229,231,234,235,232,239,238,236,196,197,201,230,198,244,246,242,251,249,255,214,220,162,163,165,8359,402,225,237,243,250,241,209,170,218,191,8976,172,189,188,161,174,187,9619,9618,9617,9474,9508,9569,9570,9558,9557,9571,9553,9559,9565,9564,9563,9488,9492,9524,9516,9500,9472,9532,9566,9567,9562,9556,9577,9574,9568,9552,9580,9575,9576,9572,9573,9576,9560,9554,9555,9579,9578,9496,9484,9608,9604,9611,9616,9600,945,223,915,960,931,963,181,964,966,952,8486,948,8734,248,949,8719,8801,177,8805,8806,8992,8993,247,8776,176,8226,183,8730,8319,178,8718,160],h=[199,252,233,226,228,224,229,231,234,235,232,239,238,236,196,197,201,230,198,244,246,242,251,249,255,214,220,162,163,165,8359,402,225,237,243,250,241,209,170,218,191,8976,172,189,188,161,171,187,9619,9618,9617,9474,9508,9569,9570,9558,9557,9571,9553,9559,9565,9564,9563,9488,9492,9524,9516,9500,9472,9532,9566,9567,9562,9556,9577,9574,9568,9552,9580,9575,9576,9572,9573,9576,9560,9554,9555,9579,9578,9496,9484,9608,9604,9611,9616,9600,945,223,915,960,931,963,181,964,966,952,8486,948,8734,248,949,8719,8801,177,8805,8806,8992,8993,247,8776,176,8226,183,8730,8319,178,8718,160],a=[199,252,233,226,228,224,229,231,234,235,232,239,238,236,196,197,201,230,198,244,246,242,251,249,255,214,220,162,163,165,8359,402,225,237,243,250,241,209,170,218,191,8976,172,189,188,161,174,187,9619,9618,9617,9474,9508,9569,9570,9558,9557,9571,9553,9559,9565,9564,9563,9488,9492,9524,9516,9500,9472,9532,9566,9567,9562,9556,9577,9574,9568,9552,9580,9575,9576,9572,9573,9576,9560,9554,9555,9579,9578,9496,9484,9608,9604,9611,9616,9600,945,223,915,960,931,963,181,964,966,952,8486,948,8734,248,949,8719,8801,177,8805,8806,8992,8993,247,8776,176,8226,183,8730,8319,178,8718,160];function E(e){if("\0"!=e&&7!=e.charCodeAt()){var r=e.charCodeAt();switch(1==m.terminalEmulation?0!=(128&r)&&(e=String.fromCharCode(h[127&r])):2==m.terminalEmulation&&0!=(128&r)&&(e=String.fromCharCode(a[127&r])),r){case 16:e=" ";break;case 24:e="↑";break;case 25:e="↓"}switch(S>m.width&&(S=m.width),T>m.height-1&&(T=m.height-1),e){case"\b":0<S&&(S--,n)&&o(" ");break;case"\t":for(var t=8-S%8,i=0;i<t;i++)E(" ");break;case"\n":++T>l[1]&&(m.recordLineTobackBuffer(0),x(1),T=l[1]),m.lineFeed="\r",S=0;break;case"\r":S=0;break;default:S>=m.width&&(S=0,C&&T++,T>=m.height-1)&&(x(1),T=m.height-1),o(e),S++}}}function o(e){y[T][S]=e,p[T][S]=(u<<6)+(w<<12)+g}function L(){for(var e=(u<<6)+(w<<12)+g,r=S;r<m.width;r++)y[T][r]=" ",p[T][r]=e}function P(e){for(var r=(u<<6)+(w<<12)+g,t=0;t<m.width;t++)y[e][t]=" ",p[e][t]=r}function x(e){for(var r,t=l[0];t<=l[1]-e;t++)y[t]=y[t+e],p[t]=p[t+e];for(t=l[1]-e+1;t<=l[1];t++)for(y[t]=[],p[t]=[],r=0;r<m.width;r++)y[t][r]=" ",p[t][r]=448}return m.TermClear=function(e){for(var r=0;r<m.height;r++)for(var t=0;t<m.width;t++)y[r][t]=" ",p[r][t]=e;B=[]},m.TermResetScreen=function(){C=K=!(w=g=0),S=T=0,n=!(u=7),l=[0,m.height-1],D=!1,m.TermClear(448),F=""},m.TermSendKeys=function(e){2==m.debugmode&&console.log("TSend("+e.length+"): "+rstr2hex(e),e),m.parent.send(e)},m.TermSendKey=function(e){2==m.debugmode&&console.log("TSend(1): "+rstr2hex(String.fromCharCode(e)),e),m.parent.send(String.fromCharCode(e))},m.TermHandleKeys=function(e){if(!e.ctrlKey)return 127==e.which?m.TermSendKey(8):13==e.which?m.TermSendKeys(m.lineFeed):0!=e.which&&m.TermSendKey(e.which),!1;e.preventDefault&&e.preventDefault(),e.stopPropagation&&e.stopPropagation()},m.TermHandleKeyUp=function(e){return 8!=e.which&&32!=e.which&&9!=e.which||(e.preventDefault&&e.preventDefault(),e.stopPropagation&&e.stopPropagation(),!1)},m.TermHandleKeyDown=function(e){if(!(65<=e.which&&e.which<=90&&1==e.ctrlKey)){if(27==e.which)return m.TermSendKeys(String.fromCharCode(27)),!0;if(1==D){if(37==e.which)return m.TermSendKeys(String.fromCharCode(27,79,68)),!0;if(38==e.which)return m.TermSendKeys(String.fromCharCode(27,79,65)),!0;if(39==e.which)return m.TermSendKeys(String.fromCharCode(27,79,67)),!0;if(40==e.which)return m.TermSendKeys(String.fromCharCode(27,79,66)),!0}else{if(37==e.which)return m.TermSendKeys(String.fromCharCode(27,91,68)),!0;if(38==e.which)return m.TermSendKeys(String.fromCharCode(27,91,65)),!0;if(39==e.which)return m.TermSendKeys(String.fromCharCode(27,91,67)),!0;if(40==e.which)return m.TermSendKeys(String.fromCharCode(27,91,66)),!0}if(33==e.which)return m.TermSendKeys(String.fromCharCode(27,91,53,126)),!0;if(34==e.which)return m.TermSendKeys(String.fromCharCode(27,91,54,126)),!0;if(35==e.which)return m.TermSendKeys(String.fromCharCode(27,91,70)),!0;if(36==e.which)return m.TermSendKeys(String.fromCharCode(27,91,72)),!0;if(45==e.which)return m.TermSendKeys(String.fromCharCode(27,91,50,126)),!0;if(46==e.which)return m.TermSendKeys(String.fromCharCode(27,91,51,126)),!0;if(9==e.which)return m.TermSendKeys("\t"),e.preventDefault&&e.preventDefault(),e.stopPropagation&&e.stopPropagation(),!0;var r=[80,81,119,120,116,117,113,114,112,77],t=[49,50,51,52,53,54,55,56,57,48,33,64],i=[80,81,82,83,84,85,86,87,88,89,90,91];if(111<e.which&e.which<124&&0==e.repeat){if(0==m.fxEmulation&&e.which<122)return m.TermSendKeys(String.fromCharCode(27,91,79,r[e.which-112])),!0;if(1==m.fxEmulation)return m.TermSendKeys(String.fromCharCode(27,t[e.which-112])),!0;if(2==m.fxEmulation)return m.TermSendKeys(String.fromCharCode(27,79,i[e.which-112])),!0}r=[80,81,119,120,116,117,113,114,112,77];return 111<e.which&e.which<122&&0==e.repeat?(m.TermSendKeys(String.fromCharCode(27,91,79,r[e.which-112])),!0):(t=[49,50,51,52,53,54,55,56,57,48,33,64],111<e.which&e.which<124&&0==e.repeat?(m.TermSendKeys(String.fromCharCode(27,t[e.which-112])),!0):(i=[80,81,82,83,84,85,86,87,88,89,90,91],111<e.which&e.which<124&&0==e.repeat?(m.TermSendKeys(String.fromCharCode(27,79,i[e.which-112])),!0):8!=e.which&&32!=e.which&&9!=e.which||(m.TermSendKey(e.which),e.preventDefault&&e.preventDefault(),e.stopPropagation&&e.stopPropagation(),!1)))}m.TermSendKey(e.which-64),e.preventDefault&&e.preventDefault(),e.stopPropagation&&e.stopPropagation()},m.recordLineTobackBuffer=function(e){var e=m.TermDrawLine("",e,""),r=e[0];B.push(r+e[1]+"<br>")},m.TermDrawLine=function(e,r,t){for(var i,n,h,a,o=1,c=0;c<m.width;++c)switch(i=p[r][c],S==c&&T==r&&K&&(i|=2),i!=o&&(h=6,a=12,2&i&&(h=12,a=6),e=e+t+'<span style="color:#'+f[i>>h&63]+";background-color:#"+f[i>>a&63],1&i&&(e+=";text-decoration:underline"),e+=';">',t="</span>"+(t=""),o=i),n=y[r][c]){case"&":e+="&amp;";break;case"<":e+="&lt;";break;case">":e+="&gt;";break;case" ":e+="&nbsp;";break;default:e+=n}return[e,t]},m.TermDraw=function(){for(var e="",r="",t=0;t<m.height;++t){var i=m.TermDrawLine(r,t,e),r=i[0],e=i[1];t!=m.height-1&&(r+="<br>")}var n=(B=800<B.length?B.slice(B.length-800):B).join("");m.DivElement.innerHTML="<font size='4'><b>"+n+r+e+"</b></font>",m.DivElement.scrollTop=m.DivElement.scrollHeight,0==m.heightLock&&setTimeout(m.TermLockHeight,10)},m.TermLockHeight=function(){m.heightLock=m.DivElement.clientHeight,m.DivElement.style.height=m.DivElement.parentNode.style.height=m.heightLock+"px",m.DivElement.style["overflow-y"]="scroll"},m.TermInit=function(){m.TermResetScreen()},m.heightLock=0,m.DivElement.style.height="",null!=r&&null!=r.cols&&null!=r.rows?m.Init(r.cols,r.rows):m.Init(),m}