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.
|
|
|
|
*/
|
|
|
|
|
2020-07-06 10:34:55 +00:00
|
|
|
import { Page, PageEmittedEvents } from './Page';
|
2020-05-29 11:57:54 +00:00
|
|
|
import { WebWorker } from './WebWorker';
|
2020-05-07 10:54:55 +00:00
|
|
|
import { CDPSession } from './Connection';
|
|
|
|
import { Browser, BrowserContext } from './Browser';
|
2020-06-04 10:47:13 +00:00
|
|
|
import { Viewport } from './PuppeteerViewport';
|
2020-07-10 10:51:52 +00:00
|
|
|
import { Protocol } from 'devtools-protocol';
|
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 {
|
2020-06-22 12:57:04 +00:00
|
|
|
private _targetInfo: Protocol.Target.TargetInfo;
|
|
|
|
private _browserContext: BrowserContext;
|
|
|
|
|
|
|
|
private _sessionFactory: () => Promise<CDPSession>;
|
|
|
|
private _ignoreHTTPSErrors: boolean;
|
|
|
|
private _defaultViewport?: Viewport;
|
|
|
|
private _pagePromise?: Promise<Page>;
|
|
|
|
private _workerPromise?: Promise<WebWorker>;
|
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2020-04-29 14:18:09 +00:00
|
|
|
_initializedPromise: Promise<boolean>;
|
2020-06-22 12:57:04 +00:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2020-04-29 14:18:09 +00:00
|
|
|
_initializedCallback: (x: boolean) => void;
|
2020-06-22 12:57:04 +00:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2020-04-29 14:18:09 +00:00
|
|
|
_isClosedPromise: Promise<boolean>;
|
2020-06-22 12:57:04 +00:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2020-04-29 14:18:09 +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;
|
2020-04-29 14:18:09 +00:00
|
|
|
|
2020-06-22 12:57:04 +00:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2020-05-07 10:54:55 +00:00
|
|
|
constructor(
|
|
|
|
targetInfo: Protocol.Target.TargetInfo,
|
|
|
|
browserContext: BrowserContext,
|
|
|
|
sessionFactory: () => Promise<CDPSession>,
|
|
|
|
ignoreHTTPSErrors: boolean,
|
2020-05-07 13:49:42 +00:00
|
|
|
defaultViewport: Viewport | null
|
2020-05-07 10:54:55 +00:00
|
|
|
) {
|
2018-02-26 20:10:06 +00:00
|
|
|
this._targetInfo = targetInfo;
|
2018-05-10 20:26:08 +00:00
|
|
|
this._browserContext = browserContext;
|
2018-02-26 20:10:06 +00:00
|
|
|
this._targetId = targetInfo.targetId;
|
|
|
|
this._sessionFactory = sessionFactory;
|
|
|
|
this._ignoreHTTPSErrors = ignoreHTTPSErrors;
|
2018-08-01 23:23:03 +00:00
|
|
|
this._defaultViewport = defaultViewport;
|
2018-02-26 20:10:06 +00:00
|
|
|
/** @type {?Promise<!Puppeteer.Page>} */
|
|
|
|
this._pagePromise = null;
|
2020-05-29 11:57:54 +00:00
|
|
|
/** @type {?Promise<!WebWorker>} */
|
2019-05-10 00:29:18 +00:00
|
|
|
this._workerPromise = null;
|
2020-05-07 10:54:55 +00:00
|
|
|
this._initializedPromise = new Promise<boolean>(
|
|
|
|
(fulfill) => (this._initializedCallback = fulfill)
|
|
|
|
).then(async (success) => {
|
|
|
|
if (!success) return false;
|
2018-12-13 01:09:42 +00:00
|
|
|
const opener = this.opener();
|
|
|
|
if (!opener || !opener._pagePromise || this.type() !== 'page')
|
|
|
|
return true;
|
|
|
|
const openerPage = await opener._pagePromise;
|
2020-07-06 10:34:55 +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;
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
this._isClosedPromise = new Promise<boolean>(
|
|
|
|
(fulfill) => (this._closedCallback = fulfill)
|
|
|
|
);
|
|
|
|
this._isInitialized =
|
|
|
|
this._targetInfo.type !== 'page' || this._targetInfo.url !== '';
|
|
|
|
if (this._isInitialized) this._initializedCallback(true);
|
2018-02-26 20:10:06 +00:00
|
|
|
}
|
|
|
|
|
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> {
|
2018-02-26 20:10:06 +00:00
|
|
|
return this._sessionFactory();
|
|
|
|
}
|
|
|
|
|
2020-06-22 12:57:04 +00:00
|
|
|
/**
|
|
|
|
* If the target is not of type `"page"` or `"background_page"`, returns `null`.
|
|
|
|
*/
|
2020-05-05 12:53:22 +00:00
|
|
|
async page(): Promise<Page | null> {
|
2020-05-07 10:54:55 +00:00
|
|
|
if (
|
|
|
|
(this._targetInfo.type === 'page' ||
|
2020-05-29 08:46:44 +00:00
|
|
|
this._targetInfo.type === 'background_page' ||
|
|
|
|
this._targetInfo.type === 'webview') &&
|
2020-05-07 10:54:55 +00:00
|
|
|
!this._pagePromise
|
|
|
|
) {
|
|
|
|
this._pagePromise = this._sessionFactory().then((client) =>
|
|
|
|
Page.create(
|
|
|
|
client,
|
|
|
|
this,
|
|
|
|
this._ignoreHTTPSErrors,
|
2020-05-07 13:49:42 +00:00
|
|
|
this._defaultViewport
|
2020-05-07 10:54:55 +00:00
|
|
|
)
|
|
|
|
);
|
2018-02-26 20:10:06 +00:00
|
|
|
}
|
|
|
|
return this._pagePromise;
|
|
|
|
}
|
|
|
|
|
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 (
|
|
|
|
this._targetInfo.type !== 'service_worker' &&
|
|
|
|
this._targetInfo.type !== 'shared_worker'
|
|
|
|
)
|
2019-05-10 00:29:18 +00:00
|
|
|
return null;
|
|
|
|
if (!this._workerPromise) {
|
2019-08-15 04:43:18 +00:00
|
|
|
// TODO(einbinder): Make workers send their console logs.
|
2020-05-07 10:54:55 +00:00
|
|
|
this._workerPromise = this._sessionFactory().then(
|
|
|
|
(client) =>
|
2020-05-29 11:57:54 +00:00
|
|
|
new WebWorker(
|
2020-05-07 10:54:55 +00:00
|
|
|
client,
|
|
|
|
this._targetInfo.url,
|
|
|
|
() => {} /* consoleAPICalled */,
|
|
|
|
() => {} /* exceptionThrown */
|
|
|
|
)
|
|
|
|
);
|
2019-05-10 00:29:18 +00:00
|
|
|
}
|
|
|
|
return this._workerPromise;
|
|
|
|
}
|
|
|
|
|
2020-04-29 14:18:09 +00:00
|
|
|
url(): string {
|
2018-02-26 20:10:06 +00:00
|
|
|
return this._targetInfo.url;
|
|
|
|
}
|
|
|
|
|
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' {
|
2018-02-26 20:10:06 +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'
|
2020-05-07 10:54:55 +00:00
|
|
|
)
|
2018-02-26 20:10:06 +00:00
|
|
|
return type;
|
|
|
|
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 {
|
2018-05-10 20:26:08 +00:00
|
|
|
return this._browserContext.browser();
|
2018-04-17 17:37:17 +00:00
|
|
|
}
|
|
|
|
|
2020-04-29 14:18:09 +00:00
|
|
|
browserContext(): BrowserContext {
|
2018-05-10 20:26:08 +00:00
|
|
|
return this._browserContext;
|
|
|
|
}
|
|
|
|
|
2020-06-22 12:57:04 +00:00
|
|
|
/**
|
|
|
|
* Get the target that opened this target. Top-level targets return `null`.
|
|
|
|
*/
|
2020-04-29 14:18:09 +00:00
|
|
|
opener(): Target | null {
|
2020-05-07 10:54:55 +00:00
|
|
|
const { openerId } = this._targetInfo;
|
|
|
|
if (!openerId) return null;
|
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 {
|
2018-02-26 20:10:06 +00:00
|
|
|
this._targetInfo = targetInfo;
|
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
if (
|
|
|
|
!this._isInitialized &&
|
|
|
|
(this._targetInfo.type !== 'page' || this._targetInfo.url !== '')
|
|
|
|
) {
|
2018-02-26 20:10:06 +00:00
|
|
|
this._isInitialized = true;
|
|
|
|
this._initializedCallback(true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|