2019-01-26 04:21:14 +00:00
|
|
|
/**
|
|
|
|
* Copyright 2019 Google Inc. All rights reserved.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2022-06-22 13:25:44 +00:00
|
|
|
import {Page, PageEmittedEvents} from './Page.js';
|
|
|
|
import {WebWorker} from './WebWorker.js';
|
|
|
|
import {CDPSession} from './Connection.js';
|
|
|
|
import {Browser, BrowserContext, IsPageTargetCallback} from './Browser.js';
|
|
|
|
import {Viewport} from './PuppeteerViewport.js';
|
|
|
|
import {Protocol} from 'devtools-protocol';
|
|
|
|
import {TaskQueue} from './TaskQueue.js';
|
2022-07-21 18:50:46 +00:00
|
|
|
import {TargetManager} from './TargetManager.js';
|
2018-02-26 20:10:06 +00:00
|
|
|
|
2020-07-02 11:15:39 +00:00
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
2020-04-29 14:18:09 +00:00
|
|
|
export class Target {
|
2022-06-13 09:16:25 +00:00
|
|
|
#browserContext: BrowserContext;
|
2022-07-21 18:50:46 +00:00
|
|
|
#session?: CDPSession;
|
2022-06-13 09:16:25 +00:00
|
|
|
#targetInfo: Protocol.Target.TargetInfo;
|
|
|
|
#sessionFactory: () => Promise<CDPSession>;
|
|
|
|
#ignoreHTTPSErrors: boolean;
|
|
|
|
#defaultViewport?: Viewport;
|
|
|
|
#pagePromise?: Promise<Page>;
|
|
|
|
#workerPromise?: Promise<WebWorker>;
|
|
|
|
#screenshotTaskQueue: TaskQueue;
|
2020-06-22 12:57:04 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2020-04-29 14:18:09 +00:00
|
|
|
_initializedPromise: Promise<boolean>;
|
2020-06-22 12:57:04 +00:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2022-05-31 14:34:16 +00:00
|
|
|
_initializedCallback!: (x: boolean) => void;
|
2020-06-22 12:57:04 +00:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2020-11-04 06:42:23 +00:00
|
|
|
_isClosedPromise: Promise<void>;
|
2020-06-22 12:57:04 +00:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2022-05-31 14:34:16 +00:00
|
|
|
_closedCallback!: () => void;
|
2020-06-22 12:57:04 +00:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2020-04-29 14:18:09 +00:00
|
|
|
_isInitialized: boolean;
|
2020-06-22 12:57:04 +00:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
_targetId: string;
|
2022-05-13 06:04:46 +00:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
_isPageTargetCallback: IsPageTargetCallback;
|
2020-04-29 14:18:09 +00:00
|
|
|
|
2022-07-21 18:50:46 +00:00
|
|
|
#targetManager: TargetManager;
|
|
|
|
|
2020-06-22 12:57:04 +00:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2020-05-07 10:54:55 +00:00
|
|
|
constructor(
|
|
|
|
targetInfo: Protocol.Target.TargetInfo,
|
2022-07-21 18:50:46 +00:00
|
|
|
session: CDPSession | undefined,
|
2020-05-07 10:54:55 +00:00
|
|
|
browserContext: BrowserContext,
|
2022-07-21 18:50:46 +00:00
|
|
|
targetManager: TargetManager,
|
2020-05-07 10:54:55 +00:00
|
|
|
sessionFactory: () => Promise<CDPSession>,
|
|
|
|
ignoreHTTPSErrors: boolean,
|
2021-09-23 12:37:35 +00:00
|
|
|
defaultViewport: Viewport | null,
|
2022-05-13 06:04:46 +00:00
|
|
|
screenshotTaskQueue: TaskQueue,
|
|
|
|
isPageTargetCallback: IsPageTargetCallback
|
2020-05-07 10:54:55 +00:00
|
|
|
) {
|
2022-07-21 18:50:46 +00:00
|
|
|
this.#session = session;
|
|
|
|
this.#targetManager = targetManager;
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#targetInfo = targetInfo;
|
|
|
|
this.#browserContext = browserContext;
|
2018-02-26 20:10:06 +00:00
|
|
|
this._targetId = targetInfo.targetId;
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#sessionFactory = sessionFactory;
|
|
|
|
this.#ignoreHTTPSErrors = ignoreHTTPSErrors;
|
|
|
|
this.#defaultViewport = defaultViewport ?? undefined;
|
|
|
|
this.#screenshotTaskQueue = screenshotTaskQueue;
|
2022-05-13 06:04:46 +00:00
|
|
|
this._isPageTargetCallback = isPageTargetCallback;
|
2022-06-22 13:25:44 +00:00
|
|
|
this._initializedPromise = new Promise<boolean>(fulfill => {
|
2022-06-15 10:42:21 +00:00
|
|
|
return (this._initializedCallback = fulfill);
|
2022-06-22 13:25:44 +00:00
|
|
|
}).then(async success => {
|
2022-06-14 11:55:35 +00:00
|
|
|
if (!success) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-12-13 01:09:42 +00:00
|
|
|
const opener = this.opener();
|
2022-06-14 11:55:35 +00:00
|
|
|
if (!opener || !opener.#pagePromise || this.type() !== 'page') {
|
2018-12-13 01:09:42 +00:00
|
|
|
return true;
|
2022-06-14 11:55:35 +00:00
|
|
|
}
|
2022-06-13 09:16:25 +00:00
|
|
|
const openerPage = await opener.#pagePromise;
|
2022-06-14 11:55:35 +00:00
|
|
|
if (!openerPage.listenerCount(PageEmittedEvents.Popup)) {
|
|
|
|
return true;
|
|
|
|
}
|
2018-12-13 01:09:42 +00:00
|
|
|
const popupPage = await this.page();
|
2020-07-06 10:34:55 +00:00
|
|
|
openerPage.emit(PageEmittedEvents.Popup, popupPage);
|
2018-12-13 01:09:42 +00:00
|
|
|
return true;
|
|
|
|
});
|
2022-06-22 13:25:44 +00:00
|
|
|
this._isClosedPromise = new Promise<void>(fulfill => {
|
2022-06-15 10:42:21 +00:00
|
|
|
return (this._closedCallback = fulfill);
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
this._isInitialized =
|
2022-06-13 09:16:25 +00:00
|
|
|
!this._isPageTargetCallback(this.#targetInfo) ||
|
|
|
|
this.#targetInfo.url !== '';
|
2022-06-14 11:55:35 +00:00
|
|
|
if (this._isInitialized) {
|
|
|
|
this._initializedCallback(true);
|
|
|
|
}
|
2018-02-26 20:10:06 +00:00
|
|
|
}
|
|
|
|
|
2022-07-21 18:50:46 +00:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
_session(): CDPSession | undefined {
|
|
|
|
return this.#session;
|
|
|
|
}
|
|
|
|
|
2020-06-22 12:57:04 +00:00
|
|
|
/**
|
|
|
|
* Creates a Chrome Devtools Protocol session attached to the target.
|
|
|
|
*/
|
2020-04-29 14:18:09 +00:00
|
|
|
createCDPSession(): Promise<CDPSession> {
|
2022-06-13 09:16:25 +00:00
|
|
|
return this.#sessionFactory();
|
2018-02-26 20:10:06 +00:00
|
|
|
}
|
|
|
|
|
2022-07-21 18:50:46 +00:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
_targetManager(): TargetManager {
|
|
|
|
return this.#targetManager;
|
|
|
|
}
|
|
|
|
|
2022-06-02 11:27:31 +00:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
_getTargetInfo(): Protocol.Target.TargetInfo {
|
2022-06-13 09:16:25 +00:00
|
|
|
return this.#targetInfo;
|
2022-06-02 11:27:31 +00:00
|
|
|
}
|
|
|
|
|
2020-06-22 12:57:04 +00:00
|
|
|
/**
|
|
|
|
* If the target is not of type `"page"` or `"background_page"`, returns `null`.
|
|
|
|
*/
|
2022-06-02 13:38:13 +00:00
|
|
|
async page(): Promise<Page | null> {
|
2022-06-13 09:16:25 +00:00
|
|
|
if (this._isPageTargetCallback(this.#targetInfo) && !this.#pagePromise) {
|
2022-07-21 18:50:46 +00:00
|
|
|
this.#pagePromise = (
|
|
|
|
this.#session ? Promise.resolve(this.#session) : this.#sessionFactory()
|
|
|
|
).then(client => {
|
2022-06-15 10:42:21 +00:00
|
|
|
return Page._create(
|
2020-05-07 10:54:55 +00:00
|
|
|
client,
|
|
|
|
this,
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#ignoreHTTPSErrors,
|
|
|
|
this.#defaultViewport ?? null,
|
|
|
|
this.#screenshotTaskQueue
|
2022-06-15 10:42:21 +00:00
|
|
|
);
|
|
|
|
});
|
2018-02-26 20:10:06 +00:00
|
|
|
}
|
2022-06-13 09:16:25 +00:00
|
|
|
return (await this.#pagePromise) ?? null;
|
2018-02-26 20:10:06 +00:00
|
|
|
}
|
|
|
|
|
2020-06-22 12:57:04 +00:00
|
|
|
/**
|
|
|
|
* If the target is not of type `"service_worker"` or `"shared_worker"`, returns `null`.
|
|
|
|
*/
|
2020-05-29 11:57:54 +00:00
|
|
|
async worker(): Promise<WebWorker | null> {
|
2020-05-07 10:54:55 +00:00
|
|
|
if (
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#targetInfo.type !== 'service_worker' &&
|
|
|
|
this.#targetInfo.type !== 'shared_worker'
|
2022-06-14 11:55:35 +00:00
|
|
|
) {
|
2019-05-10 00:29:18 +00:00
|
|
|
return null;
|
2022-06-14 11:55:35 +00:00
|
|
|
}
|
2022-06-13 09:16:25 +00:00
|
|
|
if (!this.#workerPromise) {
|
2019-08-15 04:43:18 +00:00
|
|
|
// TODO(einbinder): Make workers send their console logs.
|
2022-07-21 18:50:46 +00:00
|
|
|
this.#workerPromise = (
|
|
|
|
this.#session ? Promise.resolve(this.#session) : this.#sessionFactory()
|
|
|
|
).then(client => {
|
2022-06-15 10:42:21 +00:00
|
|
|
return new WebWorker(
|
|
|
|
client,
|
|
|
|
this.#targetInfo.url,
|
|
|
|
() => {} /* consoleAPICalled */,
|
|
|
|
() => {} /* exceptionThrown */
|
|
|
|
);
|
|
|
|
});
|
2019-05-10 00:29:18 +00:00
|
|
|
}
|
2022-06-13 09:16:25 +00:00
|
|
|
return this.#workerPromise;
|
2019-05-10 00:29:18 +00:00
|
|
|
}
|
|
|
|
|
2020-04-29 14:18:09 +00:00
|
|
|
url(): string {
|
2022-06-13 09:16:25 +00:00
|
|
|
return this.#targetInfo.url;
|
2018-02-26 20:10:06 +00:00
|
|
|
}
|
|
|
|
|
2020-06-22 12:57:04 +00:00
|
|
|
/**
|
|
|
|
* Identifies what kind of target this is.
|
|
|
|
*
|
|
|
|
* @remarks
|
|
|
|
*
|
|
|
|
* See {@link https://developer.chrome.com/extensions/background_pages | docs} for more info about background pages.
|
|
|
|
*/
|
2020-05-07 10:54:55 +00:00
|
|
|
type():
|
|
|
|
| 'page'
|
|
|
|
| 'background_page'
|
|
|
|
| 'service_worker'
|
|
|
|
| 'shared_worker'
|
|
|
|
| 'other'
|
2020-05-29 08:46:44 +00:00
|
|
|
| 'browser'
|
|
|
|
| 'webview' {
|
2022-06-13 09:16:25 +00:00
|
|
|
const type = this.#targetInfo.type;
|
2020-05-07 10:54:55 +00:00
|
|
|
if (
|
|
|
|
type === 'page' ||
|
|
|
|
type === 'background_page' ||
|
|
|
|
type === 'service_worker' ||
|
|
|
|
type === 'shared_worker' ||
|
2020-05-29 08:46:44 +00:00
|
|
|
type === 'browser' ||
|
|
|
|
type === 'webview'
|
2022-06-14 11:55:35 +00:00
|
|
|
) {
|
2018-02-26 20:10:06 +00:00
|
|
|
return type;
|
2022-06-14 11:55:35 +00:00
|
|
|
}
|
2018-02-26 20:10:06 +00:00
|
|
|
return 'other';
|
|
|
|
}
|
|
|
|
|
2020-06-22 12:57:04 +00:00
|
|
|
/**
|
|
|
|
* Get the browser the target belongs to.
|
|
|
|
*/
|
2020-04-29 14:18:09 +00:00
|
|
|
browser(): Browser {
|
2022-06-13 09:16:25 +00:00
|
|
|
return this.#browserContext.browser();
|
2018-04-17 17:37:17 +00:00
|
|
|
}
|
|
|
|
|
2021-05-25 06:47:25 +00:00
|
|
|
/**
|
|
|
|
* Get the browser context the target belongs to.
|
|
|
|
*/
|
2020-04-29 14:18:09 +00:00
|
|
|
browserContext(): BrowserContext {
|
2022-06-13 09:16:25 +00:00
|
|
|
return this.#browserContext;
|
2018-05-10 20:26:08 +00:00
|
|
|
}
|
|
|
|
|
2020-06-22 12:57:04 +00:00
|
|
|
/**
|
|
|
|
* Get the target that opened this target. Top-level targets return `null`.
|
|
|
|
*/
|
2022-05-31 14:34:16 +00:00
|
|
|
opener(): Target | undefined {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {openerId} = this.#targetInfo;
|
2022-06-14 11:55:35 +00:00
|
|
|
if (!openerId) {
|
|
|
|
return;
|
|
|
|
}
|
2018-06-01 00:06:29 +00:00
|
|
|
return this.browser()._targets.get(openerId);
|
|
|
|
}
|
|
|
|
|
2020-06-22 12:57:04 +00:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2020-04-29 14:18:09 +00:00
|
|
|
_targetInfoChanged(targetInfo: Protocol.Target.TargetInfo): void {
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#targetInfo = targetInfo;
|
2018-02-26 20:10:06 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
if (
|
|
|
|
!this._isInitialized &&
|
2022-06-13 09:16:25 +00:00
|
|
|
(!this._isPageTargetCallback(this.#targetInfo) ||
|
|
|
|
this.#targetInfo.url !== '')
|
2020-05-07 10:54:55 +00:00
|
|
|
) {
|
2018-02-26 20:10:06 +00:00
|
|
|
this._isInitialized = true;
|
|
|
|
this._initializedCallback(true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|