Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Puppeteer Puppeteer Cdp Target

From Leeroopedia
Property Value
sources packages/puppeteer-core/src/cdp/Target.ts
domains Target Management, CDP, Browser Automation
last_updated 2026-02-12 00:00 GMT

Overview

Description

The Target module defines the CDP-specific target hierarchy for representing browser targets such as pages, workers, DevTools panels, and other target types. It contains several classes:

CdpTarget -- The base class for all CDP targets. Key responsibilities:

  • Wraps a Protocol.Target.TargetInfo object and maintains the target's ID, URL, type, browser context, and CDP session.
  • Tracks initialization and closure state via Deferred objects (_initializedDeferred and _isClosedDeferred).
  • Manages child targets in a parent-child tree structure.
  • Maps CDP target types (page, background_page, service_worker, shared_worker, browser, webview, tab) to Puppeteer TargetType enum values.
  • Provides factory method for creating CDP sessions via sessionFactory.
  • Determines target visibility with _isTargetExposed() (hides tab targets and targets with subtypes).

PageTarget -- Extends CdpTarget for page targets. Lazily creates CdpPage instances, handles popup detection by checking if the opener page has Popup event listeners, and delays initialization until the target URL is non-empty.

DevToolsTarget -- Extends PageTarget for DevTools panel targets. No additional behavior.

WorkerTarget -- Extends CdpTarget for service workers and shared workers. Lazily creates CdpWebWorker instances.

OtherTarget -- Extends CdpTarget for all other target types. No additional behavior.

InitializationStatus -- An enum with SUCCESS and ABORTED values used to track target initialization state.

Usage

Targets are created internally by the CdpBrowser class via its #createTarget factory method based on the target info received from CDP. Users access targets through browser.targets(), page.target(), etc.

Code Reference

Source Location: packages/puppeteer-core/src/cdp/Target.ts (311 lines)

Signature:

export enum InitializationStatus {
  SUCCESS = 'success',
  ABORTED = 'aborted',
}

export class CdpTarget extends Target {
  constructor(
    targetInfo: Protocol.Target.TargetInfo,
    session: CdpCDPSession | undefined,
    browserContext: BrowserContext | undefined,
    targetManager: TargetManager | undefined,
    sessionFactory: ((isAutoAttachEmulated: boolean) => Promise<CdpCDPSession>) | undefined,
  );

  override url(): string;
  override type(): TargetType;
  override browser(): Browser;
  override browserContext(): BrowserContext;
  override opener(): Target | undefined;
  override createCDPSession(): Promise<CdpCDPSession>;
  override async asPage(): Promise<Page>;
}

export class PageTarget extends CdpTarget {
  constructor(
    targetInfo: Protocol.Target.TargetInfo,
    session: CdpCDPSession | undefined,
    browserContext: BrowserContext,
    targetManager: TargetManager,
    sessionFactory: (isAutoAttachEmulated: boolean) => Promise<CdpCDPSession>,
    defaultViewport: Viewport | null,
  );
  override async page(): Promise<Page | null>;
}

export class WorkerTarget extends CdpTarget {
  override async worker(): Promise<CdpWebWorker | null>;
}

export class DevToolsTarget extends PageTarget {}
export class OtherTarget extends CdpTarget {}

Import:

import {
  CdpTarget,
  PageTarget,
  DevToolsTarget,
  WorkerTarget,
  OtherTarget,
  InitializationStatus,
} from 'puppeteer-core/lib/cdp/Target.js';

I/O Contract

Inputs:

Parameter Type Required Description
targetInfo Protocol.Target.TargetInfo Yes CDP target information (targetId, type, url, browserContextId, etc.)
session undefined No An existing CDP session for this target
browserContext BrowserContext No The browser context this target belongs to
targetManager TargetManager No The target manager that tracks this target
sessionFactory Function No Factory for creating CDP sessions to this target
defaultViewport null No Default viewport for page targets

Outputs:

Output Type Description
url() string The URL of the target
type() TargetType The target type (PAGE, BACKGROUND_PAGE, SERVICE_WORKER, SHARED_WORKER, BROWSER, WEBVIEW, TAB, OTHER)
page() null> The Page instance (for PageTarget), or null
worker() null> The WebWorker instance (for WorkerTarget), or null
browser() Browser The browser this target belongs to
browserContext() BrowserContext The browser context of this target
opener() undefined The target that opened this target (e.g., popup opener)

Usage Examples

// Get all targets
const targets = browser.targets();
for (const target of targets) {
  console.log(`${target.type()}: ${target.url()}`);
}

// Get the browser target
const browserTarget = browser.target();
console.log(browserTarget.type()); // 'browser'

// Get a page's target
const page = await browser.newPage();
const target = page.target();
console.log(target.url()); // 'about:blank'

// Wait for a specific target
const newTarget = await browser.waitForTarget(t => t.url() === 'https://example.com');
const page = await newTarget.page();

// Create a CDP session for a target
const session = await target.createCDPSession();
await session.send('Runtime.enable');

// Handle popup detection (internally uses opener())
page.on('popup', async popup => {
  console.log('Popup opened:', popup.url());
});

// Access a service worker target
const swTarget = targets.find(t => t.type() === 'service_worker');
if (swTarget) {
  const worker = await swTarget.worker();
  console.log('Service worker URL:', worker.url());
}

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment