From 9974b56607e614c09dcb61afccdce9f507c59eda Mon Sep 17 00:00:00 2001 From: Francis Bouvier Date: Thu, 18 Apr 2024 16:43:19 +0200 Subject: [PATCH] Add Target.createTarget Signed-off-by: Francis Bouvier --- src/cdp/target.zig | 100 ++++++++++++++++++++++++++++++++++++++------- 1 file changed, 85 insertions(+), 15 deletions(-) diff --git a/src/cdp/target.zig b/src/cdp/target.zig index f6985569..9436fe55 100644 --- a/src/cdp/target.zig +++ b/src/cdp/target.zig @@ -11,6 +11,7 @@ const TargetMethods = enum { setAutoAttach, getTargetInfo, createBrowserContext, + createTarget, }; pub fn target( @@ -26,6 +27,7 @@ pub fn target( .setAutoAttach => tagetSetAutoAttach(alloc, id, scanner, ctx), .getTargetInfo => tagetGetTargetInfo(alloc, id, scanner, ctx), .createBrowserContext => createBrowserContext(alloc, id, scanner, ctx), + .createTarget => createTarget(alloc, id, scanner, ctx), }; } @@ -33,6 +35,20 @@ const PageTargetID = "CFCD6EC01573CF29BB638E9DC0F52DDC"; const BrowserTargetID = "2d2bdef9-1c95-416f-8c0e-83f3ab73a30c"; const BrowserContextID = "65618675CB7D3585A95049E9DFE95EA9"; +const AttachToTarget = struct { + sessionId: []const u8, + targetInfo: struct { + targetId: []const u8, + type: []const u8 = "page", + title: []const u8, + url: []const u8, + attached: bool = true, + canAccessOpener: bool = false, + browserContextId: []const u8, + }, + waitingForDebugger: bool = false, +}; + const TargetFilter = struct { type: []const u8, exclude: bool, @@ -56,22 +72,24 @@ fn tagetSetAutoAttach( const sessionID = try cdp.getSessionID(scanner); if (sessionID == null) { - const AttachToTarget = struct { - sessionId: []const u8 = cdp.SessionID, - targetInfo: struct { - targetId: []const u8 = PageTargetID, - type: []const u8 = "page", - title: []const u8 = "New Incognito tab", - url: []const u8 = cdp.URLBase, - attached: bool = true, - canAccessOpener: bool = false, - browserContextId: []const u8 = BrowserContextID, - } = .{}, - waitingForDebugger: bool = false, + const attached = AttachToTarget{ + .sessionId = cdp.SessionID, + .targetInfo = .{ + .targetId = PageTargetID, + .title = "New Incognito tab", + .url = cdp.URLBase, + .browserContextId = BrowserContextID, + }, }; - const attached = try cdp.method(alloc, "Target.attachedToTarget", AttachToTarget, .{}, null); - std.log.debug("res {s}", .{attached}); - try server.sendSync(ctx, attached); + const event = try cdp.method( + alloc, + "Target.attachedToTarget", + AttachToTarget, + attached, + null, + ); + std.log.debug("event {s}", .{event}); + try server.sendSync(ctx, event); } return result(alloc, id, null, null, sessionID); @@ -111,6 +129,7 @@ fn tagetGetTargetInfo( } const ContextID = "22648B09EDCCDD11109E2D4FEFBE4F89"; +const ContextSessionID = "4FDC2CB760A23A220497A05C95417CF4"; fn createBrowserContext( alloc: std.mem.Allocator, @@ -135,3 +154,54 @@ fn createBrowserContext( }; return result(alloc, id, Resp, Resp{}, sessionID); } + +const TargetID = "57356548460A8F29706A2ADF14316298"; + +fn createTarget( + alloc: std.mem.Allocator, + id: u64, + scanner: *std.json.Scanner, + ctx: *Ctx, +) ![]const u8 { + + // input + const Params = struct { + url: []const u8, + width: ?u64 = null, + height: ?u64 = null, + browserContextId: []const u8, + enableBeginFrameControl: bool = false, + newWindow: bool = false, + background: bool = false, + forTab: ?bool = null, + }; + _ = try getParams(alloc, Params, scanner); + const sessionID = try cdp.getSessionID(scanner); + + // send attachToTarget event + const attached = AttachToTarget{ + .sessionId = ContextSessionID, + .targetInfo = .{ + .targetId = TargetID, + .title = "", + .url = "about:blank", + .browserContextId = ContextID, + }, + .waitingForDebugger = true, + }; + const event = try cdp.method( + alloc, + "Target.attachedToTarget", + AttachToTarget, + attached, + sessionID, + ); + std.log.debug("event {s}", .{event}); + try server.sendSync(ctx, event); + + // output + const Resp = struct { + targetId: []const u8 = TargetID, + }; + return result(alloc, id, Resp, Resp{}, sessionID); +}