mirror of
https://github.com/lightpanda-io/browser.git
synced 2026-03-22 20:54:43 +00:00
Merge branch 'main' into semantic-tree
This commit is contained in:
@@ -21,6 +21,7 @@ const lp = @import("lightpanda");
|
||||
const log = @import("../../log.zig");
|
||||
const markdown = lp.markdown;
|
||||
const SemanticTree = lp.SemanticTree;
|
||||
const interactive = lp.interactive;
|
||||
const Node = @import("../Node.zig");
|
||||
const DOMNode = @import("../../browser/webapi/Node.zig");
|
||||
|
||||
@@ -28,11 +29,13 @@ pub fn processMessage(cmd: anytype) !void {
|
||||
const action = std.meta.stringToEnum(enum {
|
||||
getMarkdown,
|
||||
getSemanticTree,
|
||||
getInteractiveElements,
|
||||
}, cmd.input.action) orelse return error.UnknownMethod;
|
||||
|
||||
switch (action) {
|
||||
.getMarkdown => return getMarkdown(cmd),
|
||||
.getSemanticTree => return getSemanticTree(cmd),
|
||||
.getInteractiveElements => return getInteractiveElements(cmd),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -96,6 +99,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, cmd.arena, page);
|
||||
|
||||
// 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();
|
||||
@@ -112,3 +144,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);
|
||||
}
|
||||
|
||||
@@ -19,6 +19,8 @@
|
||||
const std = @import("std");
|
||||
const lp = @import("lightpanda");
|
||||
|
||||
const screenshot_png = @embedFile("screenshot.png");
|
||||
|
||||
const id = @import("../id.zig");
|
||||
const log = @import("../../log.zig");
|
||||
const js = @import("../../browser/js/js.zig");
|
||||
@@ -39,6 +41,8 @@ pub fn processMessage(cmd: anytype) !void {
|
||||
navigate,
|
||||
stopLoading,
|
||||
close,
|
||||
captureScreenshot,
|
||||
getLayoutMetrics,
|
||||
}, cmd.input.action) orelse return error.UnknownMethod;
|
||||
|
||||
switch (action) {
|
||||
@@ -50,6 +54,8 @@ pub fn processMessage(cmd: anytype) !void {
|
||||
.navigate => return navigate(cmd),
|
||||
.stopLoading => return cmd.sendResult(null, .{}),
|
||||
.close => return close(cmd),
|
||||
.captureScreenshot => return captureScreenshot(cmd),
|
||||
.getLayoutMetrics => return getLayoutMetrics(cmd),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -514,6 +520,109 @@ const LifecycleEvent = struct {
|
||||
timestamp: u64,
|
||||
};
|
||||
|
||||
const Viewport = struct {
|
||||
x: f64,
|
||||
y: f64,
|
||||
width: f64,
|
||||
height: f64,
|
||||
scale: f64,
|
||||
};
|
||||
|
||||
fn base64Encode(comptime input: []const u8) [std.base64.standard.Encoder.calcSize(input.len)]u8 {
|
||||
const encoder = std.base64.standard.Encoder;
|
||||
var buf: [encoder.calcSize(input.len)]u8 = undefined;
|
||||
_ = encoder.encode(&buf, input);
|
||||
return buf;
|
||||
}
|
||||
|
||||
fn captureScreenshot(cmd: anytype) !void {
|
||||
const Params = struct {
|
||||
format: ?[]const u8 = "png",
|
||||
quality: ?u8 = null,
|
||||
clip: ?Viewport = null,
|
||||
fromSurface: ?bool = false,
|
||||
captureBeyondViewport: ?bool = false,
|
||||
optimizeForSpeed: ?bool = false,
|
||||
};
|
||||
const params = try cmd.params(Params) orelse Params{};
|
||||
|
||||
const format = params.format orelse "png";
|
||||
|
||||
if (!std.mem.eql(u8, format, "png")) {
|
||||
log.warn(.not_implemented, "Page.captureScreenshot params", .{ .format = format });
|
||||
return cmd.sendError(-32000, "unsupported screenshot format.", .{});
|
||||
}
|
||||
if (params.quality != null) {
|
||||
log.warn(.not_implemented, "Page.captureScreenshot params", .{ .quality = params.quality });
|
||||
}
|
||||
if (params.clip != null) {
|
||||
log.warn(.not_implemented, "Page.captureScreenshot params", .{ .clip = params.clip });
|
||||
}
|
||||
if (params.fromSurface orelse false or params.captureBeyondViewport orelse false or params.optimizeForSpeed orelse false) {
|
||||
log.warn(.not_implemented, "Page.captureScreenshot params", .{
|
||||
.fromSurface = params.fromSurface,
|
||||
.captureBeyondViewport = params.captureBeyondViewport,
|
||||
.optimizeForSpeed = params.optimizeForSpeed,
|
||||
});
|
||||
}
|
||||
|
||||
return cmd.sendResult(.{
|
||||
.data = base64Encode(screenshot_png),
|
||||
}, .{});
|
||||
}
|
||||
|
||||
fn getLayoutMetrics(cmd: anytype) !void {
|
||||
const width = 1920;
|
||||
const height = 1080;
|
||||
|
||||
return cmd.sendResult(.{
|
||||
.layoutViewport = .{
|
||||
.pageX = 0,
|
||||
.pageY = 0,
|
||||
.clientWidth = width,
|
||||
.clientHeight = height,
|
||||
},
|
||||
.visualViewport = .{
|
||||
.offsetX = 0,
|
||||
.offsetY = 0,
|
||||
.pageX = 0,
|
||||
.pageY = 0,
|
||||
.clientWidth = width,
|
||||
.clientHeight = height,
|
||||
.scale = 1,
|
||||
.zoom = 1,
|
||||
},
|
||||
.contentSize = .{
|
||||
.x = 0,
|
||||
.y = 0,
|
||||
.width = width,
|
||||
.height = height,
|
||||
},
|
||||
.cssLayoutViewport = .{
|
||||
.pageX = 0,
|
||||
.pageY = 0,
|
||||
.clientWidth = width,
|
||||
.clientHeight = height,
|
||||
},
|
||||
.cssVisualViewport = .{
|
||||
.offsetX = 0,
|
||||
.offsetY = 0,
|
||||
.pageX = 0,
|
||||
.pageY = 0,
|
||||
.clientWidth = width,
|
||||
.clientHeight = height,
|
||||
.scale = 1,
|
||||
.zoom = 1,
|
||||
},
|
||||
.cssContentSize = .{
|
||||
.x = 0,
|
||||
.y = 0,
|
||||
.width = width,
|
||||
.height = height,
|
||||
},
|
||||
}, .{});
|
||||
}
|
||||
|
||||
const testing = @import("../testing.zig");
|
||||
test "cdp.page: getFrameTree" {
|
||||
var ctx = testing.context();
|
||||
@@ -547,3 +656,77 @@ test "cdp.page: getFrameTree" {
|
||||
}, .{ .id = 11 });
|
||||
}
|
||||
}
|
||||
|
||||
test "cdp.page: captureScreenshot" {
|
||||
var ctx = testing.context();
|
||||
defer ctx.deinit();
|
||||
{
|
||||
try ctx.processMessage(.{ .id = 10, .method = "Page.captureScreenshot", .params = .{ .format = "jpg" } });
|
||||
try ctx.expectSentError(-32000, "unsupported screenshot format.", .{ .id = 10 });
|
||||
}
|
||||
|
||||
{
|
||||
try ctx.processMessage(.{ .id = 11, .method = "Page.captureScreenshot" });
|
||||
try ctx.expectSentResult(.{
|
||||
.data = base64Encode(screenshot_png),
|
||||
}, .{ .id = 11 });
|
||||
}
|
||||
}
|
||||
|
||||
test "cdp.page: getLayoutMetrics" {
|
||||
var ctx = testing.context();
|
||||
defer ctx.deinit();
|
||||
|
||||
_ = try ctx.loadBrowserContext(.{ .id = "BID-9", .url = "hi.html", .target_id = "FID-000000000X".* });
|
||||
|
||||
const width = 1920;
|
||||
const height = 1080;
|
||||
|
||||
try ctx.processMessage(.{ .id = 12, .method = "Page.getLayoutMetrics" });
|
||||
try ctx.expectSentResult(.{
|
||||
.layoutViewport = .{
|
||||
.pageX = 0,
|
||||
.pageY = 0,
|
||||
.clientWidth = width,
|
||||
.clientHeight = height,
|
||||
},
|
||||
.visualViewport = .{
|
||||
.offsetX = 0,
|
||||
.offsetY = 0,
|
||||
.pageX = 0,
|
||||
.pageY = 0,
|
||||
.clientWidth = width,
|
||||
.clientHeight = height,
|
||||
.scale = 1,
|
||||
.zoom = 1,
|
||||
},
|
||||
.contentSize = .{
|
||||
.x = 0,
|
||||
.y = 0,
|
||||
.width = width,
|
||||
.height = height,
|
||||
},
|
||||
.cssLayoutViewport = .{
|
||||
.pageX = 0,
|
||||
.pageY = 0,
|
||||
.clientWidth = width,
|
||||
.clientHeight = height,
|
||||
},
|
||||
.cssVisualViewport = .{
|
||||
.offsetX = 0,
|
||||
.offsetY = 0,
|
||||
.pageX = 0,
|
||||
.pageY = 0,
|
||||
.clientWidth = width,
|
||||
.clientHeight = height,
|
||||
.scale = 1,
|
||||
.zoom = 1,
|
||||
},
|
||||
.cssContentSize = .{
|
||||
.x = 0,
|
||||
.y = 0,
|
||||
.width = width,
|
||||
.height = height,
|
||||
},
|
||||
}, .{ .id = 12 });
|
||||
}
|
||||
|
||||
BIN
src/cdp/domains/screenshot.png
Normal file
BIN
src/cdp/domains/screenshot.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 14 KiB |
Reference in New Issue
Block a user