Skip to content

Commit 901f79b

Browse files
committed
support url on createTarget and send lifecycle events
Support url parameter on createTarget. we now navigate on createTarget to dispatch events correctly, even in case of about:blank
1 parent 60ae5b5 commit 901f79b

File tree

3 files changed

+39
-9
lines changed

3 files changed

+39
-9
lines changed

src/browser/Page.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ const timestamp = @import("../datetime.zig").timestamp;
6363
const milliTimestamp = @import("../datetime.zig").milliTimestamp;
6464

6565
pub threadlocal var current: *Page = undefined;
66-
var default_url = URL{ ._raw = "about/blank" };
66+
var default_url = URL{ ._raw = "about:blank" };
6767
pub var default_location: Location = Location{ ._url = &default_url };
6868

6969
pub const BUF_SIZE = 1024;
@@ -201,7 +201,7 @@ fn reset(self: *Page, comptime initializing: bool) !void {
201201
self._factory = Factory.init(self);
202202

203203
self.version = 0;
204-
self.url = "about/blank";
204+
self.url = "about:blank";
205205

206206
self.document = (try self._factory.document(Node.Document.HTMLDocument{ ._proto = undefined })).asDocument();
207207

src/cdp/domains/page.zig

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
const std = @import("std");
2020
const Page = @import("../../browser/Page.zig");
2121
const Notification = @import("../../Notification.zig");
22+
const timestampF = @import("../../datetime.zig").timestamp;
2223

2324
const Allocator = std.mem.Allocator;
2425

@@ -82,11 +83,36 @@ fn setLifecycleEventsEnabled(cmd: anytype) !void {
8283
})) orelse return error.InvalidParams;
8384

8485
const bc = cmd.browser_context orelse return error.BrowserContextNotLoaded;
85-
if (params.enabled) {
86-
try bc.lifecycleEventsEnable();
87-
} else {
86+
87+
if (params.enabled == false) {
8888
bc.lifecycleEventsDisable();
89+
return cmd.sendResult(null, .{});
8990
}
91+
92+
// Enable lifecycle events.
93+
try bc.lifecycleEventsEnable();
94+
95+
// When we enable lifecycle events, we must dispatch events for all
96+
// attached targets.
97+
const page = bc.session.currentPage() orelse return error.PageNotLoaded;
98+
99+
if (page._load_state == .complete) {
100+
const now = timestampF(.monotonic);
101+
const http_client = page._session.browser.http_client;
102+
103+
try sendPageLifecycle(bc, "DOMContentLoaded", now);
104+
try sendPageLifecycle(bc, "load", now);
105+
106+
const http_active = http_client.active;
107+
const total_network_activity = http_active + http_client.intercepted;
108+
if (page._notified_network_almost_idle.check(total_network_activity <= 2)) {
109+
try sendPageLifecycle(bc, "networkAlmostIdle", now);
110+
}
111+
if (page._notified_network_idle.check(total_network_activity == 0)) {
112+
try sendPageLifecycle(bc, "networkIdle", now);
113+
}
114+
}
115+
90116
return cmd.sendResult(null, .{});
91117
}
92118

src/cdp/domains/target.zig

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ fn disposeBrowserContext(cmd: anytype) !void {
109109

110110
fn createTarget(cmd: anytype) !void {
111111
const params = (try cmd.params(struct {
112-
// url: []const u8,
112+
url: []const u8 = "about:blank",
113113
// width: ?u64 = null,
114114
// height: ?u64 = null,
115115
browserContextId: ?[]const u8 = null,
@@ -168,7 +168,7 @@ fn createTarget(cmd: anytype) !void {
168168
.targetInfo = TargetInfo{
169169
.attached = false,
170170
.targetId = target_id,
171-
.title = "about:blank",
171+
.title = params.url,
172172
.browserContextId = bc.id,
173173
.url = "about:blank",
174174
},
@@ -179,6 +179,10 @@ fn createTarget(cmd: anytype) !void {
179179
try doAttachtoTarget(cmd, target_id);
180180
}
181181

182+
try page.navigate(params.url, .{
183+
.reason = .address_bar,
184+
});
185+
182186
try cmd.sendResult(.{
183187
.targetId = target_id,
184188
}, .{});
@@ -518,7 +522,7 @@ test "cdp.target: createTarget" {
518522
{
519523
var ctx = testing.context();
520524
defer ctx.deinit();
521-
try ctx.processMessage(.{ .id = 10, .method = "Target.createTarget", .params = .{ .url = "about/blank" } });
525+
try ctx.processMessage(.{ .id = 10, .method = "Target.createTarget", .params = .{ .url = "about:blank" } });
522526

523527
// should create a browser context
524528
const bc = ctx.cdp().browser_context.?;
@@ -530,7 +534,7 @@ test "cdp.target: createTarget" {
530534
defer ctx.deinit();
531535
// active auto attach to get the Target.attachedToTarget event.
532536
try ctx.processMessage(.{ .id = 9, .method = "Target.setAutoAttach", .params = .{ .autoAttach = true, .waitForDebuggerOnStart = false } });
533-
try ctx.processMessage(.{ .id = 10, .method = "Target.createTarget", .params = .{ .url = "about/blank" } });
537+
try ctx.processMessage(.{ .id = 10, .method = "Target.createTarget", .params = .{ .url = "about:blank" } });
534538

535539
// should create a browser context
536540
const bc = ctx.cdp().browser_context.?;

0 commit comments

Comments
 (0)