From eee232c12c20c4c6928da2e1154b02901d3b972b Mon Sep 17 00:00:00 2001 From: Pierre Tachoire Date: Tue, 3 Mar 2026 12:06:56 +0100 Subject: [PATCH] cdp: allow multiple calls to attachToTarget Playwright, when creating a new CDPSession, sends an attachToBrowserTarget followed by another attachToTarget to re-attach itself to the existing target. see playwright/axtree.js from demo/ repository. --- src/cdp/domains/target.zig | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/src/cdp/domains/target.zig b/src/cdp/domains/target.zig index 243ddc8f..41fdd18b 100644 --- a/src/cdp/domains/target.zig +++ b/src/cdp/domains/target.zig @@ -243,9 +243,7 @@ fn attachToTarget(cmd: anytype) !void { return error.UnknownTargetId; } - if (bc.session_id == null) { - try doAttachtoTarget(cmd, target_id); - } + try doAttachtoTarget(cmd, target_id); return cmd.sendResult( .{ .sessionId = bc.session_id }, @@ -451,22 +449,23 @@ fn setAutoAttach(cmd: anytype) !void { fn doAttachtoTarget(cmd: anytype, target_id: []const u8) !void { const bc = cmd.browser_context.?; - lp.assert(bc.session_id == null, "CDP.target.doAttachtoTarget not null session_id", .{}); - const session_id = cmd.cdp.session_id_gen.next(); + const session_id = bc.session_id orelse cmd.cdp.session_id_gen.next(); - // extra_headers should not be kept on a new page or tab, - // currently we have only 1 page, we clear it just in case - bc.extra_headers.clearRetainingCapacity(); + if (bc.session_id == null) { + // extra_headers should not be kept on a new page or tab, + // currently we have only 1 page, we clear it just in case + bc.extra_headers.clearRetainingCapacity(); + } try cmd.sendEvent("Target.attachedToTarget", AttachToTarget{ .sessionId = session_id, .targetInfo = TargetInfo{ .targetId = target_id, - .title = "about:blank", - .url = "chrome://newtab/", + .title = bc.getTitle() orelse "about:blank", + .url = bc.getURL() orelse "chrome://newtab/", .browserContextId = bc.id, }, - }, .{}); + }, .{ .session_id = bc.session_id }); bc.session_id = session_id; }