Merge pull request #1312 from lightpanda-io/stagehand-zigdom

Stagehand zigdom
This commit is contained in:
Karl Seguin
2026-01-16 23:10:19 +00:00
committed by GitHub
2 changed files with 25 additions and 10 deletions

View File

@@ -152,7 +152,7 @@ pub fn CDPT(comptime TypeProvider: type) type {
} }
if (is_startup) { if (is_startup) {
dispatchStartupCommand(&command) catch |err| { dispatchStartupCommand(&command, input.method) catch |err| {
command.sendError(-31999, @errorName(err), .{}) catch {}; command.sendError(-31999, @errorName(err), .{}) catch {};
return err; return err;
}; };
@@ -174,7 +174,23 @@ pub fn CDPT(comptime TypeProvider: type) type {
// "special" handling - the bare minimum we need to do until the driver // "special" handling - the bare minimum we need to do until the driver
// switches to a real BrowserContext. // switches to a real BrowserContext.
// (I can imagine this logic will become driver-specific) // (I can imagine this logic will become driver-specific)
fn dispatchStartupCommand(command: anytype) !void { fn dispatchStartupCommand(command: anytype, method: []const u8) !void {
// Stagehand parses the response and error if we don't return a
// correct one for this call.
if (std.mem.eql(u8, method, "Page.getFrameTree")) {
return command.sendResult(.{
.frameTree = .{
.frame = .{
.id = "TID-STARTUP-B",
.loaderId = LOADER_ID,
.securityOrigin = URL_BASE,
.url = "about:blank",
.secureContextType = "Secure",
},
},
}, .{});
}
return command.sendResult(null, .{}); return command.sendResult(null, .{});
} }

View File

@@ -36,6 +36,7 @@ pub fn processMessage(cmd: anytype) !void {
sendMessageToTarget, sendMessageToTarget,
setAutoAttach, setAutoAttach,
setDiscoverTargets, setDiscoverTargets,
activateTarget,
}, cmd.input.action) orelse return error.UnknownMethod; }, cmd.input.action) orelse return error.UnknownMethod;
switch (action) { switch (action) {
@@ -51,14 +52,16 @@ pub fn processMessage(cmd: anytype) !void {
.sendMessageToTarget => return sendMessageToTarget(cmd), .sendMessageToTarget => return sendMessageToTarget(cmd),
.setAutoAttach => return setAutoAttach(cmd), .setAutoAttach => return setAutoAttach(cmd),
.setDiscoverTargets => return setDiscoverTargets(cmd), .setDiscoverTargets => return setDiscoverTargets(cmd),
.activateTarget => return cmd.sendResult(null, .{}),
} }
} }
fn getTargets(cmd: anytype) !void { fn getTargets(cmd: anytype) !void {
// Some clients like Stagehand expects to have an existing context. // If no context available, return an empty array.
const bc = cmd.browser_context orelse cmd.createBrowserContext() catch |err| switch (err) { const bc = cmd.browser_context orelse {
error.AlreadyExists => unreachable, return cmd.sendResult(.{
else => return err, .targetInfos = [_]TargetInfo{},
}, .{ .include_session_id = false });
}; };
const target_id = bc.target_id orelse { const target_id = bc.target_id orelse {
@@ -230,10 +233,6 @@ fn attachToTarget(cmd: anytype) !void {
return error.UnknownTargetId; return error.UnknownTargetId;
} }
if (bc.session_id != null) {
return error.SessionAlreadyLoaded;
}
if (bc.session_id == null) { if (bc.session_id == null) {
try doAttachtoTarget(cmd, target_id); try doAttachtoTarget(cmd, target_id);
} }