diff --git a/src/cdp/cdp.zig b/src/cdp/cdp.zig index 30a94594..a2956513 100644 --- a/src/cdp/cdp.zig +++ b/src/cdp/cdp.zig @@ -129,14 +129,14 @@ pub fn result( return stringify(alloc, resp); } -// caller owns the slice returned -pub fn method( +pub fn sendEvent( alloc: std.mem.Allocator, + ctx: *Ctx, name: []const u8, comptime T: type, params: T, sessionID: ?[]const u8, -) ![]const u8 { +) !void { const Resp = struct { method: []const u8, params: T, @@ -144,7 +144,9 @@ pub fn method( }; const resp = Resp{ .method = name, .params = params, .sessionId = sessionID }; - return stringify(alloc, resp); + const event_msg = try stringify(alloc, resp); + std.log.debug("event {s}", .{event_msg}); + try server.sendSync(ctx, event_msg); } pub fn getParams( diff --git a/src/cdp/page.zig b/src/cdp/page.zig index 1292186f..fd4bdfb9 100644 --- a/src/cdp/page.zig +++ b/src/cdp/page.zig @@ -168,6 +168,8 @@ fn navigate( ctx.state.url = content.params.url; ctx.state.loaderID = "AF8667A203C5392DBE9AC290044AA4C2"; + var page = try ctx.browser.currentSession().createPage(); + // output const Resp = struct { frameId: []const u8, diff --git a/src/cdp/runtime.zig b/src/cdp/runtime.zig index d5db891a..b1ebc555 100644 --- a/src/cdp/runtime.zig +++ b/src/cdp/runtime.zig @@ -31,12 +31,68 @@ fn enable( alloc: std.mem.Allocator, id: u64, scanner: *std.json.Scanner, - _: *Ctx, + ctx: *Ctx, ) ![]const u8 { + _ = ctx; + + // input const sessionID = try cdp.getSessionID(scanner); + + // output + // const uniqueID = "1367118932354479079.-1471398151593995849"; + // const mainCtx = try executionContextCreated( + // alloc, + // 1, + // cdp.URLBase, + // "", + // uniqueID, + // .{}, + // sessionID, + // ); + // std.log.debug("res {s}", .{mainCtx}); + // try server.sendAsync(ctx, mainCtx); + return result(alloc, id, null, null, sessionID); } +const AuxData = struct { + isDefault: bool = true, + type: []const u8 = "default", + frameId: []const u8 = cdp.FrameID, +}; + +const ExecutionContextDescription = struct { + id: u64, + origin: []const u8, + name: []const u8, + uniqueId: []const u8, + auxData: ?AuxData = null, +}; + +fn executionContextCreated( + alloc: std.mem.Allocator, + id: u64, + origin: []const u8, + name: []const u8, + uniqueID: []const u8, + auxData: ?AuxData, + sessionID: ?[]const u8, +) ![]const u8 { + const Params = struct { + context: ExecutionContextDescription, + }; + const params = Params{ + .context = .{ + .id = id, + .origin = origin, + .name = name, + .uniqueId = uniqueID, + .auxData = auxData, + }, + }; + return try cdp.method(alloc, "Runtime.executionContextCreated", Params, params, sessionID); +} + fn runIfWaitingForDebugger( alloc: std.mem.Allocator, id: u64, diff --git a/src/cdp/target.zig b/src/cdp/target.zig index 5a6ab47a..67fd6474 100644 --- a/src/cdp/target.zig +++ b/src/cdp/target.zig @@ -81,15 +81,7 @@ fn tagetSetAutoAttach( .browserContextId = BrowserContextID, }, }; - const event = try cdp.method( - alloc, - "Target.attachedToTarget", - AttachToTarget, - attached, - null, - ); - std.log.debug("event {s}", .{event}); - try server.sendSync(ctx, event); + try cdp.sendEvent(alloc, ctx, "Target.attachedToTarget", AttachToTarget, attached, null); } return result(alloc, id, null, null, sessionID); @@ -196,15 +188,7 @@ fn createTarget( }, .waitingForDebugger = true, }; - const event = try cdp.method( - alloc, - "Target.attachedToTarget", - AttachToTarget, - attached, - sessionID, - ); - std.log.debug("event {s}", .{event}); - try server.sendSync(ctx, event); + try cdp.sendEvent(alloc, ctx, "Target.attachedToTarget", AttachToTarget, attached, sessionID); // output const Resp = struct {