mirror of
https://github.com/lightpanda-io/browser.git
synced 2026-03-22 20:54:43 +00:00
add LP.getInteractiveElements CDP command
Returns a structured list of all interactive elements on a page: buttons, links, inputs, ARIA widgets, contenteditable regions, and elements with event listeners. Includes accessible names, roles, listener types, and key attributes. Event listener introspection (both addEventListener and inline handlers) is unique to LP — no other browser exposes this to automation code.
This commit is contained in:
@@ -19,15 +19,18 @@
|
||||
const std = @import("std");
|
||||
const lp = @import("lightpanda");
|
||||
const markdown = lp.markdown;
|
||||
const interactive = lp.interactive;
|
||||
const Node = @import("../Node.zig");
|
||||
|
||||
pub fn processMessage(cmd: anytype) !void {
|
||||
const action = std.meta.stringToEnum(enum {
|
||||
getMarkdown,
|
||||
getInteractiveElements,
|
||||
}, cmd.input.action) orelse return error.UnknownMethod;
|
||||
|
||||
switch (action) {
|
||||
.getMarkdown => return getMarkdown(cmd),
|
||||
.getInteractiveElements => return getInteractiveElements(cmd),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -54,6 +57,35 @@ fn getMarkdown(cmd: anytype) !void {
|
||||
}, .{});
|
||||
}
|
||||
|
||||
fn getInteractiveElements(cmd: anytype) !void {
|
||||
const Params = struct {
|
||||
nodeId: ?Node.Id = null,
|
||||
};
|
||||
const params = (try cmd.params(Params)) orelse Params{};
|
||||
|
||||
const bc = cmd.browser_context orelse return error.NoBrowserContext;
|
||||
const page = bc.session.currentPage() orelse return error.PageNotLoaded;
|
||||
|
||||
const root = if (params.nodeId) |nodeId|
|
||||
(bc.node_registry.lookup_by_id.get(nodeId) orelse return error.InvalidNodeId).dom
|
||||
else
|
||||
page.document.asNode();
|
||||
|
||||
const elements = try interactive.collectInteractiveElements(root, page, cmd.arena);
|
||||
|
||||
// Register nodes so nodeIds are valid for subsequent CDP calls.
|
||||
var node_ids: std.ArrayList(Node.Id) = try .initCapacity(cmd.arena, elements.len);
|
||||
for (elements) |el| {
|
||||
const registered = try bc.node_registry.register(el.node);
|
||||
node_ids.appendAssumeCapacity(registered.id);
|
||||
}
|
||||
|
||||
return cmd.sendResult(.{
|
||||
.elements = elements,
|
||||
.nodeIds = node_ids.items,
|
||||
}, .{});
|
||||
}
|
||||
|
||||
const testing = @import("../testing.zig");
|
||||
test "cdp.lp: getMarkdown" {
|
||||
var ctx = testing.context();
|
||||
@@ -70,3 +102,20 @@ test "cdp.lp: getMarkdown" {
|
||||
const result = ctx.client.?.sent.items[0].object.get("result").?.object;
|
||||
try testing.expect(result.get("markdown") != null);
|
||||
}
|
||||
|
||||
test "cdp.lp: getInteractiveElements" {
|
||||
var ctx = testing.context();
|
||||
defer ctx.deinit();
|
||||
|
||||
const bc = try ctx.loadBrowserContext(.{});
|
||||
_ = try bc.session.createPage();
|
||||
|
||||
try ctx.processMessage(.{
|
||||
.id = 1,
|
||||
.method = "LP.getInteractiveElements",
|
||||
});
|
||||
|
||||
const result = ctx.client.?.sent.items[0].object.get("result").?.object;
|
||||
try testing.expect(result.get("elements") != null);
|
||||
try testing.expect(result.get("nodeIds") != null);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user