211 lines
9.7 KiB
HTML
211 lines
9.7 KiB
HTML
<!doctype html>
|
||
<html lang="zh-Hant">
|
||
<head>
|
||
<meta charset="utf-8">
|
||
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover, user-scalable=no">
|
||
<meta name="theme-color" content="#101010">
|
||
<title>Bot 的電腦</title>
|
||
<link rel="icon" href="icon.svg">
|
||
<style>
|
||
:root {
|
||
--bg: #0d0d0d;
|
||
--bar: #161616;
|
||
--elev: #222222;
|
||
--hover: rgba(245, 245, 245, 0.08);
|
||
--line: rgba(245, 245, 245, 0.12);
|
||
--text: #f5f5f5;
|
||
--dim: rgba(245, 245, 245, 0.58);
|
||
--accent: #1884fc;
|
||
--warn: #ffc83d;
|
||
--ok: #10ab61;
|
||
--font: -apple-system, BlinkMacSystemFont, "SF Pro Text", "PingFang TC", "Noto Sans TC", "Segoe UI", system-ui, sans-serif;
|
||
}
|
||
* { box-sizing: border-box; }
|
||
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); font: 13.5px/1.4 var(--font); overflow: hidden; -webkit-font-smoothing: antialiased; }
|
||
body { display: flex; flex-direction: column; height: 100dvh; }
|
||
header {
|
||
display: flex; align-items: center; gap: 8px;
|
||
padding: 8px 12px; padding-top: max(8px, env(safe-area-inset-top));
|
||
min-height: 48px; background: var(--bar); border-bottom: 1px solid var(--line); flex: none;
|
||
}
|
||
body.embed header { min-height: 44px; padding: 6px 10px; }
|
||
header .name { font-weight: 600; font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
|
||
header .state { color: var(--dim); font-size: 12px; white-space: nowrap; display: flex; align-items: center; gap: 6px; }
|
||
.dot { width: 7px; height: 7px; border-radius: 50%; background: var(--dim); flex: none; }
|
||
.dot.live { background: var(--ok); box-shadow: 0 0 0 3px rgba(16, 171, 97, 0.18); }
|
||
.dot.hold { background: var(--warn); box-shadow: 0 0 0 3px rgba(255, 200, 61, 0.2); }
|
||
.grow { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 1px; }
|
||
button {
|
||
font: inherit; font-size: 13px; font-weight: 500; color: var(--text);
|
||
background: var(--elev); border: 1px solid var(--line); border-radius: 999px;
|
||
height: 30px; padding: 0 12px; cursor: pointer; white-space: nowrap;
|
||
display: inline-flex; align-items: center; justify-content: center; gap: 5px;
|
||
transition: background 0.12s, border-color 0.12s;
|
||
}
|
||
button:hover { background: var(--hover); }
|
||
button.main { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }
|
||
button.main:hover { filter: brightness(1.08); }
|
||
button.hold-on { background: rgba(255, 200, 61, 0.18); border-color: var(--warn); color: var(--warn); font-weight: 600; }
|
||
button.icon { width: 30px; padding: 0; border-radius: 9px; background: transparent; border-color: transparent; color: var(--dim); }
|
||
button.icon:hover { background: var(--hover); color: var(--text); }
|
||
#screen { flex: 1; min-height: 0; position: relative; background: #000; }
|
||
#screen > div { background: #000 !important; }
|
||
#msg {
|
||
position: absolute; inset: 0; display: flex; align-items: center; justify-content: center;
|
||
text-align: center; padding: 24px; color: var(--dim); font-size: 13.5px; line-height: 1.6; pointer-events: none;
|
||
}
|
||
#type {
|
||
display: none; gap: 6px; padding: 8px 10px;
|
||
padding-bottom: max(8px, env(safe-area-inset-bottom));
|
||
background: var(--bar); border-top: 1px solid var(--line); flex-wrap: wrap; align-items: center;
|
||
}
|
||
#type.on { display: flex; }
|
||
#type input {
|
||
flex: 1 1 180px; min-width: 0; height: 32px; font: inherit; font-size: 14px; color: var(--text);
|
||
background: #0e0e0e; border: 1px solid var(--line); border-radius: 10px; padding: 0 10px; outline: none;
|
||
}
|
||
#type input:focus { border-color: var(--accent); }
|
||
.key-group { display: flex; gap: 4px; flex-wrap: wrap; }
|
||
.key-group button { height: 28px; padding: 0 9px; border-radius: 8px; font-size: 12px; }
|
||
</style>
|
||
</head>
|
||
<body>
|
||
<header>
|
||
<button class="icon" id="back" title="關閉">✕</button>
|
||
<div class="grow">
|
||
<div class="name" id="name">Bot 的電腦</div>
|
||
<div class="state"><span class="dot" id="dot"></span><span id="state">連線中…</span></div>
|
||
</div>
|
||
<button id="kb" title="輸入文字(支援中文與貼上)">打字</button>
|
||
<button class="main" id="take">接管</button>
|
||
<button class="icon" id="popout" title="在新分頁打開" style="display:none">↗</button>
|
||
</header>
|
||
<div id="screen"><div id="msg">正在連接畫面…</div></div>
|
||
<div id="type">
|
||
<input id="text" placeholder="輸入文字(支援中文輸入法),按 Enter 送出到電腦" autocomplete="off" enterkeyhint="send">
|
||
<button class="main" id="send">送出</button>
|
||
<div class="key-group">
|
||
<button data-k="Return">Enter</button>
|
||
<button data-k="Escape">Esc</button>
|
||
<button data-k="Tab">Tab</button>
|
||
<button data-k="BackSpace">⌫</button>
|
||
<button data-k="Up">↑</button>
|
||
<button data-k="Down">↓</button>
|
||
</div>
|
||
</div>
|
||
<script type="module">
|
||
import RFB from "./novnc.js";
|
||
|
||
const q = new URLSearchParams(location.hash.slice(1));
|
||
const sq = new URLSearchParams(location.search);
|
||
const embed = q.get("embed") === "1" || sq.get("embed") === "1" || window.parent !== window;
|
||
if (embed) document.body.classList.add("embed");
|
||
|
||
const name = q.get("name") || "Bot 的電腦";
|
||
const token = localStorage.getItem("anybot.token") || "";
|
||
const wsBase = (location.protocol === "https:" ? "wss://" : "ws://") + location.host;
|
||
// The lasting login never rides in the connection's address (addresses end up in logs): a
|
||
// ticket good for this one screen, for a few minutes, is asked for first. (An older server
|
||
// that has none still gets the login, as before.)
|
||
async function ticketFor(path, body) {
|
||
try {
|
||
const r = await fetch(path, {method: "POST", headers: {"Authorization": "Bearer " + token, "Content-Type": "application/json"}, body: JSON.stringify(body)});
|
||
return r.ok ? ((await r.json()).ticket || "") : "";
|
||
} catch (_) { return ""; }
|
||
}
|
||
const lan = q.get("lan") === "1";
|
||
const given = q.get("ticket") || await ticketFor(lan ? "api/vnc/ticket" : "relay/screen-ticket", lan ? {} : {host: q.get("host") || ""});
|
||
const auth = given ? "ticket=" + encodeURIComponent(given) : "t=" + encodeURIComponent(token);
|
||
// 每個 Bot 一個螢幕: the bot whose screen this is (the AnyBot shows its own screen, or the shared one).
|
||
const botq = /^\d+$/.test(q.get("bot") || "") ? "&bot=" + q.get("bot") : "";
|
||
const url = lan ? `${wsBase}/api/vnc?${auth}${botq}` : `${wsBase}/relay/vnc/${encodeURIComponent(q.get("host") || "")}?${auth}${botq}`;
|
||
|
||
const $ = (id) => document.getElementById(id);
|
||
document.title = name;
|
||
$("name").textContent = name;
|
||
if (!embed) $("back").textContent = "←";
|
||
if (embed) {
|
||
$("popout").style.display = "";
|
||
$("popout").onclick = () => {
|
||
const clean = new URLSearchParams(q);
|
||
clean.delete("embed");
|
||
window.open("screen#" + clean.toString(), "_blank", "noopener");
|
||
};
|
||
}
|
||
|
||
let rfb = null, ws = null, control = q.get("control") === "1", tries = 0, connected = false, closed = false;
|
||
const KEYS = { Return: 0xff0d, Escape: 0xff1b, Tab: 0xff09, BackSpace: 0xff08, Up: 0xff52, Down: 0xff54 };
|
||
|
||
function state(text, kind) { $("state").textContent = text; $("dot").className = "dot" + (kind ? " " + kind : ""); }
|
||
function message(text) { $("msg").textContent = text; $("msg").style.display = text ? "" : "none"; }
|
||
function showControl() {
|
||
$("take").textContent = control ? "交還給 Bot" : "接管";
|
||
$("take").className = control ? "hold-on" : "main";
|
||
$("kb").style.display = control ? "" : "none";
|
||
if (!control) $("type").classList.remove("on");
|
||
if (rfb) { rfb.viewOnly = !control; if (control) rfb.focus(); }
|
||
if (connected) state(control ? "你在操作(Bot 會等你完成)" : "即時觀看", control ? "hold" : "live");
|
||
if (embed && window.parent !== window) {
|
||
try { window.parent.postMessage({ anybotScreen: "control", control }, location.origin); } catch (_) {}
|
||
}
|
||
}
|
||
|
||
function connect() {
|
||
if (closed) return;
|
||
message("正在連接畫面…"); state("連線中…");
|
||
ws = new WebSocket(url);
|
||
rfb = new RFB($("screen"), ws, { shared: true });
|
||
rfb.scaleViewport = true;
|
||
rfb.resizeSession = false;
|
||
rfb.qualityLevel = 6;
|
||
rfb.compressionLevel = 2;
|
||
rfb.showDotCursor = true;
|
||
rfb.viewOnly = !control;
|
||
rfb.addEventListener("connect", () => { tries = 0; connected = true; message(""); showControl(); });
|
||
rfb.addEventListener("disconnect", () => {
|
||
rfb = null; connected = false;
|
||
if (closed) return;
|
||
tries += 1;
|
||
state("連線中斷,重新連線中…");
|
||
message(tries > 3 ? "暫時連不上這台電腦(可能正在休眠或尚未啟動)。你可以在旁邊請它喚醒,或稍後自動重試。" : "連線中斷,正在重新連線…");
|
||
setTimeout(connect, Math.min(1000 * tries, 6000));
|
||
});
|
||
}
|
||
|
||
$("take").onclick = () => { control = !control; showControl(); };
|
||
$("kb").onclick = () => { $("type").classList.toggle("on"); if ($("type").classList.contains("on")) $("text").focus(); };
|
||
function send() {
|
||
const t = $("text").value;
|
||
if (!t || !ws || ws.readyState !== 1) return;
|
||
ws.send(t);
|
||
$("text").value = "";
|
||
}
|
||
$("send").onclick = send;
|
||
$("text").addEventListener("keydown", (e) => { if (e.key === "Enter" && !e.isComposing) { e.preventDefault(); send(); } });
|
||
document.querySelectorAll("[data-k]").forEach((b) => b.onclick = () => { if (rfb) rfb.sendKey(KEYS[b.dataset.k], b.dataset.k); });
|
||
$("back").onclick = () => {
|
||
closed = true;
|
||
if (rfb) { try { rfb.disconnect(); } catch (_) {} }
|
||
if (embed && window.parent !== window) {
|
||
window.parent.postMessage({ anybotScreen: "close" }, location.origin);
|
||
} else if (history.length > 1) {
|
||
history.back();
|
||
} else {
|
||
location.href = "./";
|
||
}
|
||
};
|
||
|
||
window.addEventListener("message", (e) => {
|
||
if (e.origin !== location.origin || !e.data) return;
|
||
if (e.data.anybotSetControl !== undefined) {
|
||
control = !!e.data.anybotSetControl;
|
||
showControl();
|
||
}
|
||
});
|
||
|
||
showControl();
|
||
connect();
|
||
</script>
|
||
</body>
|
||
</html>
|