cdp: force traget creation on setAutoAttach

This commit is contained in:
Pierre Tachoire
2025-11-18 18:19:21 +01:00
parent c962858f61
commit 90b0346dd5

View File

@@ -166,13 +166,25 @@ fn createTarget(cmd: anytype) !void {
// if target_id is null, we should never have a session_id // if target_id is null, we should never have a session_id
std.debug.assert(bc.session_id == null); std.debug.assert(bc.session_id == null);
const target_id = cmd.cdp.target_id_gen.next(); const target_id = try doCreateTarget(cmd, params.url);
try cmd.sendResult(.{
.targetId = target_id,
}, .{});
}
fn doCreateTarget(cmd: anytype, url: []const u8) ![]const u8 {
const bc = cmd.browser_context orelse return error.BrowserContextNotLoaded;
const target_id = cmd.cdp.target_id_gen.next();
bc.target_id = target_id; bc.target_id = target_id;
var page = try bc.session.createPage(); var page = try bc.session.createPage();
{ {
const aux_data = try std.fmt.allocPrint(cmd.arena, "{{\"isDefault\":true,\"type\":\"default\",\"frameId\":\"{s}\"}}", .{target_id}); const aux_data = try std.fmt.allocPrint(
cmd.arena,
"{{\"isDefault\":true,\"type\":\"default\",\"frameId\":\"{s}\"}}",
.{target_id},
);
bc.inspector.contextCreated( bc.inspector.contextCreated(
page.js, page.js,
"", "",
@@ -205,15 +217,13 @@ fn createTarget(cmd: anytype) !void {
try doAttachtoTarget(cmd, target_id); try doAttachtoTarget(cmd, target_id);
} }
if (!std.mem.eql(u8, "about:blank", params.url)) { if (!std.mem.eql(u8, "about:blank", url)) {
try page.navigate(params.url, .{ try page.navigate(url, .{
.reason = .address_bar, .reason = .address_bar,
}); });
} }
try cmd.sendResult(.{ return target_id;
.targetId = target_id,
}, .{});
} }
fn attachToTarget(cmd: anytype) !void { fn attachToTarget(cmd: anytype) !void {
@@ -413,6 +423,13 @@ fn setAutoAttach(cmd: anytype) !void {
return; return;
} }
_ = cmd.createBrowserContext() catch |err| switch (err) {
error.AlreadyExists => unreachable,
else => return err,
};
_ = try doCreateTarget(cmd, "about:blank");
// This is a hack. Puppeteer, and probably others, expect the Browser to // This is a hack. Puppeteer, and probably others, expect the Browser to
// automatically started creating targets. Things like an empty tab, or // automatically started creating targets. Things like an empty tab, or
// a blank page. And they block until this happens. So we send an event // a blank page. And they block until this happens. So we send an event
@@ -421,16 +438,16 @@ fn setAutoAttach(cmd: anytype) !void {
// there. // there.
// This hack requires the main cdp dispatch handler to special case // This hack requires the main cdp dispatch handler to special case
// messages from this "STARTUP" session. // messages from this "STARTUP" session.
try cmd.sendEvent("Target.attachedToTarget", AttachToTarget{ // try cmd.sendEvent("Target.attachedToTarget", AttachToTarget{
.sessionId = "STARTUP", // .sessionId = "STARTUP",
.targetInfo = TargetInfo{ // .targetInfo = TargetInfo{
.type = "page", // .type = "page",
.targetId = "TID-STARTUP-P", // .targetId = "TID-STARTUP-P",
.title = "New Private Tab", // .title = "New Private Tab",
.url = "chrome://newtab/", // .url = "chrome://newtab/",
.browserContextId = "BID-STARTUP", // .browserContextId = "BID-STARTUP",
}, // },
}, .{}); // }, .{});
} }
fn doAttachtoTarget(cmd: anytype, target_id: []const u8) !void { fn doAttachtoTarget(cmd: anytype, target_id: []const u8) !void {