longtable 0.1.53 → 0.1.55
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/bridge.mjs +26 -0
- package/src/claude.mjs +3 -0
- package/src/client-rules.mjs +1 -1
- package/src/context.mjs +208 -0
- package/src/copy-guard.mjs +71 -0
- package/src/extras.mjs +7 -5
- package/src/local.mjs +32 -9
- package/src/pointed.mjs +11 -12
- package/src/prompts.mjs +365 -0
- package/src/repo.mjs +1 -1
- package/src/run.mjs +69 -694
- package/src/signin.mjs +3 -0
- package/src/store.mjs +5 -0
- package/src/typecheck.mjs +3 -3
- package/src/browser.mjs +0 -340
package/src/signin.mjs
CHANGED
|
@@ -8,6 +8,9 @@
|
|
|
8
8
|
import http from "node:http";
|
|
9
9
|
import { randomBytes } from "node:crypto";
|
|
10
10
|
import { spawn } from "node:child_process";
|
|
11
|
+
// The package's only two dependencies, used only here, at connect (bin/longtable.mjs loads this file then): they come
|
|
12
|
+
// with the npx run that connects. The copy that keeps itself up to date (update.mjs) carries no node_modules, so
|
|
13
|
+
// nothing that runs after connecting may import a package; the terminal fetches its own (pty.mjs).
|
|
11
14
|
import pty from "@lydell/node-pty";
|
|
12
15
|
import headless from "@xterm/headless";
|
|
13
16
|
import { childEnv, tokenWorks } from "./claude.mjs";
|
package/src/store.mjs
CHANGED
|
@@ -127,3 +127,8 @@ if (existsSync(oldClaudeToken) && !existsSync(defaultClaudeToken)) {
|
|
|
127
127
|
mkdirSync(join(defaultHome, "secrets"), { recursive: true });
|
|
128
128
|
renameSync(oldClaudeToken, defaultClaudeToken);
|
|
129
129
|
}
|
|
130
|
+
|
|
131
|
+
// A tripwire: something that should never happen, written to this computer's log in one fixed sentence (never a
|
|
132
|
+
// path, a name or anything from a file) and nothing more. It never stops or changes the job. sweep-notes/rules.md
|
|
133
|
+
// lists each one.
|
|
134
|
+
export function trip(what) { try { log(`tripwire: ${what}`); } catch {} }
|
package/src/typecheck.mjs
CHANGED
|
@@ -9,10 +9,9 @@
|
|
|
9
9
|
import { execFile, execFileSync } from "node:child_process";
|
|
10
10
|
import { copyFileSync, existsSync, lstatSync, mkdirSync, readFileSync, readdirSync, realpathSync, rmSync, rmdirSync, symlinkSync, unlinkSync, writeFileSync } from "node:fs";
|
|
11
11
|
import { dirname, join } from "node:path";
|
|
12
|
+
import { isSecretName } from "./pointed.mjs";
|
|
12
13
|
|
|
13
14
|
const win = process.platform === "win32";
|
|
14
|
-
// Never linked or copied into a working folder, whatever git ignores.
|
|
15
|
-
const SECRET = /(^|\/)(\.env[^/]*|.*\.(pem|key|p12|pfx)|id_[a-z0-9]+|\.npmrc|\.netrc|credentials[^/]*)(\/|$)/i;
|
|
16
15
|
|
|
17
16
|
export function tscOf(folder) {
|
|
18
17
|
const tsc = folder ? join(folder, "node_modules", "typescript", "bin", "tsc") : null;
|
|
@@ -34,7 +33,8 @@ export function mirrorIgnored(dir, folder) {
|
|
|
34
33
|
for (const raw of list) {
|
|
35
34
|
const isDir = raw.endsWith("/");
|
|
36
35
|
const p = raw.replace(/\/$/, "");
|
|
37
|
-
|
|
36
|
+
// Never a key or sign-in file, whatever git ignores.
|
|
37
|
+
if (!p || p.startsWith("..") || p.split("/").some(isSecretName) || p.split("/").includes(".git")) continue;
|
|
38
38
|
const at = join(dir, ...p.split("/"));
|
|
39
39
|
if (existsSync(at) || !existsSync(dirname(at))) continue;
|
|
40
40
|
try {
|
package/src/browser.mjs
DELETED
|
@@ -1,340 +0,0 @@
|
|
|
1
|
-
import { spawn, spawnSync } from "node:child_process";
|
|
2
|
-
import { existsSync, mkdirSync, readFileSync, rmSync } from "node:fs";
|
|
3
|
-
import { join } from "node:path";
|
|
4
|
-
|
|
5
|
-
// The person's browser at the table, like the one in Claude's own app: one Chrome (or Edge, Brave, Chromium) the
|
|
6
|
-
// connection runs with no window, its profile kept in ~/.longtable/browser so sign-ins stay. Their own page watches it
|
|
7
|
-
// live and passes their clicks and keys in (local.mjs); their AI drives the same one when "Let it open pages" is on, so
|
|
8
|
-
// they can watch it and take over. Frames go only to their own page on this computer, never to Longtable.
|
|
9
|
-
// It is spoken to through Chrome's DevTools pipe (no WebSocket needed on Node 20), and also listens on a DevTools port
|
|
10
|
-
// on 127.0.0.1 only, which is how their AI's browser tools attach to it.
|
|
11
|
-
|
|
12
|
-
export function findBrowser(platform = process.platform, env = process.env, exists = existsSync) {
|
|
13
|
-
const win = (base, ...rest) => (base ? join(base, ...rest) : null);
|
|
14
|
-
const list = platform === "win32" ? [
|
|
15
|
-
win(env.PROGRAMFILES, "Google", "Chrome", "Application", "chrome.exe"),
|
|
16
|
-
win(env["PROGRAMFILES(X86)"], "Google", "Chrome", "Application", "chrome.exe"),
|
|
17
|
-
win(env.LOCALAPPDATA, "Google", "Chrome", "Application", "chrome.exe"),
|
|
18
|
-
win(env.PROGRAMFILES, "BraveSoftware", "Brave-Browser", "Application", "brave.exe"),
|
|
19
|
-
win(env.LOCALAPPDATA, "BraveSoftware", "Brave-Browser", "Application", "brave.exe"),
|
|
20
|
-
win(env["PROGRAMFILES(X86)"], "Microsoft", "Edge", "Application", "msedge.exe"),
|
|
21
|
-
win(env.PROGRAMFILES, "Microsoft", "Edge", "Application", "msedge.exe"),
|
|
22
|
-
] : platform === "darwin" ? [
|
|
23
|
-
"/Applications/Google Chrome.app/Contents/MacOS/Google Chrome",
|
|
24
|
-
"/Applications/Brave Browser.app/Contents/MacOS/Brave Browser",
|
|
25
|
-
"/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge",
|
|
26
|
-
"/Applications/Chromium.app/Contents/MacOS/Chromium",
|
|
27
|
-
] : ["google-chrome", "google-chrome-stable", "chromium", "chromium-browser", "brave-browser", "microsoft-edge"].map((n) => {
|
|
28
|
-
const r = spawnSync("sh", ["-c", `command -v ${n}`], { encoding: "utf8" });
|
|
29
|
-
return r.status === 0 ? r.stdout.trim() : null;
|
|
30
|
-
});
|
|
31
|
-
return list.find((p) => p && exists(p)) || null;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
// Only addresses a browser should be sent to from a typed bar: the web, and pages on this computer.
|
|
35
|
-
export function addressFor(typed) {
|
|
36
|
-
const t = String(typed ?? "").trim();
|
|
37
|
-
if (!t) return null;
|
|
38
|
-
if (/^about:blank$/i.test(t)) return "about:blank";
|
|
39
|
-
const local = /^(localhost|127\.0\.0\.1|\[::1\])(:\d+)?(\/|$)/i.test(t);
|
|
40
|
-
let url;
|
|
41
|
-
try { url = new URL(/^[a-z][a-z0-9+.-]*:\/\//i.test(t) ? t : `${local ? "http" : "https"}://${t}`); } catch { return null; }
|
|
42
|
-
return url.protocol === "http:" || url.protocol === "https:" ? url.href : null;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
// Keys that are not text: their DevTools name, key code and the text they make.
|
|
46
|
-
const KEYS = { Enter: [13, "\r"], Backspace: [8], Tab: [9], Escape: [27], Delete: [46], ArrowLeft: [37], ArrowUp: [38], ArrowRight: [39], ArrowDown: [40], Home: [36], End: [35], PageUp: [33], PageDown: [34] };
|
|
47
|
-
const EDITS = { a: "selectAll", c: "copy", x: "cut", v: "paste", z: "undo", y: "redo" };
|
|
48
|
-
const IDLE_MS = 30 * 60 * 1000;
|
|
49
|
-
|
|
50
|
-
export class Browser {
|
|
51
|
-
constructor(home, log) {
|
|
52
|
-
this.profile = join(home, "browser");
|
|
53
|
-
this.log = log;
|
|
54
|
-
this.viewers = new Set();
|
|
55
|
-
this.users = 0;
|
|
56
|
-
this.page = null; // the tab shown: { targetId, sessionId, url, title }
|
|
57
|
-
this.tabs = new Map(); // every tab, by id: { id, url, title }; as many as they or their AI open
|
|
58
|
-
this.sessions = new Map(); // tab id -> the session it is spoken to on
|
|
59
|
-
this.size = { w: 1280, h: 800 };
|
|
60
|
-
this.fit = null; // the size of the picture on their screen, while someone watches
|
|
61
|
-
this.starting = null;
|
|
62
|
-
this.proc = null;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
// Started on first use; a browser nobody watched or used for half an hour is closed.
|
|
66
|
-
start() {
|
|
67
|
-
if (this.ready) return Promise.resolve(this.port);
|
|
68
|
-
this.starting ??= this.launch().finally(() => { this.starting = null; });
|
|
69
|
-
return this.starting;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
async launch() {
|
|
73
|
-
const exe = findBrowser();
|
|
74
|
-
if (!exe) throw new Error("No Chrome, Edge or Brave was found on this computer");
|
|
75
|
-
mkdirSync(this.profile, { recursive: true });
|
|
76
|
-
const portFile = join(this.profile, "DevToolsActivePort");
|
|
77
|
-
rmSync(portFile, { force: true });
|
|
78
|
-
const proc = spawn(exe, ["--headless=new", "--remote-debugging-pipe", "--remote-debugging-port=0", "--remote-debugging-address=127.0.0.1",
|
|
79
|
-
`--user-data-dir=${this.profile}`, "--no-first-run", "--no-default-browser-check", `--window-size=${this.size.w},${this.size.h}`, "about:blank"],
|
|
80
|
-
{ stdio: ["ignore", "ignore", "ignore", "pipe", "pipe"], windowsHide: true });
|
|
81
|
-
this.proc = proc;
|
|
82
|
-
this.calls = new Map();
|
|
83
|
-
this.next = 1;
|
|
84
|
-
let buf = "";
|
|
85
|
-
proc.stdio[4].setEncoding("utf8");
|
|
86
|
-
proc.stdio[4].on("data", (d) => {
|
|
87
|
-
buf += d;
|
|
88
|
-
let at;
|
|
89
|
-
while ((at = buf.indexOf("\0")) >= 0) {
|
|
90
|
-
const msg = buf.slice(0, at);
|
|
91
|
-
buf = buf.slice(at + 1);
|
|
92
|
-
try { this.got(JSON.parse(msg)); } catch (e) { this.log(`browser: a message could not be read: ${e.message}`); }
|
|
93
|
-
}
|
|
94
|
-
});
|
|
95
|
-
proc.stdio[3].on("error", () => {});
|
|
96
|
-
proc.on("exit", () => {
|
|
97
|
-
if (this.proc !== proc) return;
|
|
98
|
-
clearInterval(this.idle);
|
|
99
|
-
this.proc = null;
|
|
100
|
-
this.ready = false;
|
|
101
|
-
this.port = 0;
|
|
102
|
-
this.page = null;
|
|
103
|
-
this.tabs.clear();
|
|
104
|
-
this.sessions.clear();
|
|
105
|
-
for (const { fail } of this.calls.values()) fail(new Error("the browser closed"));
|
|
106
|
-
this.calls.clear();
|
|
107
|
-
this.tell({ closed: true });
|
|
108
|
-
});
|
|
109
|
-
try {
|
|
110
|
-
// Chrome writes the port it took once it listens.
|
|
111
|
-
for (let i = 0; i < 150 && !this.port; i++) {
|
|
112
|
-
await new Promise((r) => setTimeout(r, 100));
|
|
113
|
-
if (!this.proc) throw new Error("the browser did not start (is its profile open in another window?)");
|
|
114
|
-
try { this.port = Number(readFileSync(portFile, "utf8").split("\n")[0]) || 0; } catch {}
|
|
115
|
-
}
|
|
116
|
-
if (!this.port) throw new Error("the browser did not say where it listens");
|
|
117
|
-
await this.call("Target.setDiscoverTargets", { discover: true });
|
|
118
|
-
const { targetInfos } = await this.call("Target.getTargets");
|
|
119
|
-
for (const t of targetInfos) this.note(t);
|
|
120
|
-
const page = targetInfos.find((t) => this.tabs.has(t.targetId));
|
|
121
|
-
await this.show(page ? page.targetId : (await this.call("Target.createTarget", { url: "about:blank" })).targetId);
|
|
122
|
-
} catch (e) {
|
|
123
|
-
this.close();
|
|
124
|
-
throw e;
|
|
125
|
-
}
|
|
126
|
-
this.ready = true;
|
|
127
|
-
this.idle = setInterval(() => {
|
|
128
|
-
if (!this.viewers.size && !this.users && Date.now() - this.used > IDLE_MS) this.close();
|
|
129
|
-
}, 60 * 1000);
|
|
130
|
-
this.idle.unref?.();
|
|
131
|
-
this.used = Date.now();
|
|
132
|
-
this.log("browser: started");
|
|
133
|
-
return this.port;
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
close() {
|
|
137
|
-
clearInterval(this.idle);
|
|
138
|
-
this.ready = false;
|
|
139
|
-
const p = this.proc;
|
|
140
|
-
this.proc = null;
|
|
141
|
-
this.port = 0;
|
|
142
|
-
this.page = null;
|
|
143
|
-
this.tabs.clear();
|
|
144
|
-
this.sessions.clear();
|
|
145
|
-
if (p) { try { p.kill(); } catch {} }
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
call(method, params = {}, sessionId) {
|
|
149
|
-
if (!this.proc) return Promise.reject(new Error("the browser is not running"));
|
|
150
|
-
const id = this.next++;
|
|
151
|
-
return new Promise((ok, fail) => {
|
|
152
|
-
this.calls.set(id, { ok, fail });
|
|
153
|
-
this.proc.stdio[3].write(`${JSON.stringify({ id, method, params, ...(sessionId ? { sessionId } : {}) })}\0`);
|
|
154
|
-
});
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
got(msg) {
|
|
158
|
-
if (msg.id) {
|
|
159
|
-
const c = this.calls.get(msg.id);
|
|
160
|
-
if (!c) return;
|
|
161
|
-
this.calls.delete(msg.id);
|
|
162
|
-
return msg.error ? c.fail(new Error(msg.error.message)) : c.ok(msg.result || {});
|
|
163
|
-
}
|
|
164
|
-
const p = msg.params || {};
|
|
165
|
-
if (msg.method === "Page.screencastFrame") {
|
|
166
|
-
this.call("Page.screencastFrameAck", { sessionId: p.sessionId }, msg.sessionId).catch(() => {});
|
|
167
|
-
if (msg.sessionId !== this.page?.sessionId) return;
|
|
168
|
-
this.size = { w: p.metadata.deviceWidth, h: p.metadata.deviceHeight };
|
|
169
|
-
return this.tell({ frame: p.data, tab: this.page.targetId });
|
|
170
|
-
}
|
|
171
|
-
// A new tab, theirs or their AI's, is the one shown, as it would be in front of them.
|
|
172
|
-
if (msg.method === "Target.targetCreated" && this.note(p.targetInfo)) {
|
|
173
|
-
this.tellTabs();
|
|
174
|
-
if (this.ready) this.show(p.targetInfo.targetId).catch((e) => this.log(`browser: could not show a new tab: ${e.message}`));
|
|
175
|
-
return;
|
|
176
|
-
}
|
|
177
|
-
if (msg.method === "Target.targetInfoChanged" && this.note(p.targetInfo)) {
|
|
178
|
-
this.tellTabs();
|
|
179
|
-
if (p.targetInfo.targetId === this.page?.targetId) {
|
|
180
|
-
this.page.url = p.targetInfo.url;
|
|
181
|
-
this.page.title = p.targetInfo.title;
|
|
182
|
-
this.tell({ url: this.page.url, title: this.page.title });
|
|
183
|
-
}
|
|
184
|
-
return;
|
|
185
|
-
}
|
|
186
|
-
if (msg.method === "Target.targetDestroyed" && this.tabs.delete(p.targetId)) {
|
|
187
|
-
this.sessions.delete(p.targetId);
|
|
188
|
-
this.tellTabs();
|
|
189
|
-
if (p.targetId !== this.page?.targetId) return;
|
|
190
|
-
this.page = null;
|
|
191
|
-
this.casting = null;
|
|
192
|
-
// The last tab closed: a blank one takes its place, as a browser window would keep one open.
|
|
193
|
-
const next = [...this.tabs.keys()].pop();
|
|
194
|
-
return (next ? this.show(next) : this.call("Target.createTarget", { url: "about:blank" }))
|
|
195
|
-
.catch((e) => this.log(`browser: no tab left to show: ${e.message}`));
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
// A page target is a tab (never the browser's own pages, such as DevTools). Kept with its address and title.
|
|
200
|
-
note(t) {
|
|
201
|
-
if (!t || t.type !== "page" || /^(devtools|chrome-extension):/.test(t.url || "")) return false;
|
|
202
|
-
this.tabs.set(t.targetId, { id: t.targetId, url: t.url || "", title: t.title || "" });
|
|
203
|
-
return true;
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
tellTabs() { this.tell({ tabs: [...this.tabs.values()], active: this.page?.targetId || null }); }
|
|
207
|
-
|
|
208
|
-
// One switch at a time, in the order asked: a tab picked just after others opened is the one left showing.
|
|
209
|
-
show(targetId) {
|
|
210
|
-
this.showing = (this.showing || Promise.resolve()).catch(() => {}).then(() => this.showNow(targetId));
|
|
211
|
-
return this.showing;
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
async showNow(targetId) {
|
|
215
|
-
if (this.page?.targetId === targetId || !this.proc) return;
|
|
216
|
-
// The tab shown before stops sending pictures.
|
|
217
|
-
if (this.page && this.casting) { this.call("Page.stopScreencast", {}, this.page.sessionId).catch(() => {}); this.casting = null; }
|
|
218
|
-
let sessionId = this.sessions.get(targetId);
|
|
219
|
-
if (!sessionId) {
|
|
220
|
-
({ sessionId } = await this.call("Target.attachToTarget", { targetId, flatten: true }));
|
|
221
|
-
this.sessions.set(targetId, sessionId);
|
|
222
|
-
await this.call("Page.enable", {}, sessionId);
|
|
223
|
-
}
|
|
224
|
-
const { targetInfo } = await this.call("Target.getTargetInfo", { targetId });
|
|
225
|
-
this.page = { targetId, sessionId, url: targetInfo?.url || "", title: targetInfo?.title || "" };
|
|
226
|
-
this.tell({ url: this.page.url, title: this.page.title });
|
|
227
|
-
this.tellTabs();
|
|
228
|
-
if (this.viewers.size) {
|
|
229
|
-
await this.fitTo();
|
|
230
|
-
await this.cast();
|
|
231
|
-
const { data } = await this.call("Page.captureScreenshot", { format: "jpeg", quality: 70 }, sessionId);
|
|
232
|
-
this.tell({ frame: data, tab: targetId });
|
|
233
|
-
}
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
// The page laid out at the size it is shown at, sharp on their screen, as in Claude's own browser. With nobody
|
|
237
|
-
// watching it goes back to the browser's own window size, which is what their AI then sees.
|
|
238
|
-
async fitTo() {
|
|
239
|
-
const s = this.page?.sessionId;
|
|
240
|
-
if (!s) return;
|
|
241
|
-
if (this.fit && this.viewers.size) await this.call("Emulation.setDeviceMetricsOverride", { width: this.fit.w, height: this.fit.h, deviceScaleFactor: this.fit.dpr, mobile: false }, s);
|
|
242
|
-
else await this.call("Emulation.clearDeviceMetricsOverride", {}, s);
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
// Frames from the tab shown, once (a second start is refused by the browser).
|
|
246
|
-
async cast() {
|
|
247
|
-
const s = this.page?.sessionId;
|
|
248
|
-
if (!s || this.casting === s) return;
|
|
249
|
-
this.casting = s;
|
|
250
|
-
try { await this.call("Page.startScreencast", { format: "jpeg", quality: 70, maxWidth: 4000, maxHeight: 4000 }, s); }
|
|
251
|
-
catch (e) { this.casting = null; throw e; }
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
tell(event) { for (const v of this.viewers) v(event); }
|
|
255
|
-
|
|
256
|
-
// Someone's page watching. It hears where the browser is at once, then every frame.
|
|
257
|
-
async watch(viewer) {
|
|
258
|
-
this.viewers.add(viewer);
|
|
259
|
-
this.used = Date.now();
|
|
260
|
-
await this.start();
|
|
261
|
-
viewer({ url: this.page?.url || "", title: this.page?.title || "" });
|
|
262
|
-
viewer({ tabs: [...this.tabs.values()], active: this.page?.targetId || null });
|
|
263
|
-
await this.fitTo();
|
|
264
|
-
await this.cast();
|
|
265
|
-
// A still page sends no new frame, so the one on screen is asked for.
|
|
266
|
-
const { data } = await this.call("Page.captureScreenshot", { format: "jpeg", quality: 70 }, this.page.sessionId);
|
|
267
|
-
viewer({ frame: data, tab: this.page.targetId });
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
unwatch(viewer) {
|
|
271
|
-
this.viewers.delete(viewer);
|
|
272
|
-
this.used = Date.now();
|
|
273
|
-
if (!this.viewers.size && this.page) {
|
|
274
|
-
this.casting = null;
|
|
275
|
-
this.fit = null;
|
|
276
|
-
this.call("Page.stopScreencast", {}, this.page.sessionId).catch(() => {});
|
|
277
|
-
this.fitTo().catch(() => {});
|
|
278
|
-
}
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
// Their AI's run uses it: it is started for that, and kept open until the run is done.
|
|
282
|
-
async hold() { this.users++; this.used = Date.now(); try { return await this.start(); } catch (e) { this.users--; throw e; } }
|
|
283
|
-
release() { this.users = Math.max(0, this.users - 1); this.used = Date.now(); }
|
|
284
|
-
|
|
285
|
-
// What their page asks of it. Points are given as fractions of the picture, so any size of screen lands right.
|
|
286
|
-
async act(a) {
|
|
287
|
-
this.used = Date.now();
|
|
288
|
-
// The size it is shown at, which may come before the browser has finished starting: kept, and used when it has.
|
|
289
|
-
if (a.do === "size") {
|
|
290
|
-
const n = (v) => Math.round(Math.max(200, Math.min(3000, Number(v) || 200)));
|
|
291
|
-
this.fit = { w: n(a.w), h: n(a.h), dpr: Math.max(1, Math.min(3, Number(a.dpr) || 1)) };
|
|
292
|
-
if (!this.page || !this.viewers.size) return null;
|
|
293
|
-
// Frames at the new size: the old ones are stopped and started again.
|
|
294
|
-
this.casting = null;
|
|
295
|
-
await this.call("Page.stopScreencast", {}, this.page.sessionId).catch(() => {});
|
|
296
|
-
await this.fitTo();
|
|
297
|
-
return this.cast();
|
|
298
|
-
}
|
|
299
|
-
if (!this.page) throw new Error("the browser is not open");
|
|
300
|
-
const s = this.page.sessionId;
|
|
301
|
-
const at = (n, size) => Math.max(0, Math.min(size, Number(n) * size));
|
|
302
|
-
switch (a.do) {
|
|
303
|
-
case "go": {
|
|
304
|
-
const url = addressFor(a.url);
|
|
305
|
-
if (!url) throw new Error("that is not a web address");
|
|
306
|
-
return this.call("Page.navigate", { url }, s);
|
|
307
|
-
}
|
|
308
|
-
case "back": case "forward": {
|
|
309
|
-
const { currentIndex, entries } = await this.call("Page.getNavigationHistory", {}, s);
|
|
310
|
-
const e = entries[currentIndex + (a.do === "back" ? -1 : 1)];
|
|
311
|
-
return e ? this.call("Page.navigateToHistoryEntry", { entryId: e.id }, s) : null;
|
|
312
|
-
}
|
|
313
|
-
case "reload": return this.call("Page.reload", {}, s);
|
|
314
|
-
// Tabs, with no limit: their page warns past a sensible number, nothing here refuses one.
|
|
315
|
-
case "tab": return this.tabs.has(a.id) ? this.show(a.id) : null;
|
|
316
|
-
case "newtab": return this.call("Target.createTarget", { url: addressFor(a.url) || "about:blank" });
|
|
317
|
-
case "closetab": return this.tabs.has(a.id) ? this.call("Target.closeTarget", { targetId: a.id }) : null;
|
|
318
|
-
case "click": {
|
|
319
|
-
const x = at(a.x, this.size.w), y = at(a.y, this.size.h);
|
|
320
|
-
await this.call("Input.dispatchMouseEvent", { type: "mouseMoved", x, y }, s);
|
|
321
|
-
await this.call("Input.dispatchMouseEvent", { type: "mousePressed", x, y, button: "left", clickCount: 1 }, s);
|
|
322
|
-
return this.call("Input.dispatchMouseEvent", { type: "mouseReleased", x, y, button: "left", clickCount: 1 }, s);
|
|
323
|
-
}
|
|
324
|
-
case "scroll":
|
|
325
|
-
return this.call("Input.dispatchMouseEvent", { type: "mouseWheel", x: at(a.x, this.size.w), y: at(a.y, this.size.h), deltaX: 0, deltaY: Math.max(-2000, Math.min(2000, Number(a.dy) || 0)) }, s);
|
|
326
|
-
case "type": return this.call("Input.insertText", { text: String(a.text ?? "").slice(0, 8000) }, s);
|
|
327
|
-
case "key": {
|
|
328
|
-
const mods = (a.ctrl ? 2 : 0) | (a.meta ? 4 : 0) | (a.shift ? 8 : 0) | (a.alt ? 1 : 0);
|
|
329
|
-
const k = String(a.key ?? "");
|
|
330
|
-
const edit = (a.ctrl || a.meta) && EDITS[k.toLowerCase()];
|
|
331
|
-
const named = KEYS[k];
|
|
332
|
-
if (!named && !edit) return null;
|
|
333
|
-
const base = { key: k, code: named ? k : `Key${k.toUpperCase()}`, windowsVirtualKeyCode: named ? named[0] : k.toUpperCase().charCodeAt(0), modifiers: mods };
|
|
334
|
-
await this.call("Input.dispatchKeyEvent", { type: "keyDown", ...base, ...(named?.[1] && !mods ? { text: named[1] } : {}), ...(edit ? { commands: [edit] } : {}) }, s);
|
|
335
|
-
return this.call("Input.dispatchKeyEvent", { type: "keyUp", ...base }, s);
|
|
336
|
-
}
|
|
337
|
-
default: throw new Error("unknown browser step");
|
|
338
|
-
}
|
|
339
|
-
}
|
|
340
|
-
}
|