@@ -39,6 +39,7 @@ const Methods = enum {
3939 createTarget ,
4040 closeTarget ,
4141 sendMessageToTarget ,
42+ detachFromTarget ,
4243};
4344
4445pub fn target (
@@ -60,13 +61,14 @@ pub fn target(
6061 .createTarget = > createTarget (alloc , msg , ctx ),
6162 .closeTarget = > closeTarget (alloc , msg , ctx ),
6263 .sendMessageToTarget = > sendMessageToTarget (alloc , msg , ctx ),
64+ .detachFromTarget = > detachFromTarget (alloc , msg , ctx ),
6365 };
6466}
6567
6668// TODO: hard coded IDs
67- const PageTargetID = "CFCD6EC01573CF29BB638E9DC0F52DDC " ;
68- const BrowserTargetID = "2d2bdef9-1c95-416f-8c0e -83f3ab73a30c" ;
69- const BrowserContextID = "65618675CB7D3585A95049E9DFE95EA9 " ;
69+ pub const PageTargetID = "PAGETARGETIDB638E9DC0F52DDC " ;
70+ pub const BrowserTargetID = "browser9-targ-et6f-id0e -83f3ab73a30c" ;
71+ pub const BrowserContextID = "BROWSERCONTEXTIDA95049E9DFE95EA9 " ;
7072
7173// TODO: noop method
7274fn setDiscoverTargets (
@@ -138,7 +140,7 @@ fn setAutoAttach(
138140 .sessionId = cdp .BrowserSessionID ,
139141 .targetInfo = .{
140142 .targetId = PageTargetID ,
141- .title = "New Incognito tab " ,
143+ .title = "about:blank " ,
142144 .url = cdp .URLBase ,
143145 .browserContextId = BrowserContextID ,
144146 },
@@ -171,8 +173,8 @@ fn attachToTarget(
171173 const attached = AttachToTarget {
172174 .sessionId = cdp .BrowserSessionID ,
173175 .targetInfo = .{
174- .targetId = PageTargetID ,
175- .title = "New Incognito tab " ,
176+ .targetId = input . params . targetId ,
177+ .title = "about:blank " ,
176178 .url = cdp .URLBase ,
177179 .browserContextId = BrowserContextID ,
178180 },
@@ -185,7 +187,7 @@ fn attachToTarget(
185187 sessionId : []const u8 ,
186188 };
187189 const output = SessionId {
188- .sessionId = input .sessionId orelse BrowserContextID ,
190+ .sessionId = input .sessionId orelse cdp . BrowserSessionID ,
189191 };
190192 return result (alloc , input .id , SessionId , output , null );
191193}
@@ -252,7 +254,7 @@ fn getBrowserContexts(
252254 return result (alloc , input .id , Resp , resp , null );
253255}
254256
255- const ContextID = "22648B09EDCCDD11109E2D4FEFBE4F89 " ;
257+ const ContextID = "CONTEXTIDDCCDD11109E2D4FEFBE4F89 " ;
256258
257259// TODO: noop method
258260fn createBrowserContext (
@@ -313,8 +315,8 @@ fn disposeBrowserContext(
313315}
314316
315317// TODO: hard coded IDs
316- const TargetID = "57356548460A8F29706A2ADF14316298 " ;
317- const LoaderID = "DD4A76F842AA389647D702B4D805F49A " ;
318+ const TargetID = "TARGETID460A8F29706A2ADF14316298 " ;
319+ const LoaderID = "LOADERID42AA389647D702B4D805F49A " ;
318320
319321fn createTarget (
320322 alloc : std.mem.Allocator ,
@@ -342,6 +344,23 @@ fn createTarget(
342344 ctx .state .securityOrigin = "://" ;
343345 ctx .state .secureContextType = "InsecureScheme" ;
344346 ctx .state .loaderID = LoaderID ;
347+ ctx .state .sessionID = msg .sessionId ;
348+
349+ // TODO stop the previous page instead?
350+ if (ctx .browser .session .page != null ) return error .pageAlreadyExists ;
351+
352+ // create the page
353+ const p = try ctx .browser .session .createPage ();
354+ ctx .state .executionContextId += 1 ;
355+ // start the js env
356+ const auxData = try std .fmt .allocPrint (
357+ alloc ,
358+ // NOTE: we assume this is the default web page
359+ "{{\" isDefault\" :true,\" type\" :\" default\" ,\" frameId\" :\" {s}\" }}" ,
360+ .{ctx .state .frameID },
361+ );
362+ defer alloc .free (auxData );
363+ try p .start (auxData );
345364
346365 // send targetCreated event
347366 const created = TargetCreated {
@@ -361,9 +380,10 @@ fn createTarget(
361380 .sessionId = cdp .ContextSessionID ,
362381 .targetInfo = .{
363382 .targetId = ctx .state .frameID ,
364- .title = "" ,
383+ .title = "about:blank " ,
365384 .url = ctx .state .url ,
366385 .browserContextId = input .params .browserContextId orelse ContextID ,
386+ .attached = true ,
367387 },
368388 .waitingForDebugger = true ,
369389 };
@@ -438,6 +458,8 @@ fn closeTarget(
438458 null ,
439459 );
440460
461+ if (ctx .browser .session .page != null ) ctx .browser .session .page .? .end ();
462+
441463 return "" ;
442464}
443465
@@ -484,3 +506,18 @@ fn sendMessageToTarget(
484506
485507 return "" ;
486508}
509+
510+ // noop
511+ fn detachFromTarget (
512+ alloc : std.mem.Allocator ,
513+ msg : * IncomingMessage ,
514+ _ : * Ctx ,
515+ ) ! []const u8 {
516+ // input
517+ const input = try Input (void ).get (alloc , msg );
518+ defer input .deinit ();
519+ log .debug ("Req > id {d}, method {s}" , .{ input .id , "target.detachFromTarget" });
520+
521+ // output
522+ return result (alloc , input .id , bool , true , input .sessionId );
523+ }
0 commit comments