mirror of
https://github.com/lightpanda-io/browser.git
synced 2026-03-22 04:34:44 +00:00
add LP.getStructuredData CDP command
This commit is contained in:
@@ -19,15 +19,18 @@
|
||||
const std = @import("std");
|
||||
const lp = @import("lightpanda");
|
||||
const markdown = lp.markdown;
|
||||
const structured_data = lp.structured_data;
|
||||
const Node = @import("../Node.zig");
|
||||
|
||||
pub fn processMessage(cmd: anytype) !void {
|
||||
const action = std.meta.stringToEnum(enum {
|
||||
getMarkdown,
|
||||
getStructuredData,
|
||||
}, cmd.input.action) orelse return error.UnknownMethod;
|
||||
|
||||
switch (action) {
|
||||
.getMarkdown => return getMarkdown(cmd),
|
||||
.getStructuredData => return getStructuredData(cmd),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -54,6 +57,21 @@ fn getMarkdown(cmd: anytype) !void {
|
||||
}, .{});
|
||||
}
|
||||
|
||||
fn getStructuredData(cmd: anytype) !void {
|
||||
const bc = cmd.browser_context orelse return error.NoBrowserContext;
|
||||
const page = bc.session.currentPage() orelse return error.PageNotLoaded;
|
||||
|
||||
const data = try structured_data.collectStructuredData(
|
||||
page.document.asNode(),
|
||||
cmd.arena,
|
||||
page,
|
||||
);
|
||||
|
||||
return cmd.sendResult(.{
|
||||
.structuredData = data,
|
||||
}, .{});
|
||||
}
|
||||
|
||||
const testing = @import("../testing.zig");
|
||||
test "cdp.lp: getMarkdown" {
|
||||
var ctx = testing.context();
|
||||
@@ -70,3 +88,19 @@ 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: getStructuredData" {
|
||||
var ctx = testing.context();
|
||||
defer ctx.deinit();
|
||||
|
||||
const bc = try ctx.loadBrowserContext(.{});
|
||||
_ = try bc.session.createPage();
|
||||
|
||||
try ctx.processMessage(.{
|
||||
.id = 1,
|
||||
.method = "LP.getStructuredData",
|
||||
});
|
||||
|
||||
const result = ctx.client.?.sent.items[0].object.get("result").?.object;
|
||||
try testing.expect(result.get("structuredData") != null);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user