mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
Support Page.{Alert,Confirm,Prompt,BeforeUnload} events
This patch adds Page events to fire when javascript dialogs get opened.
This commit is contained in:
parent
de9605a8b0
commit
bdf895bed6
21
lib/Page.js
21
lib/Page.js
@ -60,7 +60,7 @@ class Page extends EventEmitter {
|
||||
client.on('Network.responseReceived', event => this.emit(Page.Events.ResponseReceived, event.response));
|
||||
client.on('Network.loadingFailed', event => this.emit(Page.Events.ResourceLoadingFailed, event));
|
||||
client.on('Runtime.consoleAPICalled', event => this._onConsoleAPI(event));
|
||||
client.on('Page.javascriptDialogOpening', dialog => this.emit(Page.Events.Dialog, dialog));
|
||||
client.on('Page.javascriptDialogOpening', event => this._onDialog(event));
|
||||
client.on('Runtime.exceptionThrown', exception => this._handleException(exception.exceptionDetails));
|
||||
}
|
||||
|
||||
@ -207,6 +207,20 @@ class Page extends EventEmitter {
|
||||
this.emit(Page.Events.ConsoleMessage, values.join(' '));
|
||||
}
|
||||
|
||||
_onDialog(event) {
|
||||
var eventType = null;
|
||||
if (event.type === 'alert')
|
||||
eventType = Page.Events.Alert;
|
||||
else if (event.type === 'confirm')
|
||||
eventType = Page.Events.Confirm;
|
||||
else if (event.type === 'prompt')
|
||||
eventType = Page.Events.Prompt;
|
||||
else if (event.type === 'beforeunload')
|
||||
eventType = Page.Events.BeforeUnload;
|
||||
if (eventType)
|
||||
this.emit(eventType, event.message);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {boolean} accept
|
||||
* @param {string} promptText
|
||||
@ -416,9 +430,12 @@ Page.ScreenshotTypes = {
|
||||
};
|
||||
|
||||
Page.Events = {
|
||||
Alert: 'alert',
|
||||
BeforeUnload: 'beforeunload',
|
||||
Confirm: 'confirm',
|
||||
ConsoleMessage: 'consolemessage',
|
||||
Dialog: 'dialog',
|
||||
Exception: 'exception',
|
||||
Prompt: 'prompt',
|
||||
ResourceLoadingFailed: 'resourceloadingfailed',
|
||||
ResponseReceived: 'responsereceived',
|
||||
};
|
||||
|
@ -57,14 +57,16 @@ class WebPage {
|
||||
|
||||
this.libraryPath = path.dirname(scriptPath);
|
||||
|
||||
this._onConfirm = undefined;
|
||||
this._onConfirmCallback = undefined;
|
||||
this._onAlertCallback = undefined;
|
||||
this._onError = noop;
|
||||
|
||||
this._pageEvents = new AsyncEmitter(this._page);
|
||||
this._pageEvents.on(PageEvents.ResponseReceived, response => this._onResponseReceived(response));
|
||||
this._pageEvents.on(PageEvents.ResourceLoadingFailed, event => (this.onResourceError || noop).call(null, event));
|
||||
this._pageEvents.on(PageEvents.ConsoleMessage, msg => (this.onConsoleMessage || noop).call(null, msg));
|
||||
this._pageEvents.on(PageEvents.Dialog, dialog => this._onDialog(dialog));
|
||||
this._pageEvents.on(PageEvents.Confirm, message => this._onConfirm(message));
|
||||
this._pageEvents.on(PageEvents.Alert, message => this._onAlert(message));
|
||||
this._pageEvents.on(PageEvents.Exception, (exception, stack) => (this._onError || noop).call(null, exception, stack));
|
||||
}
|
||||
|
||||
@ -156,7 +158,7 @@ class WebPage {
|
||||
* @return {(function()|undefined)}
|
||||
*/
|
||||
get onConfirm() {
|
||||
return this._onConfirm;
|
||||
return this._onConfirmCallback;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -165,16 +167,45 @@ class WebPage {
|
||||
set onConfirm(handler) {
|
||||
if (typeof handler !== 'function')
|
||||
handler = undefined;
|
||||
this._onConfirm = handler;
|
||||
this._onConfirmCallback = handler;
|
||||
}
|
||||
|
||||
_onDialog(dialog) {
|
||||
if (!this._onConfirm)
|
||||
/**
|
||||
* @param {string} message
|
||||
*/
|
||||
_onConfirm(message) {
|
||||
if (!this._onConfirmCallback)
|
||||
return;
|
||||
var accept = this._onConfirm.call(null);
|
||||
var accept = this._onConfirmCallback.call(null, message);
|
||||
await(this._page.handleDialog(accept));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return {(function()|undefined)}
|
||||
*/
|
||||
get onAlert() {
|
||||
return this._onAlertCallback;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {function()} handler
|
||||
*/
|
||||
set onAlert(handler) {
|
||||
if (typeof handler !== 'function')
|
||||
handler = undefined;
|
||||
this._onAlertCallback = handler;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} message
|
||||
*/
|
||||
_onAlert(message) {
|
||||
if (!this._onAlertCallback)
|
||||
return;
|
||||
this._onAlertCallback.call(null, message);
|
||||
await(this._page.handleDialog(true));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return {string}
|
||||
*/
|
||||
|
Loading…
Reference in New Issue
Block a user