UNPKG

2.24 kBPlain TextView Raw
1/**
2 * @license
3 * Copyright 2023 Google Inc.
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7import type {Browser} from './Browser.js';
8import type {BrowserContext} from './BrowserContext.js';
9import type {CDPSession} from './CDPSession.js';
10import type {Page} from './Page.js';
11import type {WebWorker} from './WebWorker.js';
12
13/**
14 * @public
15 */
16export enum TargetType {
17 PAGE = 'page',
18 BACKGROUND_PAGE = 'background_page',
19 SERVICE_WORKER = 'service_worker',
20 SHARED_WORKER = 'shared_worker',
21 BROWSER = 'browser',
22 WEBVIEW = 'webview',
23 OTHER = 'other',
24 /**
25 * @internal
26 */
27 TAB = 'tab',
28}
29
30/**
31 * Target represents a
32 * {@link https://chromedevtools.github.io/devtools-protocol/tot/Target/ | CDP target}.
33 * In CDP a target is something that can be debugged such a frame, a page or a
34 * worker.
35 * @public
36 */
37export abstract class Target {
38 /**
39 * @internal
40 */
41 protected constructor() {}
42
43 /**
44 * If the target is not of type `"service_worker"` or `"shared_worker"`, returns `null`.
45 */
46 async worker(): Promise<WebWorker | null> {
47 return null;
48 }
49
50 /**
51 * If the target is not of type `"page"`, `"webview"` or `"background_page"`,
52 * returns `null`.
53 */
54 async page(): Promise<Page | null> {
55 return null;
56 }
57
58 /**
59 * Forcefully creates a page for a target of any type. It is useful if you
60 * want to handle a CDP target of type `other` as a page. If you deal with a
61 * regular page target, use {@link Target.page}.
62 */
63 abstract asPage(): Promise<Page>;
64
65 abstract url(): string;
66
67 /**
68 * Creates a Chrome Devtools Protocol session attached to the target.
69 */
70 abstract createCDPSession(): Promise<CDPSession>;
71
72 /**
73 * Identifies what kind of target this is.
74 *
75 * @remarks
76 *
77 * See {@link https://developer.chrome.com/extensions/background_pages | docs} for more info about background pages.
78 */
79 abstract type(): TargetType;
80
81 /**
82 * Get the browser the target belongs to.
83 */
84 abstract browser(): Browser;
85
86 /**
87 * Get the browser context the target belongs to.
88 */
89 abstract browserContext(): BrowserContext;
90
91 /**
92 * Get the target that opened this target. Top-level targets return `null`.
93 */
94 abstract opener(): Target | undefined;
95}