Skip to content

Commit 9974b56

Browse files
Add Target.createTarget
Signed-off-by: Francis Bouvier <[email protected]>
1 parent 0506a7b commit 9974b56

File tree

1 file changed

+85
-15
lines changed

1 file changed

+85
-15
lines changed

src/cdp/target.zig

Lines changed: 85 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const TargetMethods = enum {
1111
setAutoAttach,
1212
getTargetInfo,
1313
createBrowserContext,
14+
createTarget,
1415
};
1516

1617
pub fn target(
@@ -26,13 +27,28 @@ pub fn target(
2627
.setAutoAttach => tagetSetAutoAttach(alloc, id, scanner, ctx),
2728
.getTargetInfo => tagetGetTargetInfo(alloc, id, scanner, ctx),
2829
.createBrowserContext => createBrowserContext(alloc, id, scanner, ctx),
30+
.createTarget => createTarget(alloc, id, scanner, ctx),
2931
};
3032
}
3133

3234
const PageTargetID = "CFCD6EC01573CF29BB638E9DC0F52DDC";
3335
const BrowserTargetID = "2d2bdef9-1c95-416f-8c0e-83f3ab73a30c";
3436
const BrowserContextID = "65618675CB7D3585A95049E9DFE95EA9";
3537

38+
const AttachToTarget = struct {
39+
sessionId: []const u8,
40+
targetInfo: struct {
41+
targetId: []const u8,
42+
type: []const u8 = "page",
43+
title: []const u8,
44+
url: []const u8,
45+
attached: bool = true,
46+
canAccessOpener: bool = false,
47+
browserContextId: []const u8,
48+
},
49+
waitingForDebugger: bool = false,
50+
};
51+
3652
const TargetFilter = struct {
3753
type: []const u8,
3854
exclude: bool,
@@ -56,22 +72,24 @@ fn tagetSetAutoAttach(
5672
const sessionID = try cdp.getSessionID(scanner);
5773

5874
if (sessionID == null) {
59-
const AttachToTarget = struct {
60-
sessionId: []const u8 = cdp.SessionID,
61-
targetInfo: struct {
62-
targetId: []const u8 = PageTargetID,
63-
type: []const u8 = "page",
64-
title: []const u8 = "New Incognito tab",
65-
url: []const u8 = cdp.URLBase,
66-
attached: bool = true,
67-
canAccessOpener: bool = false,
68-
browserContextId: []const u8 = BrowserContextID,
69-
} = .{},
70-
waitingForDebugger: bool = false,
75+
const attached = AttachToTarget{
76+
.sessionId = cdp.SessionID,
77+
.targetInfo = .{
78+
.targetId = PageTargetID,
79+
.title = "New Incognito tab",
80+
.url = cdp.URLBase,
81+
.browserContextId = BrowserContextID,
82+
},
7183
};
72-
const attached = try cdp.method(alloc, "Target.attachedToTarget", AttachToTarget, .{}, null);
73-
std.log.debug("res {s}", .{attached});
74-
try server.sendSync(ctx, attached);
84+
const event = try cdp.method(
85+
alloc,
86+
"Target.attachedToTarget",
87+
AttachToTarget,
88+
attached,
89+
null,
90+
);
91+
std.log.debug("event {s}", .{event});
92+
try server.sendSync(ctx, event);
7593
}
7694

7795
return result(alloc, id, null, null, sessionID);
@@ -111,6 +129,7 @@ fn tagetGetTargetInfo(
111129
}
112130

113131
const ContextID = "22648B09EDCCDD11109E2D4FEFBE4F89";
132+
const ContextSessionID = "4FDC2CB760A23A220497A05C95417CF4";
114133

115134
fn createBrowserContext(
116135
alloc: std.mem.Allocator,
@@ -135,3 +154,54 @@ fn createBrowserContext(
135154
};
136155
return result(alloc, id, Resp, Resp{}, sessionID);
137156
}
157+
158+
const TargetID = "57356548460A8F29706A2ADF14316298";
159+
160+
fn createTarget(
161+
alloc: std.mem.Allocator,
162+
id: u64,
163+
scanner: *std.json.Scanner,
164+
ctx: *Ctx,
165+
) ![]const u8 {
166+
167+
// input
168+
const Params = struct {
169+
url: []const u8,
170+
width: ?u64 = null,
171+
height: ?u64 = null,
172+
browserContextId: []const u8,
173+
enableBeginFrameControl: bool = false,
174+
newWindow: bool = false,
175+
background: bool = false,
176+
forTab: ?bool = null,
177+
};
178+
_ = try getParams(alloc, Params, scanner);
179+
const sessionID = try cdp.getSessionID(scanner);
180+
181+
// send attachToTarget event
182+
const attached = AttachToTarget{
183+
.sessionId = ContextSessionID,
184+
.targetInfo = .{
185+
.targetId = TargetID,
186+
.title = "",
187+
.url = "about:blank",
188+
.browserContextId = ContextID,
189+
},
190+
.waitingForDebugger = true,
191+
};
192+
const event = try cdp.method(
193+
alloc,
194+
"Target.attachedToTarget",
195+
AttachToTarget,
196+
attached,
197+
sessionID,
198+
);
199+
std.log.debug("event {s}", .{event});
200+
try server.sendSync(ctx, event);
201+
202+
// output
203+
const Resp = struct {
204+
targetId: []const u8 = TargetID,
205+
};
206+
return result(alloc, id, Resp, Resp{}, sessionID);
207+
}

0 commit comments

Comments
 (0)