説明なし
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

connect.html 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1">
  6. <title>BizGaze Support — Agent Console</title>
  7. <style>
  8. :root{ --brand:#FFC708; --brand-d:#E0AC00; --blue:#1F3B73; --blue-d:#16294f; --ink:#1f2430; --muted:#6b7280; --bg:#f6f8fb; --card:#fff; --line:#e6e9ef; }
  9. *{box-sizing:border-box;}
  10. body{font-family:'Segoe UI',system-ui,sans-serif;background:var(--bg);color:var(--ink);margin:0;}
  11. .topbar{background:var(--blue);padding:.7rem 1.2rem;display:flex;align-items:center;justify-content:space-between;gap:.6rem;}
  12. .brandrow{display:flex;align-items:center;gap:.6rem;}
  13. .logo{width:28px;height:28px;border-radius:7px;background:var(--brand);display:grid;place-items:center;font-weight:800;color:var(--blue);}
  14. .brand{font-weight:700;color:#fff;} .brand span{color:var(--brand);font-weight:600;}
  15. .agentchip{color:#dbe4f5;font-size:.85rem;}
  16. .agentchip b{color:#fff;} .agentchip a{color:var(--brand);text-decoration:none;margin-left:.5rem;cursor:pointer;}
  17. .wrap{min-height:calc(100vh - 50px);display:grid;place-items:center;padding:1.5rem;}
  18. .card{background:var(--card);border:1px solid var(--line);border-radius:18px;padding:2.2rem;max-width:430px;width:100%;box-shadow:0 10px 30px rgba(20,30,60,.06);}
  19. h1{font-size:1.35rem;margin:.1rem 0 .3rem;color:var(--blue);text-align:center;}
  20. .sub{color:var(--muted);font-size:.92rem;margin-bottom:1.3rem;text-align:center;}
  21. .lbl{display:block;font-size:.74rem;color:var(--muted);text-transform:uppercase;letter-spacing:.06em;margin:.85rem 0 .25rem;}
  22. input{width:100%;padding:.7rem;border-radius:10px;border:2px solid var(--line);background:#fbfcfe;color:var(--ink);font-size:1rem;}
  23. input:focus{outline:none;border-color:var(--brand);}
  24. input.code{font-size:1.7rem;letter-spacing:.35rem;text-align:center;}
  25. .btn{margin-top:1.1rem;width:100%;padding:.85rem;background:var(--brand);color:var(--ink);border:none;border-radius:10px;font-weight:700;font-size:1.02rem;cursor:pointer;}
  26. .btn:hover{background:var(--brand-d);}
  27. .status{color:var(--muted);font-size:.88rem;margin-top:.9rem;text-align:center;min-height:1.1em;}
  28. .err{color:#b91c1c;}
  29. .prefill{background:#EAF0FB;border:1px solid #c7d6f0;border-radius:8px;padding:.5rem .7rem;font-size:.85rem;color:var(--blue-d);margin-top:.25rem;}
  30. .topbar2{background:var(--card);border-bottom:1px solid var(--line);padding:.5rem 1rem;display:none;justify-content:space-between;align-items:center;}
  31. .topbar2.show{display:flex;} #barStatus{font-weight:600;font-size:.9rem;color:var(--blue);}
  32. #endBtn{padding:.45rem 1rem;background:#fee2e2;color:#b91c1c;border:none;border-radius:8px;font-weight:600;cursor:pointer;}
  33. #video{width:100vw;height:calc(100vh - 46px);background:#0b1220;object-fit:contain;display:none;cursor:crosshair;outline:none;}
  34. .profile{position:relative}
  35. .profile .pbtn{display:flex;align-items:center;gap:.4rem;background:rgba(255,255,255,.14);color:#fff;border:1px solid #46598c;border-radius:10px;padding:.45rem .85rem;font-weight:600;font-size:.88rem;cursor:pointer}
  36. .profile .pbtn:hover{background:rgba(255,255,255,.24)}
  37. .profile .pmenu{position:absolute;right:0;top:calc(100% + 6px);background:#fff;border:1px solid #e6e9ef;border-radius:10px;box-shadow:0 10px 28px rgba(0,0,0,.18);min-width:190px;overflow:hidden;z-index:5000;display:none}
  38. .profile .pmenu.open{display:block}
  39. .profile .pmenu a{display:block;padding:.6rem .9rem;color:#1f2430;text-decoration:none;font-size:.9rem;cursor:pointer}
  40. .profile .pmenu a:hover{background:#f1f5f9}
  41. .profile .pmenu a.danger{color:#b91c1c;border-top:1px solid #eef1f6}
  42. </style>
  43. </head>
  44. <body>
  45. <div class="topbar" id="topbar">
  46. <div class="brandrow"><img src="/logo.png" alt="" style="height:46px;width:auto;max-width:190px;border-radius:8px;object-fit:contain;background:#fff;padding:5px 12px;image-rendering:-webkit-optimize-contrast" onerror="this.replaceWith(Object.assign(document.createElement('div'),{className:'logo',textContent:'B'}))"><div class="brand">BizGaze <span>Support</span></div></div>
  47. <div class="agentchip" id="agentChip"></div>
  48. </div>
  49. <div class="topbar2" id="bar"><span id="barStatus"></span><button id="endBtn">End session</button></div>
  50. <div class="wrap" id="wrap"><div class="card" id="card"></div></div>
  51. <video id="video" autoplay playsinline muted tabindex="0"></video>
  52. <script>
  53. let ICE={iceServers:[{urls:'stun:stun.l.google.com:19302'}]};
  54. try{fetch('/api/ice').then(r=>r.ok?r.json():null).then(c=>{if(c&&c.iceServers)ICE=c;}).catch(()=>{});}catch(_){}
  55. function pEsc(s){return String(s==null?'':s).replace(/[&<>"]/g,c=>({'&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;'}[c]));}
  56. function profileHTML(name){return '<div class="profile"><button class="pbtn" id="pbtn">'+pEsc(name)+' <span style="font-size:.65rem">&#9662;</span></button><div class="pmenu" id="pmenu"><a href="/console">Console / Dashboard</a><a id="plogout" class="danger">Logout</a></div></div>';}
  57. function wireProfile(){const btn=document.getElementById('pbtn'),menu=document.getElementById('pmenu');if(!btn)return;btn.onclick=(e)=>{e.stopPropagation();menu.classList.toggle('open');};document.addEventListener('click',()=>menu.classList.remove('open'));const lo=document.getElementById('plogout');if(lo)lo.onclick=async()=>{try{await fetch('/api/logout',{method:'POST'});}catch(_){}location.href='/';};}
  58. function makeBrandClickable(){document.querySelectorAll('.brandrow,.wordmark').forEach(el=>{el.style.cursor='pointer';el.addEventListener('click',()=>{location.href='/';});});}
  59. makeBrandClickable();
  60. const params=new URLSearchParams(location.search);
  61. const presetTicket=params.get('ticket')||'';
  62. const presetCode=params.get('code')||'';
  63. const card=document.getElementById('card'), wrap=document.getElementById('wrap'),
  64. agentChip=document.getElementById('agentChip'), bar=document.getElementById('bar'),
  65. topbar=document.getElementById('topbar'), video=document.getElementById('video'), barStatus=document.getElementById('barStatus');
  66. let ws,pc,inputChannel,chatChannel,sessionId,me=null;
  67. async function api(path,body,method='POST'){
  68. const opt={method,headers:{'Content-Type':'application/json'}};
  69. if(body)opt.body=JSON.stringify(body);
  70. const r=await fetch(path,opt); const data=await r.json().catch(()=>({}));
  71. if(!r.ok) throw new Error(data.error||'request failed'); return data;
  72. }
  73. function onEnter(ids, fn){ ids.forEach(id => { const el=document.getElementById(id); if(el) el.addEventListener('keydown', e=>{ if(e.key==='Enter'){ e.preventDefault(); fn(); } }); }); }
  74. // ---- boot: are we a logged-in agent? ----
  75. (async function boot(){
  76. try{ me=await api('/api/me',null,'GET'); renderAgent(); }
  77. catch{ renderLogin(); }
  78. })();
  79. // ---- LOGIN ----
  80. function renderLogin(){
  81. agentChip.textContent='';
  82. card.innerHTML = `
  83. <h1>Agent sign in</h1>
  84. <div class="sub">Sign in to start a support session. Your name is shown to the customer.</div>
  85. <span class="lbl">Email</span><input id="email" type="email" placeholder="you@bizgaze.com">
  86. <span class="lbl">Password</span><input id="pw" type="password" placeholder="password">
  87. <label style="display:flex;align-items:center;gap:.5rem;margin:.7rem 0;color:var(--ink);font-size:.9rem;cursor:pointer"><input type="checkbox" id="rememberMe" style="width:18px;height:18px;accent-color:var(--blue);margin:0"> Remember me on this device</label>
  88. <button class="btn" id="loginBtn" style="width:100%">Sign in</button>
  89. <div class="status err" id="err"></div>`;
  90. {
  91. const doSignIn=async()=>{
  92. try{
  93. await api('/api/login',{email:document.getElementById('email').value,password:document.getElementById('pw').value,remember:(document.getElementById('rememberMe')||{}).checked||false});
  94. me=await api('/api/me',null,'GET'); renderAgent();
  95. }catch(e){ document.getElementById('err').textContent=e.message; }
  96. };
  97. document.getElementById('loginBtn').onclick=doSignIn;
  98. onEnter(['email','pw'], doSignIn);
  99. }
  100. }
  101. // ---- AGENT CONNECT ----
  102. function renderAgent(){
  103. const displayName = me.name || me.email;
  104. agentChip.innerHTML = profileHTML(displayName); wireProfile();
  105. card.innerHTML = `
  106. <h1>Start a support session</h1>
  107. <div class="sub">Ask the customer to open the share page and read you their code.</div>
  108. <span class="lbl">Ticket number (optional)</span>
  109. <input id="ticketInput" maxlength="40" placeholder="e.g. TKT-1042 — leave blank for a direct session" value="${esc(presetTicket)}" ${presetTicket?'readonly':''}>
  110. ${presetTicket?'<div class="prefill">Linked from service request '+esc(presetTicket)+'</div>':''}
  111. <span class="lbl">Session code from customer</span>
  112. <input id="codeInput" class="code" maxlength="6" inputmode="numeric" placeholder="000000" value="${esc(presetCode)}">
  113. <button class="btn" id="connectBtn">Connect</button>
  114. <div class="status" id="status">Enter the customer's code to begin.</div>`;
  115. connectWS();
  116. document.getElementById('connectBtn').onclick=startConnect;
  117. onEnter(['ticketInput','codeInput'], startConnect);
  118. }
  119. async function startConnect(){
  120. const statusEl=document.getElementById('status'); statusEl.className='status';
  121. const ticket=document.getElementById('ticketInput').value.trim();
  122. const code=document.getElementById('codeInput').value.trim();
  123. if(!/^\d{6}$/.test(code)){ statusEl.textContent='Please enter the 6-digit code.'; return; }
  124. statusEl.textContent='Connecting…';
  125. ws.send(JSON.stringify({type:'code-connect',code,ticket}));
  126. }
  127. function connectWS(){
  128. ws=new WebSocket((location.protocol==='https:'?'wss://':'ws://')+location.host+'/ws');
  129. ws.onmessage=async(e)=>{const m=JSON.parse(e.data);const statusEl=document.getElementById('status');switch(m.type){
  130. case 'code-pending': sessionId=m.sessionId; renderWaiting(); setupPeer(); break;
  131. case 'session-ready': if(statusEl)statusEl.textContent='Allowed — connecting…'; break;
  132. case 'offer': await pc.setRemoteDescription(new RTCSessionDescription(m.sdp));
  133. try{ const mic=await navigator.mediaDevices.getUserMedia({audio:true}); window.__mic=mic; mic.getAudioTracks().forEach(t=>pc.addTrack(t,mic)); }catch(e){}
  134. const ans=await pc.createAnswer(); await pc.setLocalDescription(ans);
  135. ws.send(JSON.stringify({type:'answer',sessionId,sdp:pc.localDescription})); break;
  136. case 'ice-candidate': if(m.candidate&&pc) await pc.addIceCandidate(new RTCIceCandidate(m.candidate)); break;
  137. case 'session-denied': renderEnded('The customer declined the request.'); break;
  138. case 'session-ended': {
  139. const msgs={'share-cancelled':'The customer cancelled the screen selection. Ask them to refresh their page for a new code.',
  140. 'customer-ended':'The customer stopped sharing their screen. Ask them to refresh their page for a new code.',
  141. 'agent-ended':'You ended the session.'};
  142. renderEnded(msgs[m.reason]||'The session has ended.'); break;
  143. }
  144. case 'error': if(statusEl){statusEl.className='status err';statusEl.textContent=m.message;} break;
  145. }};
  146. }
  147. function renderWaiting(){
  148. card.innerHTML=`
  149. <h1>Waiting for the customer…</h1>
  150. <div class="sub">Code accepted. The customer has been asked to tap <b>Allow</b> on their screen.</div>
  151. <div class="status" id="status">Waiting for the customer to tap Allow…</div>
  152. <button class="btn" id="cancelBtn" style="background:#eef1f6;color:#1F3B73">Cancel</button>`;
  153. document.getElementById('cancelBtn').onclick=()=>{ try{ws.send(JSON.stringify({type:'end-session',sessionId}));}catch(e){} location.reload(); };
  154. }
  155. function renderEnded(msg){
  156. removeSessionUI();
  157. if(pc){ try{pc.close();}catch(e){} pc=null; }
  158. video.style.display='none'; bar.classList.remove('show');
  159. topbar.style.display='flex'; wrap.style.display='grid';
  160. card.innerHTML=`
  161. <h1>Session ended</h1>
  162. <div class="sub">${esc(msg)}</div>
  163. <button class="btn" id="againBtn">Start a new session</button>`;
  164. document.getElementById('againBtn').onclick=()=>location.reload();
  165. }
  166. let chatOpen=false;
  167. const SVG_MIC='<svg viewBox="0 0 24 24" width="18" height="18" fill="none" stroke="#fff" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect x="9" y="2" width="6" height="11" rx="3"/><path d="M5 10a7 7 0 0 0 14 0"/><line x1="12" y1="19" x2="12" y2="22"/></svg>';
  168. const SVG_MICOFF='<svg viewBox="0 0 24 24" width="18" height="18" fill="none" stroke="#fff" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><line x1="3" y1="3" x2="21" y2="21"/><path d="M9 9v4a3 3 0 0 0 5 2.2"/><path d="M5 10a7 7 0 0 0 10.9 5.6"/><line x1="12" y1="19" x2="12" y2="22"/></svg>';
  169. const SVG_CHAT='<svg viewBox="0 0 24 24" width="18" height="18" fill="none" stroke="#fff" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2z"/></svg>';
  170. const SVG_END='<svg viewBox="0 0 24 24" width="17" height="17" fill="#fff"><rect x="5" y="5" width="14" height="14" rx="2.5"/></svg>';
  171. function _btn(id,svg,label,bg){const b=document.createElement('button');b.id=id;b.innerHTML='<span style="display:inline-flex">'+svg+'</span><span>'+label+'</span>';b.style.cssText='display:inline-flex;align-items:center;gap:7px;border:none;border-radius:12px;padding:.62rem 1rem;font-weight:600;font-size:.9rem;cursor:pointer;color:#fff;background:'+bg+';transition:background .15s,transform .08s';b.onmouseenter=()=>b.style.transform='translateY(-1px)';b.onmouseleave=()=>b.style.transform='none';return b;}
  172. function buildBar(){
  173. if(document.getElementById('sessionBar'))return;
  174. const bar=document.createElement('div'); bar.id='sessionBar';
  175. bar.style.cssText='position:fixed;left:50%;bottom:18px;transform:translateX(-50%);z-index:2147483000;display:flex;gap:10px;align-items:center;background:rgba(15,23,42,.94);padding:8px 12px;border-radius:16px;box-shadow:0 10px 28px rgba(0,0,0,.35)';
  176. const mic=_btn('micBtn',SVG_MIC,'Mic','#2563eb');
  177. const chat=_btn('chatBtn',SVG_CHAT,'Chat','#475569');
  178. const end=_btn('endBtn2',SVG_END,'End','#dc2626');
  179. bar.appendChild(mic);bar.appendChild(chat);bar.appendChild(end);
  180. document.body.appendChild(bar);
  181. mic.onclick=()=>{const m=window.__mic;if(!m)return;const t=m.getAudioTracks()[0];if(!t)return;t.enabled=!t.enabled;mic.innerHTML='<span style="display:inline-flex">'+(t.enabled?SVG_MIC:SVG_MICOFF)+'</span><span>'+(t.enabled?'Mic':'Muted')+'</span>';mic.style.background=t.enabled?'#2563eb':'#6b7280';};
  182. chat.onclick=toggleChat;
  183. end.onclick=()=>{ try{ws.send(JSON.stringify({type:'end-session',sessionId,reason:'agent-ended'}));}catch(_){} };
  184. buildChatPanel();
  185. document.addEventListener('pointerdown',ensureAudio,{once:true});
  186. document.addEventListener('keydown',ensureAudio,{once:true});
  187. if('Notification' in window && Notification.permission==='default'){ try{Notification.requestPermission();}catch(_){}}
  188. }
  189. function buildChatPanel(){
  190. if(document.getElementById('chatPanel'))return;
  191. const p=document.createElement('div'); p.id='chatPanel';
  192. p.style.cssText='position:fixed;right:18px;bottom:84px;width:300px;max-width:92vw;height:360px;max-height:62vh;z-index:2147483001;background:#fff;border:1px solid #e6e9ef;border-radius:16px;box-shadow:0 14px 34px rgba(0,0,0,.28);display:none;flex-direction:column;overflow:hidden';
  193. p.innerHTML='<div style="background:#1F3B73;color:#fff;padding:.6rem .85rem;font-weight:600;font-size:.92rem;display:flex;justify-content:space-between;align-items:center">Chat <span id="chatClose" style="cursor:pointer;opacity:.85">&#10005;</span></div><div id="chatMsgs" style="flex:1;overflow-y:auto;padding:.6rem;display:flex;flex-direction:column;gap:.4rem;font-size:.85rem"></div><div style="display:flex;gap:.4rem;padding:.5rem;border-top:1px solid #e6e9ef"><input id="chatInput" placeholder="Type a message..." style="flex:1;padding:.5rem;border:1px solid #e6e9ef;border-radius:8px;font-size:.85rem;outline:none"><button id="chatSend" style="background:#FFC708;border:none;border-radius:8px;padding:.5rem .85rem;font-weight:700;cursor:pointer">Send</button></div>';
  194. document.body.appendChild(p);
  195. document.getElementById('chatSend').onclick=sendChat;
  196. document.getElementById('chatClose').onclick=toggleChat;
  197. document.getElementById('chatInput').addEventListener('keydown',e=>{if(e.key==='Enter')sendChat();});
  198. }
  199. function toggleChat(){const p=document.getElementById('chatPanel');if(!p)return;chatOpen=!chatOpen;p.style.display=chatOpen?'flex':'none';const b=document.getElementById('chatBtn');if(chatOpen){b&&(b.style.background='#475569');const i=document.getElementById('chatInput');if(i)setTimeout(()=>i.focus(),50);}}
  200. function addChat(msg){const c=document.getElementById('chatMsgs');if(!c)return;const mine=msg.from==='__self';const w=document.createElement('div');w.style.cssText='max-width:85%;padding:.4rem .6rem;border-radius:10px;'+(mine?'align-self:flex-end;background:#EAF0FB;color:#16294f':'align-self:flex-start;background:#f1f5f9;color:#1f2430');w.innerHTML='<div style="font-size:.7rem;opacity:.65;margin-bottom:2px">'+esc(msg.name||'')+'</div>'+esc(msg.text);c.appendChild(w);c.scrollTop=c.scrollHeight;if(!mine)notifyMsg(msg);}
  201. function notifyMsg(msg){const b=document.getElementById('chatBtn');if(b&&!chatOpen){b.style.background='#FFC708';b.style.color='#1f2430';}toast((msg.name||'Message')+': '+msg.text);try{beep();}catch(_){}if('Notification' in window && Notification.permission==='granted' && (document.hidden||!chatOpen)){try{new Notification('New message from '+(msg.name||'support'),{body:msg.text});}catch(_){}}}
  202. function toast(text){let t=document.getElementById('msgToast');if(!t){t=document.createElement('div');t.id='msgToast';t.style.cssText='position:fixed;top:18px;left:50%;transform:translateX(-50%);z-index:2147483600;background:#16a34a;color:#fff;padding:.7rem 1.1rem;border-radius:12px;box-shadow:0 10px 26px rgba(0,0,0,.35);font-size:.92rem;font-weight:600;border:2px solid #0c7a36;max-width:82vw;transition:opacity .4s';document.body.appendChild(t);}t.innerHTML='\ud83d\udcac '+text;t.style.opacity='1';clearTimeout(window.__toastT);window.__toastT=setTimeout(()=>{t.style.opacity='0';},2800);}
  203. let __ac=null;
  204. function ensureAudio(){try{__ac=__ac||new (window.AudioContext||window.webkitAudioContext)();if(__ac.state==='suspended')__ac.resume();}catch(_){}}
  205. function beep(){ensureAudio();if(!__ac)return;try{const o=__ac.createOscillator(),g=__ac.createGain();o.type='sine';o.connect(g);g.connect(__ac.destination);const t0=__ac.currentTime;o.frequency.setValueAtTime(880,t0);o.frequency.setValueAtTime(660,t0+0.09);g.gain.setValueAtTime(0.0001,t0);g.gain.exponentialRampToValueAtTime(0.12,t0+0.02);g.gain.exponentialRampToValueAtTime(0.0001,t0+0.22);o.start(t0);o.stop(t0+0.24);}catch(_){}}
  206. try{['pointerdown','keydown','touchstart','click'].forEach(ev=>document.addEventListener(ev,ensureAudio,{passive:true}));}catch(_){}
  207. function sendChat(){const i=document.getElementById('chatInput');if(!i)return;const t=i.value.trim();if(!t)return;if(chatChannel&&chatChannel.readyState==='open'){chatChannel.send(JSON.stringify({name:(me&&(me.name||me.email))||'Support agent',text:t}));}addChat({from:'__self',name:'You',text:t});i.value='';}
  208. function removeSessionUI(){['sessionBar','chatPanel','remoteAudio','muteBtn','msgToast'].forEach(id=>{const e=document.getElementById(id);if(e)e.remove();});}
  209. function setupPeer(){
  210. pc=new RTCPeerConnection(ICE);
  211. inputChannel=pc.createDataChannel('input',{ordered:true});
  212. pc.ondatachannel=(ev)=>{ if(ev.channel.label==='chat'){ chatChannel=ev.channel; chatChannel.onmessage=(e)=>{try{addChat(JSON.parse(e.data));}catch(_){}}; } };
  213. pc.ontrack=(ev)=>{
  214. if(ev.track.kind==='audio'){ let a=document.getElementById('remoteAudio'); if(!a){a=document.createElement('audio');a.id='remoteAudio';a.autoplay=true;document.body.appendChild(a);} a.srcObject=ev.streams[0]; return; }
  215. video.srcObject=ev.streams[0]; wrap.style.display='none'; topbar.style.display='none'; video.style.display='block'; video.focus(); buildBar();
  216. };
  217. pc.onicecandidate=(ev)=>{if(ev.candidate)ws.send(JSON.stringify({type:'ice-candidate',sessionId,candidate:ev.candidate}));};
  218. }
  219. const send=(o)=>{if(inputChannel&&inputChannel.readyState==='open')inputChannel.send(JSON.stringify(o));};
  220. const rel=(e)=>{const r=video.getBoundingClientRect();return{x:(e.clientX-r.left)/r.width,y:(e.clientY-r.top)/r.height};};
  221. let lm=0;
  222. video.addEventListener('mousemove',e=>{const t=performance.now();if(t-lm<30)return;lm=t;send({kind:'mousemove',...rel(e)});});
  223. video.addEventListener('mousedown',e=>{video.focus();send({kind:'mousedown',button:e.button,...rel(e)});});
  224. video.addEventListener('mouseup',e=>send({kind:'mouseup',button:e.button,...rel(e)}));
  225. video.addEventListener('dblclick',e=>send({kind:'dblclick',...rel(e)}));
  226. video.addEventListener('wheel',e=>{e.preventDefault();send({kind:'scroll',dx:e.deltaX,dy:e.deltaY});},{passive:false});
  227. video.addEventListener('contextmenu',e=>e.preventDefault());
  228. video.addEventListener('keydown',e=>{e.preventDefault();send({kind:'keydown',key:e.key,code:e.code});});
  229. video.addEventListener('keyup',e=>{e.preventDefault();send({kind:'keyup',key:e.key,code:e.code});});
  230. document.getElementById('endBtn').onclick=()=>{ws.send(JSON.stringify({type:'end-session',sessionId,reason:'agent-ended'}));};
  231. function esc(s){return String(s==null?'':s).replace(/[&<>"]/g,c=>({'&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;'}[c]));}
  232. </script>
  233. </body>
  234. </html>