diff --git a/README.md b/README.md index f9e4ca8..63fcc50 100755 --- a/README.md +++ b/README.md @@ -88,6 +88,7 @@ If you want sharp terminal text on any terminal, Carbonyl and Browsh are good ch | --- | --- | | [Installation](docs/installation.md) | One-command setup, availability, and platform targets | | [Getting started](docs/getting-started.md) | Opening pages, typing, dialogs, and quitting | +| [Browser tools](docs/browser-tools.md) | Developer tools (F12), text copy/paste, and unpacked extensions | | [Terminal support](docs/terminals.md) | Graphics modes and compatibility | | [Troubleshooting](docs/troubleshooting.md) | Display issues, startup failures, and bug reports | | [Vimium and tabs](docs/vimium.md) | Bundled keyboard navigation, tabs, mouse coexistence, and limitations | @@ -96,7 +97,7 @@ If you want sharp terminal text on any terminal, Carbonyl and Browsh are good ch 1. **Multiplexers:** tmux and screen support. 2. **Distribution:** Homebrew and AUR packages, more clean-machine testing, automated dependency updates. -3. **Browser polish:** persistent browsing sessions, clipboard integration, dark mode that follows your terminal. +3. **Browser polish:** persistent browsing sessions, richer clipboard formats, dark mode that follows your terminal. 4. **Native Windows:** add native packages; Windows through WSL2 is supported today. The [installation plan](docs/plans/one-command-install.md) and [browser UI plan](docs/plans/browser-ui.md) define the work and release checks. See [GitHub Releases](https://github.com/codr1/termium/releases) for native packages and release notes. diff --git a/client/browser_tools_test.go b/client/browser_tools_test.go new file mode 100644 index 0000000..97ee1be --- /dev/null +++ b/client/browser_tools_test.go @@ -0,0 +1,49 @@ +package main + +import ( + "bytes" + "encoding/base64" + "testing" + + "github.com/gdamore/tcell/v2" + pb "termium/client/pb" +) + +func TestBrowserToolsKeysAndClipboard(t *testing.T) { + _ = uiScreen(t, 100, 40) + kh, ops := recorder() + kh.state = pb.BrowserState{ActiveTabId: "tab", Generation: 7} + kh.globalKey(key(tcell.KeyF12), false) + if len(*ops) != 1 || (*ops)[0].navigation.Action != pb.NavigationAction_DEVTOOLS { + t.Fatal("F12 did not open tools") + } + kh.globalKey(key(tcell.KeyF8), false) + if len(*ops) != 2 || (*ops)[1].selection.TabId != "tab" || (*ops)[1].selection.Generation != 7 { + t.Fatal("copy lost target identity") + } + old := graphicsOutput + var output bytes.Buffer + graphicsOutput = &output + t.Cleanup(func() { graphicsOutput = old }) + text := "世界\n\x1b]52;c;malicious\a" + kh.result(operationResult{clipboard: &text}) + want := "\x1b]52;c;" + base64.StdEncoding.EncodeToString([]byte(text)) + "\a" + if output.String() != want { + t.Fatalf("clipboard must encode content, got %q", output.String()) + } + output.Reset() + kh.result(operationResult{clipboard: &text, stale: true}) + if output.Len() != 0 { + t.Fatal("stale copy changed clipboard") + } + empty := "" + kh.result(operationResult{clipboard: &empty}) + if output.Len() != 0 { + t.Fatal("empty selection cleared clipboard") + } + kh.pasteEvent(true) + kh.globalKey(key(tcell.KeyF12), false) + if len(*ops) != 2 { + t.Fatal("paste triggered developer tools") + } +} diff --git a/client/config.go b/client/config.go index 8e3a619..29dc060 100644 --- a/client/config.go +++ b/client/config.go @@ -4,9 +4,11 @@ import ( "flag" "fmt" "os" + "path/filepath" ) type Config struct { + Extensions []string InitialURL string Homepage string SetHomepage string @@ -29,6 +31,17 @@ type Config struct { func parseFlags() (*Config, error) { cfg := &Config{} + flag.Func("extension", "Load an unpacked Chromium extension directory (repeatable)", func(value string) error { + absolute, err := filepath.Abs(value) + if err != nil { + return err + } + if value == "" { + return fmt.Errorf("extension directory is required") + } + cfg.Extensions = append(cfg.Extensions, absolute) + return nil + }) showVersion := false flag.BoolVar(&showVersion, "version", false, "Print version and exit") @@ -117,6 +130,9 @@ func parseFlags() (*Config, error) { } // Validate server address format + if cfg.ServerAddr != "" && len(cfg.Extensions) > 0 { + return nil, fmt.Errorf("--extension applies to a local browser; configure extensions on the remote server") + } if cfg.ServerAddr != "" { // TODO: Add validation for ip:port format } diff --git a/client/input_dispatcher.go b/client/input_dispatcher.go index 17ac9dc..d57900e 100644 --- a/client/input_dispatcher.go +++ b/client/input_dispatcher.go @@ -14,11 +14,13 @@ import ( ) type browserOperation struct { + selection *pb.NavigationRequest input *pb.InputEvent navigation *pb.NavigationRequest viewport *pb.ViewportSize } type operationResult struct { + clipboard *string operation browserOperation state *pb.BrowserState err error @@ -105,6 +107,9 @@ func (d *inputDispatcher) run() { if o.input != nil { generation = o.input.Generation } + if o.selection != nil { + generation = o.selection.Generation + } if o.navigation != nil { generation = o.navigation.Generation } @@ -128,6 +133,13 @@ func (d *inputDispatcher) run() { result := operationResult{operation: o} var trailer metadata.MD switch { + case o.selection != nil: + var reply *pb.Message + reply, result.err = d.client.GetSelection(ctx, o.selection, grpc.Trailer(&trailer)) + if reply != nil && result.err == nil { + result.state = reply.State + result.clipboard = &reply.Text + } case o.input != nil: var reply *pb.Message reply, result.err = d.client.SendInput(ctx, o.input, grpc.Trailer(&trailer)) diff --git a/client/keyboard.go b/client/keyboard.go index 75abd4a..f67e1fa 100644 --- a/client/keyboard.go +++ b/client/keyboard.go @@ -2,6 +2,7 @@ package main import ( "context" + "encoding/base64" "fmt" "image" "net/url" @@ -127,6 +128,24 @@ func (kh *KeyboardHandler) applyState(state *pb.BrowserState) { } } func (kh *KeyboardHandler) result(result operationResult) { + if result.clipboard != nil && result.err == nil && !result.stale { + text := *result.clipboard + if text == "" { + kh.status = "No text selected" + return + } + if len(text) > 64*1024 { + kh.status = "Selection exceeds clipboard limit" + return + } + _, err := fmt.Fprintf(graphicsOutput, "\x1b]52;c;%s\a", base64.StdEncoding.EncodeToString([]byte(text))) + if err != nil { + kh.status = err.Error() + } else { + kh.status = "Copy sent to terminal clipboard" + } + return + } if result.operation.navigation != nil { if kh.pendingNavigation != nil && kh.pendingNavigation != result.operation.navigation { // A newer address/tab command owns the editor and loading state. @@ -271,6 +290,15 @@ func (kh *KeyboardHandler) action(id string) { return } switch id { + case "devtools": + kh.navigate(pb.NavigationAction_DEVTOOLS, "") + case "extensions": + kh.navigate(pb.NavigationAction_EXTENSIONS, "") + case "copy": + kh.menu = false + if kh.submit == nil || !kh.submit(browserOperation{selection: &pb.NavigationRequest{TabId: kh.state.ActiveTabId, Generation: kh.state.Generation}}) { + kh.status = "Input queue is busy; try again" + } case "home": kh.navigate(pb.NavigationAction_HOME, "") case "address": @@ -346,6 +374,10 @@ func (kh *KeyboardHandler) globalKey(ev *tcell.EventKey, modal bool) (bool, bool switch { case ctrl(ev, tcell.KeyCtrlL, 'l'): kh.openAddress() + case ev.Key() == tcell.KeyF12: + kh.action("devtools") + case ev.Key() == tcell.KeyF8 || (kh.focus == "page" && ctrl(ev, tcell.KeyCtrlC, 'c')): + kh.action("copy") case ev.Key() == tcell.KeyF10: kh.action("menu") case ev.Key() == tcell.KeyF1: diff --git a/client/navigation_ui.go b/client/navigation_ui.go index 2bca86f..12884eb 100644 --- a/client/navigation_ui.go +++ b/client/navigation_ui.go @@ -6,7 +6,7 @@ import ( "image" ) -var menuIDs = []string{"newtab", "closetab", "reopentab", "tabs", "home", "back", "forward", "reload", "address", "pointer", "help", "quit"} +var menuIDs = []string{"newtab", "closetab", "reopentab", "tabs", "home", "back", "forward", "reload", "address", "pointer", "devtools", "copy", "extensions", "help", "quit"} func (kh *KeyboardHandler) menuLabels() []string { if kh.tabsMenu { @@ -28,7 +28,7 @@ func (kh *KeyboardHandler) menuLabels() []string { if kh.pointerMode { pointer = "Mouse keys: on F6" } - return []string{"New tab Ctrl+T", "Close tab Ctrl+W", "Reopen closed tab X", "All tabs", "Home Alt+Home", "Back Alt+Left", "Forward Alt+Right", reload, "Open address Ctrl+L", pointer, "Shortcut help F1", "Quit Ctrl+Q"} + return []string{"New tab Ctrl+T", "Close tab Ctrl+W", "Reopen closed tab X", "All tabs", "Home Alt+Home", "Back Alt+Left", "Forward Alt+Right", reload, "Open address Ctrl+L", pointer, "Developer tools F12", "Copy selected text F8", "Extensions", "Shortcut help F1", "Quit Ctrl+Q"} } func (kh *KeyboardHandler) menuRect() image.Rectangle { width := min(34, sDims.Width) @@ -117,7 +117,7 @@ func (kh *KeyboardHandler) Draw(s tcell.Screen) { } } if kh.help { - lines := []string{"Termium shortcuts", "", "Ctrl+L Address (select all)", "Alt+Left / Alt+Right Back / Forward", "Alt+Home Home page", "Ctrl+R or F5 Reload / Stop", "F10 Menu F6 Mouse keys", "Ctrl+Q Quit Escape x3 Emergency exit", "", "Vimium: f/F links · hjkl scroll · i insert", "t new tab · J/K switch · x close · X reopen", "? Vimium help · Escape cancels a mode", "Ctrl+T new tab · Ctrl+W close tab", "Mouse: click, drag, wheel, right/middle buttons.", "Mouse keys: arrows/hjkl, Enter click, Space drag,", "r right-click, m middle-click, u/d scroll.", "", "Enter or Escape to close"} + lines := []string{"Termium shortcuts", "", "Ctrl+L Address (select all)", "Alt+Left / Alt+Right Back / Forward", "Alt+Home Home page", "Ctrl+R or F5 Reload / Stop", "F10 Menu F6 Mouse keys", "F12 Developer tools F8/Ctrl+C Copy", "Paste using your terminal paste shortcut", "Ctrl+Q Quit Escape x3 Emergency exit", "", "Vimium: f/F links · hjkl scroll · i insert", "t new tab · J/K switch · x close · X reopen", "? Vimium help · Escape cancels a mode", "Ctrl+T new tab · Ctrl+W close tab", "Mouse: click, drag, wheel, right/middle buttons.", "Mouse keys: arrows/hjkl, Enter click, Space drag,", "r right-click, m middle-click, u/d scroll.", "", "Enter or Escape to close"} drawOverlay(s, lines) } if kh.quitConfirm { diff --git a/client/server_launcher.go b/client/server_launcher.go index 053cd2d..c603f76 100644 --- a/client/server_launcher.go +++ b/client/server_launcher.go @@ -116,6 +116,9 @@ func startServer() error { } args := []string{loc.scriptPath} + for _, directory := range cfg.Extensions { + args = append(args, "--extension", directory) + } if cfg.Homepage != "" { args = append(args, "--homepage", cfg.Homepage) } diff --git a/docs/README.md b/docs/README.md index 4710ce0..96d2e62 100644 --- a/docs/README.md +++ b/docs/README.md @@ -10,6 +10,7 @@ Start with [Quickstart](quickstart.md): install, open your first page, and learn - [Terminal support](terminals.md): choosing a display mode when troubleshooting. - [Troubleshooting](troubleshooting.md): resolving problems and reporting bugs. - [Vimium and tabs](vimium.md): keyboard navigation, real tabs, and mouse coexistence. +- [Browser tools](browser-tools.md): developer tools, text copy/paste, and unpacked extensions. ## Contributor guides @@ -23,4 +24,6 @@ Start with [Quickstart](quickstart.md): install, open your first page, and learn - [Browser UI architecture](plans/browser-ui.md): bundled Vimium, tab identity, and keyboard routing. - [Graphics audit](plans/rendering-audit.md): Kitty/Sixel correctness, measured encoding costs, performance priorities, and help UI integration. +- [Browser audio research](research/browser-audio.md): headless playback feasibility and a local audio-sink proof. + Pages marked **planned** describe work that has not shipped. The usage guides describe the current application. diff --git a/docs/browser-tools.md b/docs/browser-tools.md new file mode 100644 index 0000000..9eedcbb --- /dev/null +++ b/docs/browser-tools.md @@ -0,0 +1,58 @@ +# Developer tools, clipboard and extensions + +## Inspect a page + +Press **F12**, or choose **Developer tools** from the F10 menu. Termium opens +Chromium's built-in DevTools in a separate Termium tab, initially showing +Elements. Use its panels to inspect HTML/CSS, run console commands, examine +network requests, and debug scripts. Mouse and keyboard input work in the tools. +Vimium does not run on this browser-internal page. + +Switch between the page and its tools using the tab bar or F10 → All tabs. +Ctrl+W closes the tools without closing the inspected page. Closing the inspected +page also removes its tools. Termium keeps one inspector at a time; opening tools +for another page replaces the previous inspector. No debugging TCP port is opened. +Breakpoints pause the inspected page, so its frames and input can stop until you +resume execution in DevTools. + +## Select, copy and paste + +Select webpage text by dragging with the mouse, using mouse keys (F6, Space to +hold/release a drag), or using the page's keyboard selection controls. Press +**F8** or **Ctrl+C** while page input is focused, or choose **Copy selected text** +in F10. Text selections in inputs, textareas, focused frames and open shadow DOM +are supported. Password fields are excluded. A selection may contain up to 64 KiB +of UTF-8 text; empty selections leave the clipboard alone. + +Copy uses the terminal's OSC 52 clipboard support. Your terminal must allow it; +“Copy sent to terminal clipboard” confirms sending the request, not permission +from the terminal. It also works over SSH when the local terminal supports OSC 52. +Copy is plain text, not HTML or images, and does not grant webpages clipboard-read +permission. Ctrl+C in the address editor retains its local editor behavior. + +Paste with your **terminal's paste shortcut** (commonly Ctrl+Shift+V on Linux or +Cmd+V on macOS). Termium forwards bracketed paste as literal text to the focused +page input, including Unicode and newlines, without interpreting it as Vimium +commands. A bare Ctrl+V key is not a portable request to read the host clipboard. +Clipboard access from a website's own JavaScript remains subject to Chromium's +permissions; this feature is the explicit Termium copy action. + +## Load extensions + +Pass an unpacked Chromium extension directory containing `manifest.json`: + +```sh +termium --extension /path/to/extension https://example.com +termium --extension /path/to/first --extension "/path/with spaces/second" +``` + +Directories are resolved from your current working directory. Requested +extensions load before the initial page; Vimium remains installed. Invalid or +incompatible extensions produce a browser-startup error rather than being skipped. +Only load extensions you trust with the pages you browse. + +Choose **Extensions** from F10 to view Chromium's extension manager, including +extension details and options. Loading is session-specific: supply the arguments +again on the next launch. This does not install CRX files or implement Chrome Web +Store installation, toolbar action buttons, or profile persistence. With a remote +Termium server, configure `--extension` on that server instead of the client. diff --git a/docs/research/browser-audio.md b/docs/research/browser-audio.md new file mode 100644 index 0000000..ee11f3e --- /dev/null +++ b/docs/research/browser-audio.md @@ -0,0 +1,49 @@ +# Browser audio feasibility — 2026-10-01 + +Sound is feasible without moving audio through Go, gRPC or the terminal graphics +protocol when Chromium runs on the user's machine. Chromium can use the host's +normal audio output. This investigation does not enable audio in Termium yet. + +## Source evidence + +The installed Puppeteer launcher adds `--mute-audio` for headless launches. +Termium launches `headless: true` without filtering that default. Puppeteer's +[official launch documentation](https://pptr.dev/next/api/puppeteer.puppeteernode.launch) +explicitly demonstrates `ignoreDefaultArgs: ['--mute-audio']`. + +For an eventual implementation, remove only that default flag and provide an +explicit mute control. Preserve normal autoplay and user-gesture requirements; +there is no need to grant microphone access or disable autoplay protections. +Audio playback is separate from audio capture. + +[WSLg](https://github.com/microsoft/wslg) provides an audio server for Linux apps. +An SSH session does not imply audio forwarding: unmuting a browser on a remote +server would play on that server's configured output. Remote sound would need a +separate audio transport and client playback, not a Sixel/Kitty extension. + +## Local proof + +Host: harpe, Linux under WSL, PulseAudio server `unix:/mnt/wslg/PulseServer`. +A private null sink was created for this test; the browser's `PULSE_SINK` selected +it. Only that sink's monitor was recorded. No default device was changed and no +sound was played through the user's speakers. The sink was unloaded afterward. + +Two fresh headless Chromium launches used identical pages. A real CDP-dispatched +button click started a low-gain Web Audio oscillator; AudioContext reported +`running` in both. Raw mono PCM was observed through `parec` at 48 kHz, signed +16-bit little endian. Each run waited 1.8 seconds after the click; recording also +included startup/shutdown, so sample totals are not a latency measurement. + +| Launch | Recorded samples | Nonzero samples | Peak absolute PCM amplitude | +|---|---:|---:|---:| +| Default Puppeteer mute | 38,841 | 0 | 0 | +| Remove `--mute-audio` | 38,795 | 4,452 | 1,639 | + +This proves audio reaches a host audio sink here. It does not verify physical +speakers, codecs on every site, native macOS output, synchronization with Termium's +rendered frames, or remote playback. Before enabling by default, test those local +platform paths, playback/pause across tabs, browser shutdown, and mute behavior. +No performance claim is made. + +Probe and raw log: `/tmp/termium-audio-20261001/{probe.cjs,result.log}` on harpe. +The probe uses only synthetic audio and a dedicated sink. diff --git a/proto/bc.proto b/proto/bc.proto index e094366..1a932b6 100644 --- a/proto/bc.proto +++ b/proto/bc.proto @@ -13,6 +13,7 @@ service BrowserControl { rpc GetCurrentUrl (Empty) returns (Url) {} rpc GetBrowserState (Empty) returns (BrowserState) {} rpc BrowserCommand (NavigationRequest) returns (BrowserState) {} + rpc GetSelection (NavigationRequest) returns (Message) {} rpc SendInput (InputEvent) returns (Message) {} // Paced clients request only as much capture work as they can present. rpc CaptureScreenshot (ScreenshotRequest) returns (Screenshot) {} @@ -84,6 +85,8 @@ enum NavigationAction { CLOSE_TAB = 7; REOPEN_TAB = 8; HOME = 9; + DEVTOOLS = 10; + EXTENSIONS = 11; } message NavigationRequest { diff --git a/server/src/browser-controls.ts b/server/src/browser-controls.ts index 3cb6b9c..c5e2a84 100644 --- a/server/src/browser-controls.ts +++ b/server/src/browser-controls.ts @@ -324,7 +324,10 @@ export class BrowserControls { const cdp = await this.session(); this.x = event.x; this.y = event.y; if (inputTraceEnabled) inputTrace('pointer.session', { session: cdp.id() }); const base = { x: event.x, y: event.y, modifiers: event.modifiers }; - await cdp.send('Input.dispatchMouseEvent', { ...base, type: 'mouseMoved', buttons: this.held }); + // Chromium needs the active button as well as the held mask + // to treat this as a drag (including text selection). + const button = this.held & 1 ? 'left' : this.held & 2 ? 'right' : this.held & 4 ? 'middle' : 'none'; + await cdp.send('Input.dispatchMouseEvent', { ...base, type: 'mouseMoved', buttons: this.held, button }); if (event.kind === InputKind.WHEEL_INPUT) { await cdp.send('Input.dispatchMouseEvent', { ...base, type: 'mouseWheel', buttons: this.held, deltaX: event.deltaX, deltaY: event.deltaY }); break; diff --git a/server/src/browser-session.ts b/server/src/browser-session.ts index 36faf8f..fc8ec0b 100644 --- a/server/src/browser-session.ts +++ b/server/src/browser-session.ts @@ -30,6 +30,8 @@ export class BrowserSession { private initializing?: Promise; private refreshing?: Promise; private records = new Map(); + private inspector?: Tab & { owner: string; targetId: string }; + private inspectorSelected = false; private selected = ''; private documentGeneration = -1; private epoch = 0; @@ -97,7 +99,15 @@ export class BrowserSession { candidates = candidates.filter(info => this.records.get(info.targetId)!.window === window); } if (candidates.length !== 1) throw Error('Browser is switching tabs; retry'); - const active = this.records.get(candidates[0].targetId)!; + let active = this.records.get(candidates[0].targetId)!; + if (this.inspector && (this.inspector.page.isClosed() || !byId.has(this.inspector.owner))) { + const old = this.inspector; + this.inspector = undefined; + this.inspectorSelected = false; + this.records.delete(old.id); + if (!old.page.isClosed()) await old.page.close().catch(() => {}); + } + if (this.inspectorSelected && this.inspector) active = this.inspector; if (replacement) await this.openHome(active); if (this.selected !== active.id || this.documentGeneration !== active.controls.generation) { const old = this.records.get(this.selected); @@ -107,8 +117,10 @@ export class BrowserSession { this.epoch++; if (old && old !== active) await old.controls.resetInput().catch(() => {}); } - return { active, tabs: infos.map(info => this.records.get(info.targetId)!), generation: this.epoch, - titles: new Map(infos.map(info => [info.targetId, info.title])) }; + const tabs = infos.map(info => this.records.get(info.targetId)!); + const titles = new Map(infos.map(info => [info.targetId, info.title])); + if (this.inspector) { tabs.push(this.inspector); titles.set(this.inspector.id, 'Developer tools'); } + return { active, tabs, generation: this.epoch, titles }; } async ensurePage() { return (await this.snapshot()).active.page; } @@ -146,6 +158,17 @@ export class BrowserSession { if (!targeted) stale(); if (request.generation && request.generation !== s.generation) stale(); switch (request.action) { + case NavigationAction.DEVTOOLS: + if (targeted !== s.active) stale(); + await this.openInspector(targeted); + break; + case NavigationAction.EXTENSIONS: { + this.inspectorSelected = false; + const page = await this.browser.newPage(); + await page.goto('chrome://extensions/', { waitUntil: 'domcontentloaded' }); + await page.bringToFront(); + break; + } case NavigationAction.HOME: if (targeted !== s.active) stale(); await this.openHome(s.active); @@ -156,6 +179,7 @@ export class BrowserSession { try { url = new URL(request.url); } catch { throw Object.assign(Error('Enter a valid HTTP or HTTPS address'), { code: grpc.status.INVALID_ARGUMENT }); } if (!['http:', 'https:'].includes(url.protocol)) throw Object.assign(Error('Use an HTTP or HTTPS address'), { code: grpc.status.INVALID_ARGUMENT }); } + this.inspectorSelected = false; const p = await this.browser.newPage(); await p.bringToFront(); const created = await this.snapshot(); @@ -165,8 +189,18 @@ export class BrowserSession { else await this.openHome(ownTab); break; } - case NavigationAction.SELECT_TAB: await targeted!.page.bringToFront(); break; + case NavigationAction.SELECT_TAB: + this.inspectorSelected = targeted === this.inspector; + if (!this.inspectorSelected) await targeted.page.bringToFront(); + break; case NavigationAction.CLOSE_TAB: + if (targeted === this.inspector) { + await this.inspector.page.close(); + this.records.delete(targeted.id); + this.inspector = undefined; + this.inspectorSelected = false; + break; + } // Page.close with runBeforeUnload keeps cancellation usable. await targeted!.page.close({ runBeforeUnload: true }); break; @@ -185,6 +219,63 @@ export class BrowserSession { }); } + private async openInspector(owner: Tab) { + if (owner === this.inspector) return; + if (this.inspector && this.inspector.owner !== owner.id) { + await this.inspector.page.close(); + this.records.delete(this.inspector.id); + this.inspector = undefined; + } + if (!this.inspector || this.inspector.page.isClosed()) { + const { targetId } = await this.cdp.send('Target.openDevTools', { targetId: owner.id, panelId: 'elements' }); + // Puppeteer categorizes the built-in DevTools frontend as "other"; + // it has no normal tab-strip metadata. Keep it as an explicit tab. + const target = await this.browser.waitForTarget(t => (t as any)._targetId === targetId, { timeout: 3000 }); + const page = await target.page(); + if (!page) throw Error('Chromium did not expose its Developer Tools page'); + const controls = new BrowserControls(async () => page); + await controls.attach(page); + this.inspector = { id: `devtools:${targetId}`, targetId, owner: owner.id, page, controls, window: owner.window }; + this.records.set(this.inspector.id, this.inspector); + } + this.inspectorSelected = true; + } + + getSelection(request: NavigationRequest) { + return this.enqueue(async () => { + const s = await this.snapshot(); + if ((request.tabId && request.tabId !== s.active.id) || (request.generation && request.generation !== s.generation)) stale(); + let text = ''; + let frame = s.active.page.mainFrame(); + for (let depth = 0; depth < 32; depth++) { + // Follow the focused element chain rather than document.hasFocus: + // a headless page can have a valid selection without OS focus. + const handle = await frame.evaluateHandle(() => { + let element = document.activeElement; + while (element?.shadowRoot?.activeElement) element = element.shadowRoot.activeElement; + return element; + }); + try { + const element = handle.asElement(); + const child = element ? await element.contentFrame() : null; + if (child) { frame = child; continue; } + text = await frame.evaluate(element => { + if (element instanceof HTMLInputElement || element instanceof HTMLTextAreaElement) { + if (element instanceof HTMLInputElement && element.type === 'password') return ''; + return element.value.substring(element.selectionStart ?? 0, element.selectionEnd ?? 0); + } + return window.getSelection()?.toString() ?? ''; + }, handle); + break; + } finally { await handle.dispose(); } + } + if (Buffer.byteLength(text, 'utf8') > 64 * 1024) throw Error('Selection exceeds the 64 KiB clipboard limit'); + const after = await this.snapshot(); + if (after.generation !== s.generation || after.active.id !== s.active.id) stale(); + return { text, state: await this.state() }; + }); + } + private async openHome(tab: Tab) { await tab.controls.resetInput(); if (this.vimium.home === this.vimium.welcome) { diff --git a/server/src/server.ts b/server/src/server.ts index cfa2256..64eb5e8 100644 --- a/server/src/server.ts +++ b/server/src/server.ts @@ -52,6 +52,7 @@ let dialogIdCounter = 0; // CLI setup with Commander program + .option('--extension ', 'Load an unpacked extension (repeatable)', (value: string, previous: string[]) => [...previous, path.resolve(value)], []) .option('--homepage ', 'Startup and new-tab page', 'https://termium.dev/welcome/') .option('-b, --browser ', 'Connect to an existing browser instance (ip:port)', '') .option('-d, --debug [filename]', 'Enable debug mode (log to stdout or optional file)', '') @@ -171,6 +172,15 @@ async function launchBrowser() { ] }); } + try { + for (const directory of options.extension as string[]) { + await browser!.installExtension(directory); + } + } catch (error) { + if (!options.browser) await browser!.close(); + browser = null; + throw error; + } startCaptureDiagnostics(browser!); } @@ -210,6 +220,10 @@ const browserControlHandlers: BrowserControlServer = { try { callback(null, await controls.command(call.request)); } catch (error) { callback({ code:(error as any).code ?? grpc.status.INTERNAL, message:(error as Error).message, metadata:(error as any).metadata }); } }, + getSelection: async (call, callback) => { + try { callback(null, await controls.getSelection(call.request)); } + catch (error) { callback({ code: (error as any).code ?? grpc.status.INTERNAL, message: (error as Error).message, metadata: (error as any).metadata }); } + }, sendInput: async (call, callback) => { try { const state = await controls.input(call.request); callback(null, { text:'Input dispatched', state }); } catch (error) { callback({ code:(error as any).code ?? grpc.status.INTERNAL, message:(error as Error).message, metadata:(error as any).metadata }); } diff --git a/server/tests/browser-tools.integration.cjs b/server/tests/browser-tools.integration.cjs new file mode 100644 index 0000000..a3d4a2b --- /dev/null +++ b/server/tests/browser-tools.integration.cjs @@ -0,0 +1,85 @@ +const {test}=require('node:test'); +const assert=require('node:assert/strict'); +const http=require('node:http'); +const puppeteer=require('puppeteer'); +const {BrowserSession}=require('../dist/src/browser-session'); +const {NavigationRequest,NavigationAction:A,InputEvent,InputKind}=require('../dist/generated/bc'); + +test('DevTools inspect, capture, switch, close and selection/paste use the active page', {timeout:60000},async t=>{ + const server=http.createServer((q,r)=>{r.setHeader('content-type','text/html');r.end('Tools fixture

inspect this text

');}); + await new Promise(r=>server.listen(0,'127.0.0.1',r));t.after(()=>server.close()); + const b=await puppeteer.launch({headless:true,pipe:true,enableExtensions:true});t.after(()=>b.close()); + const s=new BrowserSession(async()=>b); + const cmd=(action,fields={})=>s.command(NavigationRequest.fromPartial({action,...fields})); + await s.ensurePage();await cmd(A.NAVIGATE,{url:`http://127.0.0.1:${server.address().port}/`}); + const page=await s.ensurePage();await page.waitForSelector('#text'); + const rect=await page.$eval('#text',e=>{const r=e.getBoundingClientRect();return {x:r.x,y:r.y,width:r.width,height:r.height}}); + const pointer=(x,buttons,clickCount)=>s.input(InputEvent.fromPartial({kind:InputKind.POINTER_INPUT,x:Math.round(x),y:Math.round(rect.y+rect.height/2),buttons,clickCount})); + await pointer(rect.x,1,1);await pointer(rect.x+rect.width+2,1,0);await pointer(rect.x+rect.width+2,0,1); + let state=await s.state();const owner=state.activeTabId; + assert.equal((await s.getSelection(NavigationRequest.fromPartial({tabId:owner,generation:state.generation}))).text,'inspect this text'); + await page.click('#edit');await s.input(InputEvent.fromPartial({kind:InputKind.PASTE_INPUT,text:'literal f 世界\nnext'})); + assert.equal(await page.$eval('#edit',e=>e.value),'literal f 世界\nnext'); + await page.$eval('#edit',e=>e.setSelectionRange(10,14)); + assert.equal((await s.getSelection(NavigationRequest.fromPartial({}))).text,'世界\nn'); + await page.click('input');await page.$eval('input',e=>e.select()); + assert.equal((await s.getSelection(NavigationRequest.fromPartial({}))).text,''); + await page.evaluate(async()=>{const f=document.createElement('iframe');f.srcdoc='';const loaded=new Promise(r=>f.onload=r);document.body.append(f);await loaded;}); + const frame=page.frames().find(f=>f!==page.mainFrame()); + await frame.click('input');await frame.$eval('input',e=>e.select()); + assert.equal((await s.getSelection(NavigationRequest.fromPartial({}))).text,'frame selection'); + await page.evaluate(()=>{const host=document.createElement('div');document.body.append(host);const root=host.attachShadow({mode:'open'});root.innerHTML='';root.querySelector('textarea').focus();root.querySelector('textarea').select();}); + assert.equal((await s.getSelection(NavigationRequest.fromPartial({}))).text,'shadow selection'); + await assert.rejects(s.getSelection(NavigationRequest.fromPartial({tabId:'wrong'})),/changed/); + state=await cmd(A.DEVTOOLS);assert.match(state.activeTabId,/^devtools:/);const inspector=state.activeTabId; + const dev=await s.ensurePage();assert.match(dev.url(),/^devtools:/); + await s.setViewport(800,600);assert.ok((await s.capture('png')).data.length>1000); + // The real built-in frontend must initialize its panels, not just load a URL. + await dev.waitForFunction(()=>document.querySelector('.tabbed-pane')!==null,{timeout:5000}); + // Panels can render before the frontend attaches to its inspected target, + // especially on native macOS. Wait for the actual connection, not a DOM shell. + const inspected = await dev.waitForFunction(async () => { + const SDK = await import('./core/sdk/sdk.js'); + const target = SDK.TargetManager.TargetManager.instance().primaryPageTarget(); + if (!target) return false; + const reply = await target.runtimeAgent().invoke_evaluate({expression:'document.title',returnByValue:true}); + return reply.result?.value === 'Tools fixture'; + }, {timeout:10000}); + assert.equal(await inspected.jsonValue(),true,'DevTools is connected to the requested page'); + await inspected.dispose(); + state=await cmd(A.SELECT_TAB,{tabId:owner});assert.equal(state.activeTabId,owner); + state=await cmd(A.DEVTOOLS);assert.equal(state.activeTabId,inspector,'reuse inspector'); + state=await cmd(A.CLOSE_TAB,{tabId:inspector});assert.equal(state.activeTabId,owner); + state=await cmd(A.DEVTOOLS);const secondInspector=state.activeTabId; + state=await cmd(A.CLOSE_TAB,{tabId:owner}); + assert.ok(!state.tabs.some(t=>t.id===secondInspector),'closing inspected page removes inspector'); + state=await cmd(A.EXTENSIONS);assert.match(state.url,/^chrome:\/\/extensions/); +}); + +test('server loads requested unpacked extensions before opening a page', {timeout:30000}, async t => { + const fs=require('node:fs/promises'), os=require('node:os'), path=require('node:path'); + const {spawn}=require('node:child_process'); + const grpc=require('@grpc/grpc-js'); + const {BrowserControlClient}=require('../dist/generated/bc'); + const dir=await fs.mkdtemp(path.join(os.tmpdir(),'termium-tools-'));t.after(()=>fs.rm(dir,{recursive:true,force:true})); + const ext=path.join(dir,'extension with spaces');await fs.mkdir(ext); + await fs.writeFile(path.join(ext,'manifest.json'),JSON.stringify({manifest_version:3,name:'Termium integration fixture',version:'1.0',content_scripts:[{matches:['http://127.0.0.1/*'],js:['content.js'],run_at:'document_end'}]})); + await fs.writeFile(path.join(ext,'content.js'),`document.title='Extension loaded';`); + const fixture=http.createServer((q,r)=>{r.setHeader('content-type','text/html');r.end('Before extension

page

')});await new Promise(r=>fixture.listen(0,'127.0.0.1',r));t.after(()=>fixture.close()); + const child=spawn(process.execPath,[path.join(__dirname,'../dist/src/server.js'),'--socket',path.join(dir,'server.sock'),'--extension',ext,'--homepage','about:blank'],{stdio:['ignore','pipe','pipe']}); + let log='';child.stderr.on('data',d=>{log+=d}); + const closed=new Promise(resolve=>child.once('close',resolve)); + t.after(async()=>{child.kill('SIGTERM');await closed;}); + await new Promise((resolve,reject)=>{ + const timer=setTimeout(()=>reject(Error('server readiness timeout '+log)),5000); + child.once('error',e=>{clearTimeout(timer);reject(e)}); + child.stdout.on('data',d=>{log+=d;if(log.includes('TERMIUM_READY')){clearTimeout(timer);resolve()}}); + }); + const client=new BrowserControlClient('unix:'+path.join(dir,'server.sock'),grpc.credentials.createInsecure());t.after(()=>client.close()); + const call=(method,arg)=>new Promise((resolve,reject)=>client[method](arg,{deadline:Date.now()+10000},(e,r)=>e?reject(e):resolve(r))); + await call('openTab',{}); + await call('browserCommand',NavigationRequest.fromPartial({action:A.NAVIGATE,url:`http://127.0.0.1:${fixture.address().port}/`})); + const end=Date.now()+5000;let state; + do {state=await call('getBrowserState',{});if(state.title==='Extension loaded')break;await new Promise(r=>setTimeout(r,20))}while(Date.now()