Skip to content

Commit 90b0346

Browse files
committed
cdp: force traget creation on setAutoAttach
1 parent c962858 commit 90b0346

File tree

1 file changed

+34
-17
lines changed

1 file changed

+34
-17
lines changed

src/cdp/domains/target.zig

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -166,13 +166,25 @@ fn createTarget(cmd: anytype) !void {
166166
// if target_id is null, we should never have a session_id
167167
std.debug.assert(bc.session_id == null);
168168

169-
const target_id = cmd.cdp.target_id_gen.next();
169+
const target_id = try doCreateTarget(cmd, params.url);
170+
171+
try cmd.sendResult(.{
172+
.targetId = target_id,
173+
}, .{});
174+
}
170175

176+
fn doCreateTarget(cmd: anytype, url: []const u8) ![]const u8 {
177+
const bc = cmd.browser_context orelse return error.BrowserContextNotLoaded;
178+
const target_id = cmd.cdp.target_id_gen.next();
171179
bc.target_id = target_id;
172180

173181
var page = try bc.session.createPage();
174182
{
175-
const aux_data = try std.fmt.allocPrint(cmd.arena, "{{\"isDefault\":true,\"type\":\"default\",\"frameId\":\"{s}\"}}", .{target_id});
183+
const aux_data = try std.fmt.allocPrint(
184+
cmd.arena,
185+
"{{\"isDefault\":true,\"type\":\"default\",\"frameId\":\"{s}\"}}",
186+
.{target_id},
187+
);
176188
bc.inspector.contextCreated(
177189
page.js,
178190
"",
@@ -205,15 +217,13 @@ fn createTarget(cmd: anytype) !void {
205217
try doAttachtoTarget(cmd, target_id);
206218
}
207219

208-
if (!std.mem.eql(u8, "about:blank", params.url)) {
209-
try page.navigate(params.url, .{
220+
if (!std.mem.eql(u8, "about:blank", url)) {
221+
try page.navigate(url, .{
210222
.reason = .address_bar,
211223
});
212224
}
213225

214-
try cmd.sendResult(.{
215-
.targetId = target_id,
216-
}, .{});
226+
return target_id;
217227
}
218228

219229
fn attachToTarget(cmd: anytype) !void {
@@ -413,6 +423,13 @@ fn setAutoAttach(cmd: anytype) !void {
413423
return;
414424
}
415425

426+
_ = cmd.createBrowserContext() catch |err| switch (err) {
427+
error.AlreadyExists => unreachable,
428+
else => return err,
429+
};
430+
431+
_ = try doCreateTarget(cmd, "about:blank");
432+
416433
// This is a hack. Puppeteer, and probably others, expect the Browser to
417434
// automatically started creating targets. Things like an empty tab, or
418435
// a blank page. And they block until this happens. So we send an event
@@ -421,16 +438,16 @@ fn setAutoAttach(cmd: anytype) !void {
421438
// there.
422439
// This hack requires the main cdp dispatch handler to special case
423440
// messages from this "STARTUP" session.
424-
try cmd.sendEvent("Target.attachedToTarget", AttachToTarget{
425-
.sessionId = "STARTUP",
426-
.targetInfo = TargetInfo{
427-
.type = "page",
428-
.targetId = "TID-STARTUP-P",
429-
.title = "New Private Tab",
430-
.url = "chrome://newtab/",
431-
.browserContextId = "BID-STARTUP",
432-
},
433-
}, .{});
441+
// try cmd.sendEvent("Target.attachedToTarget", AttachToTarget{
442+
// .sessionId = "STARTUP",
443+
// .targetInfo = TargetInfo{
444+
// .type = "page",
445+
// .targetId = "TID-STARTUP-P",
446+
// .title = "New Private Tab",
447+
// .url = "chrome://newtab/",
448+
// .browserContextId = "BID-STARTUP",
449+
// },
450+
// }, .{});
434451
}
435452

436453
fn doAttachtoTarget(cmd: anytype, target_id: []const u8) !void {

0 commit comments

Comments
 (0)