From 3e52abf47146ce2914d848555b14b9d46fc8389b Mon Sep 17 00:00:00 2001 From: Pierre Tachoire Date: Mon, 29 Dec 2025 10:53:41 +0100 Subject: [PATCH] cdp: add input.insertText --- src/browser/Page.zig | 31 +++++++++++++++++++++++++++++++ src/cdp/domains/input.zig | 16 ++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/src/browser/Page.zig b/src/browser/Page.zig index 37f92b57..cbda9900 100644 --- a/src/browser/Page.zig +++ b/src/browser/Page.zig @@ -2571,6 +2571,37 @@ pub fn submitForm(self: *Page, submitter_: ?*Element, form_: ?*Element.Html.Form return self.scheduleNavigation(action, opts, .form); } +// insertText is a shortcut to insert text into the active element. +pub fn insertText(self: *Page, v: []const u8) !void { + const html_element = self.document._active_element orelse return; + + if (html_element.is(Element.Html.Input)) |input| { + const input_type = input._input_type; + if (input_type == .radio or input_type == .checkbox) { + return; + } + + // If the input is selected, replace the existing value + if (input._selected) { + const new_value = try self.arena.dupe(u8, v); + try input.setValue(new_value, self); + input._selected = false; + return; + } + + // Or append the value + const current_value = input.getValue(); + const new_value = try std.mem.concat(self.arena, u8, &.{ current_value, v }); + try input.setValue(new_value, self); + } + + if (html_element.is(Element.Html.TextArea)) |textarea| { + const current_value = textarea.getValue(); + const new_value = try std.mem.concat(self.arena, u8, &.{ current_value, v }); + try textarea.setValue(new_value, self); + } +} + const RequestCookieOpts = struct { is_http: bool = true, is_navigation: bool = false, diff --git a/src/cdp/domains/input.zig b/src/cdp/domains/input.zig index 777f7049..f97ee123 100644 --- a/src/cdp/domains/input.zig +++ b/src/cdp/domains/input.zig @@ -22,11 +22,13 @@ pub fn processMessage(cmd: anytype) !void { const action = std.meta.stringToEnum(enum { dispatchKeyEvent, dispatchMouseEvent, + insertText, }, cmd.input.action) orelse return error.UnknownMethod; switch (action) { .dispatchKeyEvent => return dispatchKeyEvent(cmd), .dispatchMouseEvent => return dispatchMouseEvent(cmd), + .insertText => return insertText(cmd), } } @@ -101,6 +103,20 @@ fn dispatchMouseEvent(cmd: anytype) !void { // result already sent } +// https://chromedevtools.github.io/devtools-protocol/tot/Input/#method-insertText +fn insertText(cmd: anytype) !void { + const params = (try cmd.params(struct { + text: []const u8, // The text to insert + })) orelse return error.InvalidParams; + + const bc = cmd.browser_context orelse return; + const page = bc.session.currentPage() orelse return; + + try page.insertText(params.text); + + try cmd.sendResult(null, .{}); +} + fn clickNavigate(cmd: anytype, uri: std.Uri) !void { const bc = cmd.browser_context.?;