CDP/MCP: add highly compressed text format for semantic tree

This commit is contained in:
Adrià Arrufat
2026-03-08 22:41:51 +09:00
parent b8139a6e83
commit b674c2e448
3 changed files with 138 additions and 9 deletions

View File

@@ -37,17 +37,36 @@ pub fn processMessage(cmd: anytype) !void {
}
fn getSemanticTree(cmd: anytype) !void {
const Params = struct {
format: ?[]const u8 = 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 dom_node = page.document.asNode();
const st = SemanticTree{
.dom_node = dom_node,
.registry = &bc.node_registry,
.page = page,
.arena = cmd.arena,
};
if (params.format) |format| {
if (std.mem.eql(u8, format, "text")) {
var aw: std.Io.Writer.Allocating = .init(cmd.arena);
defer aw.deinit();
try st.textStringify(&aw.writer);
return cmd.sendResult(.{
.semanticTree = aw.written(),
}, .{});
}
}
return cmd.sendResult(.{
.semanticTree = SemanticTree{
.dom_node = dom_node,
.registry = &bc.node_registry,
.page = page,
.arena = cmd.arena,
},
.semanticTree = st,
}, .{});
}