cdp: add input.insertText

This commit is contained in:
Pierre Tachoire
2025-12-29 10:53:41 +01:00
parent d697944b5a
commit 3e52abf471
2 changed files with 47 additions and 0 deletions

View File

@@ -2571,6 +2571,37 @@ pub fn submitForm(self: *Page, submitter_: ?*Element, form_: ?*Element.Html.Form
return self.scheduleNavigation(action, opts, .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 { const RequestCookieOpts = struct {
is_http: bool = true, is_http: bool = true,
is_navigation: bool = false, is_navigation: bool = false,

View File

@@ -22,11 +22,13 @@ pub fn processMessage(cmd: anytype) !void {
const action = std.meta.stringToEnum(enum { const action = std.meta.stringToEnum(enum {
dispatchKeyEvent, dispatchKeyEvent,
dispatchMouseEvent, dispatchMouseEvent,
insertText,
}, cmd.input.action) orelse return error.UnknownMethod; }, cmd.input.action) orelse return error.UnknownMethod;
switch (action) { switch (action) {
.dispatchKeyEvent => return dispatchKeyEvent(cmd), .dispatchKeyEvent => return dispatchKeyEvent(cmd),
.dispatchMouseEvent => return dispatchMouseEvent(cmd), .dispatchMouseEvent => return dispatchMouseEvent(cmd),
.insertText => return insertText(cmd),
} }
} }
@@ -101,6 +103,20 @@ fn dispatchMouseEvent(cmd: anytype) !void {
// result already sent // 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 { fn clickNavigate(cmd: anytype, uri: std.Uri) !void {
const bc = cmd.browser_context.?; const bc = cmd.browser_context.?;