Skip to content

Commit 4a31dd8

Browse files
Let Page.navigate do actually navigation
Signed-off-by: Francis Bouvier <[email protected]>
1 parent 1b1b7cd commit 4a31dd8

File tree

3 files changed

+106
-21
lines changed

3 files changed

+106
-21
lines changed

src/cdp/cdp.zig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,3 +262,7 @@ pub const SessionID = "9559320D92474062597D9875C664CAC0";
262262
pub const URLBase = "chrome://newtab/";
263263
pub const FrameID = "90D14BBD8AED408A0467AC93100BCDBE";
264264
pub const LoaderID = "CFC8BED824DD2FD56CF1EF33C965C79C";
265+
266+
pub const TimestampEvent = struct {
267+
timestamp: f64,
268+
};

src/cdp/page.zig

Lines changed: 97 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const cdp = @import("cdp.zig");
66
const result = cdp.result;
77
const getParams = cdp.getParams;
88
const stringify = cdp.stringify;
9+
const sendEvent = cdp.sendEvent;
910

1011
const PageMethods = enum {
1112
enable,
@@ -45,6 +46,21 @@ fn enable(
4546
return result(alloc, id, null, null, sessionID);
4647
}
4748

49+
const Frame = struct {
50+
id: []const u8,
51+
loaderId: []const u8,
52+
url: []const u8,
53+
domainAndRegistry: []const u8 = "",
54+
securityOrigin: []const u8,
55+
mimeType: []const u8 = "text/html",
56+
adFrameStatus: struct {
57+
adFrameType: []const u8 = "none",
58+
} = .{},
59+
secureContextType: []const u8,
60+
crossOriginIsolatedContextType: []const u8 = "NotIsolated",
61+
gatedAPIFeatures: [][]const u8 = &[0][]const u8{},
62+
};
63+
4864
fn getFrameTree(
4965
alloc: std.mem.Allocator,
5066
id: u64,
@@ -54,20 +70,7 @@ fn getFrameTree(
5470
const sessionID = try cdp.getSessionID(scanner);
5571
const FrameTree = struct {
5672
frameTree: struct {
57-
frame: struct {
58-
id: []const u8,
59-
loaderId: []const u8,
60-
url: []const u8,
61-
domainAndRegistry: []const u8 = "",
62-
securityOrigin: []const u8,
63-
mimeType: []const u8 = "text/html",
64-
adFrameStatus: struct {
65-
adFrameType: []const u8 = "none",
66-
} = .{},
67-
secureContextType: []const u8,
68-
crossOriginIsolatedContextType: []const u8 = "NotIsolated",
69-
gatedAPIFeatures: [][]const u8 = &[0][]const u8{},
70-
},
73+
frame: Frame,
7174
},
7275
childFrames: ?[]@This() = null,
7376
};
@@ -105,6 +108,13 @@ fn setLifecycleEventsEnabled(
105108
return result(alloc, id, null, null, sessionID);
106109
}
107110

111+
const LifeCycleEvent = struct {
112+
frameId: []const u8,
113+
loaderId: ?[]const u8,
114+
name: []const u8 = undefined,
115+
timestamp: f32 = undefined,
116+
};
117+
108118
fn addScriptToEvaluateOnNewDocument(
109119
alloc: std.mem.Allocator,
110120
id: u64,
@@ -162,14 +172,31 @@ fn navigate(
162172
frameId: ?[]const u8 = null,
163173
referrerPolicy: ?[]const u8 = null, // TODO: enum
164174
};
165-
const content = try cdp.getContent(alloc, Params, scanner);
166-
std.debug.assert(content.sessionID != null);
175+
const input = try cdp.getContent(alloc, Params, scanner);
176+
const sessionID = input.sessionID;
177+
std.debug.assert(sessionID != null);
167178

168179
// change state
169-
ctx.state.url = content.params.url;
180+
ctx.state.url = input.params.url;
170181
ctx.state.loaderID = "AF8667A203C5392DBE9AC290044AA4C2";
171182

172-
var page = try ctx.browser.currentSession().createPage();
183+
var life_event = LifeCycleEvent{
184+
.frameId = ctx.state.frameID,
185+
.loaderId = ctx.state.loaderID,
186+
};
187+
var ts_event: cdp.TimestampEvent = undefined;
188+
189+
// frameStartedLoading event
190+
const FrameStartedLoading = struct {
191+
frameId: []const u8,
192+
};
193+
const frame_started_loading = FrameStartedLoading{ .frameId = ctx.state.frameID };
194+
try sendEvent(alloc, ctx, "Page.frameStartedLoading", FrameStartedLoading, frame_started_loading, sessionID);
195+
if (ctx.state.page_life_cycle_events) {
196+
life_event.name = "init";
197+
life_event.timestamp = 343721.796037;
198+
try sendEvent(alloc, ctx, "Page.lifeCycleEvent", LifeCycleEvent, life_event, sessionID);
199+
}
173200

174201
// output
175202
const Resp = struct {
@@ -181,5 +208,56 @@ fn navigate(
181208
.frameId = ctx.state.frameID,
182209
.loaderId = ctx.state.loaderID,
183210
};
184-
return result(alloc, id, Resp, resp, content.sessionID);
211+
const res = try result(alloc, id, Resp, resp, sessionID);
212+
std.log.debug("res {s}", .{res});
213+
try server.sendSync(ctx, res);
214+
215+
// launch navigate
216+
var p = try ctx.browser.currentSession().createPage();
217+
_ = try p.navigate(input.params.url);
218+
219+
// frameNavigated event
220+
const FrameNavigated = struct {
221+
frame: Frame,
222+
type: []const u8 = "Navigation",
223+
};
224+
const frame_navigated = FrameNavigated{
225+
.frame = .{
226+
.id = ctx.state.frameID,
227+
.url = ctx.state.url,
228+
.securityOrigin = ctx.state.securityOrigin,
229+
.secureContextType = ctx.state.secureContextType,
230+
.loaderId = ctx.state.loaderID,
231+
},
232+
};
233+
try sendEvent(alloc, ctx, "Page.frameNavigated", FrameNavigated, frame_navigated, sessionID);
234+
if (ctx.state.page_life_cycle_events) {
235+
life_event.name = "load";
236+
life_event.timestamp = 343721.824655;
237+
try sendEvent(alloc, ctx, "Page.lifeCycleEvent", LifeCycleEvent, life_event, sessionID);
238+
}
239+
240+
// domContentEventFired event
241+
ts_event = .{ .timestamp = 343721.803338 };
242+
try sendEvent(alloc, ctx, "Page.domContentEventFired", cdp.TimestampEvent, ts_event, sessionID);
243+
if (ctx.state.page_life_cycle_events) {
244+
life_event.name = "DOMContentLoaded";
245+
life_event.timestamp = 343721.803338;
246+
try sendEvent(alloc, ctx, "Page.lifeCycleEvent", LifeCycleEvent, life_event, sessionID);
247+
}
248+
249+
// loadEventFired event
250+
ts_event = .{ .timestamp = 343721.824655 };
251+
try sendEvent(alloc, ctx, "Page.loadEventFired", cdp.TimestampEvent, ts_event, sessionID);
252+
if (ctx.state.page_life_cycle_events) {
253+
life_event.name = "load";
254+
life_event.timestamp = 343721.824655;
255+
try sendEvent(alloc, ctx, "Page.lifeCycleEvent", LifeCycleEvent, life_event, sessionID);
256+
}
257+
258+
// frameStoppedLoading
259+
const FrameStoppedLoading = struct { frameId: []const u8 };
260+
try sendEvent(alloc, ctx, "Page.frameStoppedLoading", FrameStoppedLoading, .{ .frameId = ctx.state.frameID }, sessionID);
261+
262+
return "";
185263
}

src/server.zig

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,12 @@ pub const Cmd = struct {
7676
}
7777
@panic(@errorName(err));
7878
};
79-
std.log.debug("res {s}", .{res});
8079

81-
sendAsync(self, res) catch unreachable;
80+
// send result
81+
if (!std.mem.eql(u8, res, "")) {
82+
std.log.debug("res {s}", .{res});
83+
sendAsync(self, res) catch unreachable;
84+
}
8285

8386
if (pos == null) break;
8487

0 commit comments

Comments
 (0)