File tree Expand file tree Collapse file tree 2 files changed +41
-0
lines changed Expand file tree Collapse file tree 2 files changed +41
-0
lines changed Original file line number Diff line number Diff line change @@ -1013,6 +1013,31 @@ pub const Page = struct {
10131013 }
10141014 }
10151015
1016+ // insertText is a shortcut to insert text into the active element.
1017+ pub fn insertText (self : * Page , v : []const u8 ) ! void {
1018+ const Document = @import ("dom/document.zig" ).Document ;
1019+ const element = (try Document .getActiveElement (@ptrCast (self .window .document ), self )) orelse return ;
1020+ const node = parser .elementToNode (element );
1021+
1022+ const tag = (try parser .nodeHTMLGetTagType (node )) orelse return ;
1023+ switch (tag ) {
1024+ .input = > {
1025+ const input_type = try parser .inputGetType (@ptrCast (element ));
1026+ if (std .mem .eql (u8 , input_type , "text" )) {
1027+ const value = try parser .inputGetValue (@ptrCast (element ));
1028+ const new_value = try std .mem .concat (self .arena , u8 , &.{ value , v });
1029+ try parser .inputSetValue (@ptrCast (element ), new_value );
1030+ }
1031+ },
1032+ .textarea = > {
1033+ const value = try parser .textareaGetValue (@ptrCast (node ));
1034+ const new_value = try std .mem .concat (self .arena , u8 , &.{ value , v });
1035+ try parser .textareaSetValue (@ptrCast (node ), new_value );
1036+ },
1037+ else = > {},
1038+ }
1039+ }
1040+
10161041 // We cannot navigate immediately as navigating will delete the DOM tree,
10171042 // which holds this event's node.
10181043 // As such we schedule the function to be called as soon as possible.
Original file line number Diff line number Diff line change @@ -23,11 +23,13 @@ pub fn processMessage(cmd: anytype) !void {
2323 const action = std .meta .stringToEnum (enum {
2424 dispatchKeyEvent ,
2525 dispatchMouseEvent ,
26+ insertText ,
2627 }, cmd .input .action ) orelse return error .UnknownMethod ;
2728
2829 switch (action ) {
2930 .dispatchKeyEvent = > return dispatchKeyEvent (cmd ),
3031 .dispatchMouseEvent = > return dispatchMouseEvent (cmd ),
32+ .insertText = > return insertText (cmd ),
3133 }
3234}
3335
@@ -115,6 +117,20 @@ fn dispatchMouseEvent(cmd: anytype) !void {
115117 // result already sent
116118}
117119
120+ // https://chromedevtools.github.io/devtools-protocol/tot/Input/#method-insertText
121+ fn insertText (cmd : anytype ) ! void {
122+ const params = (try cmd .params (struct {
123+ text : []const u8 , // The text to insert
124+ })) orelse return error .InvalidParams ;
125+
126+ const bc = cmd .browser_context orelse return ;
127+ const page = bc .session .currentPage () orelse return ;
128+
129+ try page .insertText (params .text );
130+
131+ try cmd .sendResult (null , .{});
132+ }
133+
118134fn clickNavigate (cmd : anytype , uri : std.Uri ) ! void {
119135 const bc = cmd .browser_context .? ;
120136
You can’t perform that action at this time.
0 commit comments