mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
feat(Page): add option to run 'beforeunload' when closing the page (#2478)
Today, `page.close()` method doesn't run page's beforeunload listeners. This way users can be sure that `page.close()` actually closes the page. This patch adds an optional `runBeforeUnload` option to the `page.close()` method that would run beforeunload listeners. Note: running beforeunload handlers might cancel page closing. Fixes #2386.
This commit is contained in:
14
lib/Page.js
14
lib/Page.js
@@ -823,10 +823,18 @@ class Page extends EventEmitter {
|
||||
return this.mainFrame().title();
|
||||
}
|
||||
|
||||
async close() {
|
||||
/**
|
||||
* @param {!{runBeforeUnload: (boolean|undefined)}=} options
|
||||
*/
|
||||
async close(options = {runBeforeUnload: undefined}) {
|
||||
console.assert(!!this._client._connection, 'Protocol error: Connection closed. Most likely the page has been closed.');
|
||||
await this._client._connection.send('Target.closeTarget', { targetId: this._target._targetId });
|
||||
await this._target._isClosedPromise;
|
||||
const runBeforeUnload = !!options.runBeforeUnload;
|
||||
if (runBeforeUnload) {
|
||||
await this._client.send('Page.close');
|
||||
} else {
|
||||
await this._client._connection.send('Target.closeTarget', { targetId: this._target._targetId });
|
||||
await this._target._isClosedPromise;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user