Skip to content

Commit 9e13ffb

Browse files
Add sendEvent utility function
Signed-off-by: Francis Bouvier <[email protected]>
1 parent ed38705 commit 9e13ffb

File tree

4 files changed

+67
-23
lines changed

4 files changed

+67
-23
lines changed

src/cdp/cdp.zig

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,22 +129,24 @@ pub fn result(
129129
return stringify(alloc, resp);
130130
}
131131

132-
// caller owns the slice returned
133-
pub fn method(
132+
pub fn sendEvent(
134133
alloc: std.mem.Allocator,
134+
ctx: *Ctx,
135135
name: []const u8,
136136
comptime T: type,
137137
params: T,
138138
sessionID: ?[]const u8,
139-
) ![]const u8 {
139+
) !void {
140140
const Resp = struct {
141141
method: []const u8,
142142
params: T,
143143
sessionId: ?[]const u8,
144144
};
145145
const resp = Resp{ .method = name, .params = params, .sessionId = sessionID };
146146

147-
return stringify(alloc, resp);
147+
const event_msg = try stringify(alloc, resp);
148+
std.log.debug("event {s}", .{event_msg});
149+
try server.sendSync(ctx, event_msg);
148150
}
149151

150152
pub fn getParams(

src/cdp/page.zig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,8 @@ fn navigate(
168168
ctx.state.url = content.params.url;
169169
ctx.state.loaderID = "AF8667A203C5392DBE9AC290044AA4C2";
170170

171+
var page = try ctx.browser.currentSession().createPage();
172+
171173
// output
172174
const Resp = struct {
173175
frameId: []const u8,

src/cdp/runtime.zig

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,68 @@ fn enable(
3131
alloc: std.mem.Allocator,
3232
id: u64,
3333
scanner: *std.json.Scanner,
34-
_: *Ctx,
34+
ctx: *Ctx,
3535
) ![]const u8 {
36+
_ = ctx;
37+
38+
// input
3639
const sessionID = try cdp.getSessionID(scanner);
40+
41+
// output
42+
// const uniqueID = "1367118932354479079.-1471398151593995849";
43+
// const mainCtx = try executionContextCreated(
44+
// alloc,
45+
// 1,
46+
// cdp.URLBase,
47+
// "",
48+
// uniqueID,
49+
// .{},
50+
// sessionID,
51+
// );
52+
// std.log.debug("res {s}", .{mainCtx});
53+
// try server.sendAsync(ctx, mainCtx);
54+
3755
return result(alloc, id, null, null, sessionID);
3856
}
3957

58+
const AuxData = struct {
59+
isDefault: bool = true,
60+
type: []const u8 = "default",
61+
frameId: []const u8 = cdp.FrameID,
62+
};
63+
64+
const ExecutionContextDescription = struct {
65+
id: u64,
66+
origin: []const u8,
67+
name: []const u8,
68+
uniqueId: []const u8,
69+
auxData: ?AuxData = null,
70+
};
71+
72+
fn executionContextCreated(
73+
alloc: std.mem.Allocator,
74+
id: u64,
75+
origin: []const u8,
76+
name: []const u8,
77+
uniqueID: []const u8,
78+
auxData: ?AuxData,
79+
sessionID: ?[]const u8,
80+
) ![]const u8 {
81+
const Params = struct {
82+
context: ExecutionContextDescription,
83+
};
84+
const params = Params{
85+
.context = .{
86+
.id = id,
87+
.origin = origin,
88+
.name = name,
89+
.uniqueId = uniqueID,
90+
.auxData = auxData,
91+
},
92+
};
93+
return try cdp.method(alloc, "Runtime.executionContextCreated", Params, params, sessionID);
94+
}
95+
4096
fn runIfWaitingForDebugger(
4197
alloc: std.mem.Allocator,
4298
id: u64,

src/cdp/target.zig

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -81,15 +81,7 @@ fn tagetSetAutoAttach(
8181
.browserContextId = BrowserContextID,
8282
},
8383
};
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);
84+
try cdp.sendEvent(alloc, ctx, "Target.attachedToTarget", AttachToTarget, attached, null);
9385
}
9486

9587
return result(alloc, id, null, null, sessionID);
@@ -196,15 +188,7 @@ fn createTarget(
196188
},
197189
.waitingForDebugger = true,
198190
};
199-
const event = try cdp.method(
200-
alloc,
201-
"Target.attachedToTarget",
202-
AttachToTarget,
203-
attached,
204-
sessionID,
205-
);
206-
std.log.debug("event {s}", .{event});
207-
try server.sendSync(ctx, event);
191+
try cdp.sendEvent(alloc, ctx, "Target.attachedToTarget", AttachToTarget, attached, sessionID);
208192

209193
// output
210194
const Resp = struct {

0 commit comments

Comments
 (0)