Add Target.createTarget

Signed-off-by: Francis Bouvier <francis@lightpanda.io>
This commit is contained in:
Francis Bouvier
2024-04-18 16:43:19 +02:00
parent 0506a7bb53
commit 9974b56607

View File

@@ -11,6 +11,7 @@ const TargetMethods = enum {
setAutoAttach, setAutoAttach,
getTargetInfo, getTargetInfo,
createBrowserContext, createBrowserContext,
createTarget,
}; };
pub fn target( pub fn target(
@@ -26,6 +27,7 @@ pub fn target(
.setAutoAttach => tagetSetAutoAttach(alloc, id, scanner, ctx), .setAutoAttach => tagetSetAutoAttach(alloc, id, scanner, ctx),
.getTargetInfo => tagetGetTargetInfo(alloc, id, scanner, ctx), .getTargetInfo => tagetGetTargetInfo(alloc, id, scanner, ctx),
.createBrowserContext => createBrowserContext(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 BrowserTargetID = "2d2bdef9-1c95-416f-8c0e-83f3ab73a30c";
const BrowserContextID = "65618675CB7D3585A95049E9DFE95EA9"; 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 { const TargetFilter = struct {
type: []const u8, type: []const u8,
exclude: bool, exclude: bool,
@@ -56,22 +72,24 @@ fn tagetSetAutoAttach(
const sessionID = try cdp.getSessionID(scanner); const sessionID = try cdp.getSessionID(scanner);
if (sessionID == null) { if (sessionID == null) {
const AttachToTarget = struct { const attached = AttachToTarget{
sessionId: []const u8 = cdp.SessionID, .sessionId = cdp.SessionID,
targetInfo: struct { .targetInfo = .{
targetId: []const u8 = PageTargetID, .targetId = PageTargetID,
type: []const u8 = "page", .title = "New Incognito tab",
title: []const u8 = "New Incognito tab", .url = cdp.URLBase,
url: []const u8 = cdp.URLBase, .browserContextId = BrowserContextID,
attached: bool = true, },
canAccessOpener: bool = false,
browserContextId: []const u8 = BrowserContextID,
} = .{},
waitingForDebugger: bool = false,
}; };
const attached = try cdp.method(alloc, "Target.attachedToTarget", AttachToTarget, .{}, null); const event = try cdp.method(
std.log.debug("res {s}", .{attached}); alloc,
try server.sendSync(ctx, attached); "Target.attachedToTarget",
AttachToTarget,
attached,
null,
);
std.log.debug("event {s}", .{event});
try server.sendSync(ctx, event);
} }
return result(alloc, id, null, null, sessionID); return result(alloc, id, null, null, sessionID);
@@ -111,6 +129,7 @@ fn tagetGetTargetInfo(
} }
const ContextID = "22648B09EDCCDD11109E2D4FEFBE4F89"; const ContextID = "22648B09EDCCDD11109E2D4FEFBE4F89";
const ContextSessionID = "4FDC2CB760A23A220497A05C95417CF4";
fn createBrowserContext( fn createBrowserContext(
alloc: std.mem.Allocator, alloc: std.mem.Allocator,
@@ -135,3 +154,54 @@ fn createBrowserContext(
}; };
return result(alloc, id, Resp, Resp{}, sessionID); 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);
}