feat(page): introduce "Popup" event (#3661)

This patch adds a new "popup" event for page.
"Popup" event is fired whenever page opens another page with
given opener.
This commit is contained in:
Andrey Lushnikov
2018-12-12 17:09:42 -08:00
committed by GitHub
parent c90392bdf5
commit 8aaca4eb1e
4 changed files with 83 additions and 1 deletions

View File

@@ -1192,6 +1192,7 @@ Page.Events = {
FrameNavigated: 'framenavigated',
Load: 'load',
Metrics: 'metrics',
Popup: 'popup',
WorkerCreated: 'workercreated',
WorkerDestroyed: 'workerdestroyed',
};

View File

@@ -20,7 +20,19 @@ class Target {
this._screenshotTaskQueue = screenshotTaskQueue;
/** @type {?Promise<!Puppeteer.Page>} */
this._pagePromise = null;
this._initializedPromise = new Promise(fulfill => this._initializedCallback = fulfill);
this._initializedPromise = new Promise(fulfill => this._initializedCallback = fulfill).then(async success => {
if (!success)
return false;
const opener = this.opener();
if (!opener || !opener._pagePromise || this.type() !== 'page')
return true;
const openerPage = await opener._pagePromise;
if (!openerPage.listenerCount(Page.Events.Popup))
return true;
const popupPage = await this.page();
openerPage.emit(Page.Events.Popup, popupPage);
return true;
});
this._isClosedPromise = new Promise(fulfill => this._closedCallback = fulfill);
this._isInitialized = this._targetInfo.type !== 'page' || this._targetInfo.url !== '';
if (this._isInitialized)