@@ -89,6 +89,8 @@ await _connection.SendAsync(
8989 await _initializeCompletionSource . Task . ConfigureAwait ( false ) ;
9090 }
9191
92+ public IEnumerable < ITarget > GetChildTargets ( ITarget target ) => target . ChildTargets ;
93+
9294 private void StoreExistingTargetsForInit ( )
9395 {
9496 foreach ( var kv in _discoveredTargetsByTargetId )
@@ -101,8 +103,11 @@ private void StoreExistingTargetsForInit()
101103 null ,
102104 _browser . ScreenshotTaskQueue ) ;
103105
104- if ( ( _targetFilterFunc == null || _targetFilterFunc ( targetForFilter ) ) &&
105- kv . Value . Type != TargetType . Browser )
106+ // Targets from extensions and the browser that will not be
107+ // auto-attached. Therefore, we should not add them to
108+ // #targetsIdsForInit.
109+ var skipTarget = kv . Value . Type == TargetType . Browser || kv . Value . Url . StartsWith ( "chrome-extension://" , StringComparison . OrdinalIgnoreCase ) ;
110+ if ( ! skipTarget && ( _targetFilterFunc == null || _targetFilterFunc ( targetForFilter ) ) )
106111 {
107112 _targetsIdsForInit . Add ( kv . Key ) ;
108113 }
@@ -292,6 +297,8 @@ private async Task OnAttachedToTargetAsync(object sender, TargetAttachedToTarget
292297 _attachedTargetsBySessionId . TryAdd ( session . Id , target ) ;
293298 }
294299
300+ var parentTarget = parentSession ? . Target ;
301+ parentTarget ? . AddChildTarget ( target ) ;
295302 ( parentSession ?? parentConnection as CDPSession ) ? . OnSessionReady ( session ) ;
296303
297304 await EnsureTargetsIdsForInitAsync ( ) . ConfigureAwait ( false ) ;
@@ -380,6 +387,7 @@ private void OnDetachedFromTarget(object sender, TargetDetachedFromTargetRespons
380387 return ;
381388 }
382389
390+ ( sender as CdpCDPSession ) ? . Target . RemoveChildTarget ( target ) ;
383391 _attachedTargetsByTargetId . TryRemove ( target . TargetId , out _ ) ;
384392 TargetGone ? . Invoke ( this , new TargetChangedArgs { Target = target } ) ;
385393 }
0 commit comments