Remove BrowserContext URL

Add BrowserContext.getURL which gets the URL from the session.page.
This commit is contained in:
Karl Seguin
2025-04-07 13:51:50 +08:00
parent b76875bf5d
commit f38a0d2d67
4 changed files with 13 additions and 12 deletions

View File

@@ -284,8 +284,6 @@ pub fn BrowserContext(comptime CDP_T: type) type {
// we should reject it. // we should reject it.
session_id: ?[]const u8, session_id: ?[]const u8,
// State
url: []const u8,
loader_id: []const u8, loader_id: []const u8,
security_origin: []const u8, security_origin: []const u8,
page_life_cycle_events: bool, page_life_cycle_events: bool,
@@ -306,7 +304,6 @@ pub fn BrowserContext(comptime CDP_T: type) type {
.cdp = cdp, .cdp = cdp,
.target_id = null, .target_id = null,
.session_id = null, .session_id = null,
.url = URL_BASE,
.security_origin = URL_BASE, .security_origin = URL_BASE,
.secure_context_type = "Secure", // TODO = enum .secure_context_type = "Secure", // TODO = enum
.loader_id = LOADER_ID, .loader_id = LOADER_ID,
@@ -336,6 +333,11 @@ pub fn BrowserContext(comptime CDP_T: type) type {
}; };
} }
pub fn getURL(self: *const Self) ?[]const u8 {
const page = self.session.currentPage() orelse return null;
return page.rawuri;
}
pub fn onInspectorResponse(ctx: *anyopaque, _: u32, msg: []const u8) void { pub fn onInspectorResponse(ctx: *anyopaque, _: u32, msg: []const u8) void {
if (std.log.defaultLogEnabled(.debug)) { if (std.log.defaultLogEnabled(.debug)) {
// msg should be {"id":<id>,... // msg should be {"id":<id>,...

View File

@@ -61,10 +61,10 @@ fn getFrameTree(cmd: anytype) !void {
return cmd.sendResult(.{ return cmd.sendResult(.{
.frameTree = .{ .frameTree = .{
.frame = Frame{ .frame = Frame{
.url = bc.url,
.id = target_id, .id = target_id,
.loaderId = bc.loader_id, .loaderId = bc.loader_id,
.securityOrigin = bc.security_origin, .securityOrigin = bc.security_origin,
.url = bc.getURL() orelse "about:blank",
.secureContextType = bc.secure_context_type, .secureContextType = bc.secure_context_type,
}, },
}, },
@@ -154,7 +154,6 @@ pub fn navigateToUrl(cmd: anytype, url: []const u8, send_result: bool) !void {
// change state // change state
bc.reset(); bc.reset();
bc.url = url;
bc.loader_id = cmd.cdp.loader_id_gen.next(); bc.loader_id = cmd.cdp.loader_id_gen.next();
const LifecycleEvent = struct { const LifecycleEvent = struct {
@@ -285,7 +284,7 @@ test "cdp.page: getFrameTree" {
.frame = .{ .frame = .{
.id = "TID-3", .id = "TID-3",
.loaderId = bc.loader_id, .loaderId = bc.loader_id,
.url = bc.url, .url = "about:blank",
.domainAndRegistry = "", .domainAndRegistry = "",
.securityOrigin = bc.security_origin, .securityOrigin = bc.security_origin,
.mimeType = "text/html", .mimeType = "text/html",

View File

@@ -132,7 +132,6 @@ fn createTarget(cmd: anytype) !void {
_ = try bc.session.createPage(aux_data); _ = try bc.session.createPage(aux_data);
// change CDP state // change CDP state
bc.url = "about:blank";
bc.security_origin = "://"; bc.security_origin = "://";
bc.secure_context_type = "InsecureScheme"; bc.secure_context_type = "InsecureScheme";
bc.loader_id = LOADER_ID; bc.loader_id = LOADER_ID;
@@ -142,11 +141,11 @@ fn createTarget(cmd: anytype) !void {
// has been enabled? // has been enabled?
try cmd.sendEvent("Target.targetCreated", .{ try cmd.sendEvent("Target.targetCreated", .{
.targetInfo = TargetInfo{ .targetInfo = TargetInfo{
.url = bc.url, .attached = false,
.targetId = target_id, .targetId = target_id,
.title = "about:blank", .title = "about:blank",
.browserContextId = bc.id, .browserContextId = bc.id,
.attached = false, .url = "about:blank",
}, },
}, .{}); }, .{});

View File

@@ -85,6 +85,7 @@ const Session = struct {
return error.MockBrowserPageAlreadyExists; return error.MockBrowserPageAlreadyExists;
} }
self.page = .{ self.page = .{
.rawuri = "",
.session = self, .session = self,
.aux_data = try self.arena.dupe(u8, aux_data orelse ""), .aux_data = try self.arena.dupe(u8, aux_data orelse ""),
}; };
@@ -103,6 +104,7 @@ const Session = struct {
const Page = struct { const Page = struct {
session: *Session, session: *Session,
rawuri: []const u8,
aux_data: []const u8 = "", aux_data: []const u8 = "",
doc: ?*parser.Document = null, doc: ?*parser.Document = null,
@@ -111,10 +113,9 @@ const Page = struct {
_ = aux_data; _ = aux_data;
} }
const MouseEvent = @import("../browser/browser.zig").Page.MouseEvent;
const ClickResult = @import("../browser/browser.zig").Page.ClickResult; const ClickResult = @import("../browser/browser.zig").Page.ClickResult;
pub fn click(_: *Page, _: Allocator, x: u32, y: u32) !?ClickResult { pub fn mouseEvent(_: *Page, _: Allocator, _: MouseEvent) !?ClickResult {
_ = x;
_ = y;
return null; return null;
} }
}; };