The owner asked for a browser-use MCP of magpie's own, so an agent's browser keeps working when Claude Code switches accounts, and asked that it act in the browser the user uses every day: a fresh Playwright profile is signed in to nothing. Magpie Browser is an MV3 extension (internal/browserext, ID dmlhojidlfnkgamkekjhacgkpanbkeko, pinned by its key) that the user loads once in Chrome, Edge, Arc, Brave or another Chromium browser. It holds a WebSocket to the gateway's /v1/magpie/browser/extension and carries out each tool call with chrome.debugger (the DevTools protocol) in the user's own tabs. The socket is taken only from this computer, with a loopback Host (no DNS rebinding) and the extension's own Origin; a page or another extension that connected in its place would be handed every call. Agents reach it at /mcp/magpie/browser (Streamable HTTP) or through `magpie mcp browser` (stdio), and the Library's MCP market lists it as Magpie Browser, opt-in. Like the image server it is magpie's own, but it is never served to another computer, shared and with a key or not, and never to a web page, since it acts as the user wherever they are signed in. 17 tools: navigate (url, back, forward, reload), snapshot (an accessibility tree with refs, interactive-only on ask), click, type, press, select, hover, drag, upload, scroll, wait (text, gone, seconds), dialog, evaluate, text, screenshot (page, full page, element; JPEG or PNG), console and tabs (list, new, select, close). Each agent session goes on in its own tab; a link that opens a tab is followed, and a dialog a click opens is said in the reply. The Library's sheet for it has Set up the extension…, which writes the extension to ~/.config/magpie/browser-extension pointed at this gateway and shows the folder, the three steps to load it, and whether the browser is connected, polled while the sheet is open. Run: - TestBrowserToolsInARealBrowser: the extension magpie writes, loaded in a real Chrome for Testing 141 (Playwright's chromium-1234, headless=new) with --load-extension, connects, and all 17 tools act in a real tab of a fixture site. Green on mote (macOS 27), on xiaomi-gpu (Windows 11, win-run) and in magpie-desktop (Ubuntu 24.04, wsl-run). Under a test HOME Chrome needs --use-mock-keychain / --password-store=basic, or the extension's storage waits on a keychain and it never connects. - go test ./internal/gateway -run 'Browser|ReadOnly|Image', ./internal/ browserext, ./internal/browsermcp, ./internal/library, ./internal/gui -run 'Library|Browser', ./internal/cli on mote. - GUI: library-browser-setup.test.cjs (new) in Chromium and WebKit, en, zh, zh-TW, ja, de, at 1100 and 420 wide: 22/22. With i18n-keys and every library*.test.cjs: 915 pass, 1 fail, i18n-keys' "Unmarked", red on main since5787d758a(#4524, fixed in the next commit). - gofmt -l, go vet ./..., GOOS=linux go vet -tags nogui ./..., GOOS=windows go vet ./... on mote. - Again at this commit, rebased on2e9df9b1b: gofmt -l, the vet trio, ./internal/gui -run 'Budget|Size', ./internal/proc (exec guard), ./internal/usage, ./internal/browserext, ./internal/browsermcp, and i18n-keys (0 fail with the next commit). The full go test ./... runs in magpie-desktop (Linux) as this is pushed; mote had another session's suite running. Broken by hand, each red, then restored: - browser_click's dispatch in background.js turned into a no-op: the E2E test is red at "clicked Ada". - the extension socket's Origin check, its loopback-Host check and local(): TestBrowserExtensionOnlyFromItsOrigin red for each. - browserMCP's local()/fromBrowser() guard: TestBrowserMCPGuarded red. - a tool dropped from background.js: TestBrowserToolsAreTheExtensions red. - the sheet's steps left shown once connected: the GUI test is red. Not tried: loading it by hand (Load unpacked) in the owner's own daily Chrome or Edge and driving a site they are signed in to; Arc, Brave and Edge themselves (only Chrome for Testing); Firefox and Safari, which have no chrome.debugger and are not supported. Chrome shows its "is debugging this browser" bar while a tab is being driven; that is Chrome's and cannot be hidden.
112 lines
5.1 KiB
JavaScript
112 lines
5.1 KiB
JavaScript
// Keys as Input.dispatchKeyEvent wants them. A press is a name ("Enter",
|
|
// "a", "ArrowDown") or a chord ("Control+a", "Meta+Shift+K").
|
|
|
|
import { send, sleep } from "./cdp.js";
|
|
|
|
const KEYS = {
|
|
Enter: { code: "Enter", keyCode: 13, text: "\r" },
|
|
Tab: { code: "Tab", keyCode: 9 },
|
|
Backspace: { code: "Backspace", keyCode: 8 },
|
|
Delete: { code: "Delete", keyCode: 46 },
|
|
Escape: { code: "Escape", keyCode: 27 },
|
|
Space: { key: " ", code: "Space", keyCode: 32, text: " " },
|
|
" ": { code: "Space", keyCode: 32, text: " " },
|
|
ArrowUp: { code: "ArrowUp", keyCode: 38 },
|
|
ArrowDown: { code: "ArrowDown", keyCode: 40 },
|
|
ArrowLeft: { code: "ArrowLeft", keyCode: 37 },
|
|
ArrowRight: { code: "ArrowRight", keyCode: 39 },
|
|
Home: { code: "Home", keyCode: 36 },
|
|
End: { code: "End", keyCode: 35 },
|
|
PageUp: { code: "PageUp", keyCode: 33 },
|
|
PageDown: { code: "PageDown", keyCode: 34 },
|
|
Insert: { code: "Insert", keyCode: 45 },
|
|
Shift: { code: "ShiftLeft", keyCode: 16 },
|
|
Control: { code: "ControlLeft", keyCode: 17 },
|
|
Alt: { code: "AltLeft", keyCode: 18 },
|
|
Meta: { code: "MetaLeft", keyCode: 91 },
|
|
};
|
|
for (let i = 1; i <= 12; i++) KEYS["F" + i] = { code: "F" + i, keyCode: 111 + i };
|
|
|
|
const ALIASES = {
|
|
return: "Enter", esc: "Escape", del: "Delete", up: "ArrowUp", down: "ArrowDown",
|
|
left: "ArrowLeft", right: "ArrowRight", space: "Space", ctrl: "Control",
|
|
control: "Control", cmd: "Meta", command: "Meta", meta: "Meta", option: "Alt",
|
|
alt: "Alt", shift: "Shift", pageup: "PageUp", pagedown: "PageDown",
|
|
backspace: "Backspace", tab: "Tab", enter: "Enter", escape: "Escape",
|
|
delete: "Delete", home: "Home", end: "End",
|
|
};
|
|
const MODS = { Alt: 1, Control: 2, Meta: 4, Shift: 8 };
|
|
// what a chord with Control or Meta does on a Mac, where the page never
|
|
// sees the key turn into an edit
|
|
const MAC_COMMANDS = { a: "selectAll", c: "copy", x: "cut", v: "paste", z: "undo" };
|
|
|
|
function name(k) {
|
|
if (KEYS[k]) return k;
|
|
const a = ALIASES[k.toLowerCase()];
|
|
if (a) return a;
|
|
if (/^f\d{1,2}$/i.test(k)) return k.toUpperCase();
|
|
return k;
|
|
}
|
|
|
|
function describe(k, shift) {
|
|
const n = name(k);
|
|
if (KEYS[n]) return { key: KEYS[n].key || n, ...KEYS[n] };
|
|
if (n.length !== 1) throw new Error(`"${k}" isn't a key this knows: use a name like Enter, Tab, Escape, ArrowDown, PageDown, F5, or one character`);
|
|
const ch = shift ? n.toUpperCase() : n;
|
|
const up = n.toUpperCase();
|
|
let code = "";
|
|
if (/[a-z]/i.test(n)) code = "Key" + up;
|
|
else if (/[0-9]/.test(n)) code = "Digit" + n;
|
|
return { key: ch, code, keyCode: /[a-z0-9]/i.test(n) ? up.charCodeAt(0) : 0, text: ch };
|
|
}
|
|
|
|
const mac = /Mac/i.test(navigator.platform || navigator.userAgent);
|
|
|
|
// press presses one key or chord in the focused element.
|
|
export async function press(t, combo) {
|
|
const parts = String(combo).split("+").map((s) => s.trim());
|
|
if (parts.some((p) => p === "")) {
|
|
// "Control++" or a bare "+"
|
|
const i = parts.lastIndexOf("");
|
|
parts.splice(i - (i > 0 ? 1 : 0), 2, "+");
|
|
}
|
|
const keyName = parts.pop();
|
|
const mods = parts.map(name);
|
|
for (const m of mods) if (!(m in MODS)) throw new Error(`"${m}" isn't a modifier: use Control, Alt, Shift or Meta`);
|
|
let modifiers = mods.reduce((a, m) => a | MODS[m], 0);
|
|
const k = describe(keyName, !!(modifiers & 8));
|
|
const commands = [];
|
|
if (mac && (modifiers & 4) && MAC_COMMANDS[k.key.toLowerCase()]) commands.push(MAC_COMMANDS[k.key.toLowerCase()]);
|
|
let held = 0;
|
|
for (const m of mods) {
|
|
held |= MODS[m];
|
|
await send(t.tabId, "Input.dispatchKeyEvent", { type: "rawKeyDown", modifiers: held, key: m, code: KEYS[m].code, windowsVirtualKeyCode: KEYS[m].keyCode });
|
|
}
|
|
// a chord with Control, Alt or Meta types no text
|
|
const typed = k.text && !(modifiers & 7);
|
|
await send(t.tabId, "Input.dispatchKeyEvent", {
|
|
type: typed ? "keyDown" : "rawKeyDown", modifiers, key: k.key, code: k.code,
|
|
windowsVirtualKeyCode: k.keyCode, nativeVirtualKeyCode: k.keyCode,
|
|
...(typed ? { text: k.text, unmodifiedText: k.text } : {}),
|
|
...(commands.length ? { commands } : {}),
|
|
});
|
|
await sleep(20);
|
|
await send(t.tabId, "Input.dispatchKeyEvent", { type: "keyUp", modifiers, key: k.key, code: k.code, windowsVirtualKeyCode: k.keyCode });
|
|
for (const m of mods.reverse()) {
|
|
held &= ~MODS[m];
|
|
await send(t.tabId, "Input.dispatchKeyEvent", { type: "keyUp", modifiers: held, key: m, code: KEYS[m].code, windowsVirtualKeyCode: KEYS[m].keyCode });
|
|
}
|
|
}
|
|
|
|
// typeSlowly types text a key at a time, for a field that listens to
|
|
// each key (an autocomplete, a field that formats as you type).
|
|
export async function typeSlowly(t, text) {
|
|
for (const ch of String(text)) {
|
|
if (ch === "\n") { await press(t, "Enter"); continue; }
|
|
const k = /^[a-z0-9]$/i.test(ch) ? describe(ch.toLowerCase(), ch !== ch.toLowerCase()) : { key: ch, code: "", keyCode: 0, text: ch };
|
|
await send(t.tabId, "Input.dispatchKeyEvent", { type: "keyDown", key: k.key, code: k.code, windowsVirtualKeyCode: k.keyCode, text: k.text, unmodifiedText: k.text, modifiers: /[A-Z]/.test(ch) ? 8 : 0 });
|
|
await send(t.tabId, "Input.dispatchKeyEvent", { type: "keyUp", key: k.key, code: k.code, windowsVirtualKeyCode: k.keyCode });
|
|
await sleep(30 + Math.random() * 50);
|
|
}
|
|
}
|