agent: let the screen page keep reload, the address bar and its own buttons
The page forwarded every key to the remote desktop and cancelled it, so F5 and Ctrl+R could not reload, though the disconnect message tells the operator to reload, and Enter or Space on the fit and debug buttons did nothing. F5, F12, Ctrl/Cmd+R, Ctrl/Cmd+L and any key pressed on a button now go to the browser; the rest still go to the remote desktop. The filter is a small module with a test, bundled for the page as static/screen-keys.js, and the agent-page doc describes it.
This commit is contained in:
parent
5de0ff59f4
commit
ff7215a4b1
5 changed files with 80 additions and 0 deletions
|
|
@ -3,6 +3,7 @@
|
|||
// dist/screen.html served at GET /screen
|
||||
// dist/static/agent.css served at /static/agent.css (@import
|
||||
// resolved from @hive/shared)
|
||||
// dist/static/screen-keys.js the key filter `screen.html` loads
|
||||
//
|
||||
// The in-container Rust binary mounts `dist/` (with per-agent
|
||||
// `services.hyperhive.agent.frontend.extraFiles` layered on top) as a
|
||||
|
|
@ -37,6 +38,17 @@ for (const entry of ["colors.css", "theme.css", "agent.css"]) {
|
|||
});
|
||||
}
|
||||
|
||||
// The key filter the screen page loads ahead of its own script, as the global
|
||||
// `hiveScreenKeys` (the page's script is classic, not a module).
|
||||
await build({
|
||||
entryPoints: [src("screen-keys.js")],
|
||||
outfile: staticDir("screen-keys.js"),
|
||||
bundle: true,
|
||||
format: "iife",
|
||||
globalName: "hiveScreenKeys",
|
||||
logLevel: "info",
|
||||
});
|
||||
|
||||
copyFileSync(src("screen.html"), dist("screen.html"));
|
||||
|
||||
console.log("agent build ok →", dist(""));
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@
|
|||
"type": "module",
|
||||
"scripts": {
|
||||
"build": "node ./build.mjs",
|
||||
"test": "node --test test/*.test.mjs",
|
||||
"typecheck": "tsc --noEmit"
|
||||
},
|
||||
"dependencies": {
|
||||
|
|
|
|||
17
frontend/packages/agent/src/screen-keys.js
Normal file
17
frontend/packages/agent/src/screen-keys.js
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
// Which key events the screen page leaves to the browser instead of sending to
|
||||
// the remote desktop. Kept apart from `screen.html` so a test can load it;
|
||||
// `build.mjs` bundles it as `static/screen-keys.js`, which the page loads ahead
|
||||
// of its own script as `hiveScreenKeys`.
|
||||
|
||||
/**
|
||||
* Keys that belong to the browser or to the page's own buttons: reload (F5,
|
||||
* Ctrl/Cmd+R), the address bar (Ctrl/Cmd+L), the dev tools (F12), and any key
|
||||
* pressed while a button has focus (so Enter and Space can click it).
|
||||
*
|
||||
* @param {{ key: string, ctrlKey?: boolean, metaKey?: boolean, target?: any }} ev
|
||||
*/
|
||||
export function keepForBrowser(ev) {
|
||||
if (ev.target?.closest?.("button")) return true;
|
||||
if (ev.key === "F5" || ev.key === "F12") return true;
|
||||
return Boolean(ev.ctrlKey || ev.metaKey) && /^[rl]$/i.test(ev.key);
|
||||
}
|
||||
|
|
@ -32,6 +32,7 @@
|
|||
<div id="msg"></div>
|
||||
<div id="debug-log"></div>
|
||||
|
||||
<script src="static/screen-keys.js"></script>
|
||||
<script>
|
||||
// Minimal RFB-over-WebSocket renderer. Connects to `screen/ws` on
|
||||
// the same host; the harness relays raw RFB bytes to the VNC server
|
||||
|
|
@ -942,6 +943,9 @@
|
|||
document.addEventListener("keyup", (ev) => sendKey(ev, 0));
|
||||
|
||||
function sendKey(ev, down) {
|
||||
// Reload, the address bar, the dev tools and the page's own buttons
|
||||
// keep their keys (src/screen-keys.js).
|
||||
if (hiveScreenKeys.keepForBrowser(ev)) return;
|
||||
ev.preventDefault();
|
||||
const key = rfbKeysym(ev);
|
||||
const b = new Uint8Array(8);
|
||||
|
|
|
|||
40
frontend/packages/agent/test/screen-keys.test.mjs
Normal file
40
frontend/packages/agent/test/screen-keys.test.mjs
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
// Which keys the screen page leaves to the browser. Run by `npm test`.
|
||||
import assert from "node:assert/strict";
|
||||
import { test } from "node:test";
|
||||
import { keepForBrowser } from "../src/screen-keys.js";
|
||||
|
||||
const plain = { closest: () => null };
|
||||
const button = { closest: (sel) => (sel === "button" ? {} : null) };
|
||||
|
||||
test("reload, the address bar and the dev tools stay with the browser", () => {
|
||||
assert.equal(keepForBrowser({ key: "F5", target: plain }), true);
|
||||
assert.equal(keepForBrowser({ key: "F12", target: plain }), true);
|
||||
assert.equal(
|
||||
keepForBrowser({ key: "r", ctrlKey: true, target: plain }),
|
||||
true,
|
||||
);
|
||||
assert.equal(
|
||||
keepForBrowser({ key: "R", metaKey: true, target: plain }),
|
||||
true,
|
||||
);
|
||||
assert.equal(
|
||||
keepForBrowser({ key: "l", ctrlKey: true, target: plain }),
|
||||
true,
|
||||
);
|
||||
});
|
||||
|
||||
test("a key pressed on a button stays with the button", () => {
|
||||
assert.equal(keepForBrowser({ key: "Enter", target: button }), true);
|
||||
assert.equal(keepForBrowser({ key: " ", target: button }), true);
|
||||
});
|
||||
|
||||
test("everything else goes to the remote desktop", () => {
|
||||
assert.equal(keepForBrowser({ key: "a", target: plain }), false);
|
||||
assert.equal(keepForBrowser({ key: "r", target: plain }), false);
|
||||
assert.equal(
|
||||
keepForBrowser({ key: "c", ctrlKey: true, target: plain }),
|
||||
false,
|
||||
);
|
||||
assert.equal(keepForBrowser({ key: "F4", target: plain }), false);
|
||||
assert.equal(keepForBrowser({ key: "Enter" }), false);
|
||||
});
|
||||
Loading…
Reference in a new issue