mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
Implement browser 'ignoreHTTPSErrors' option. (#177)
This patch implements Browser 'ignoreHTTPSErrors' option. Fixes #84.
This commit is contained in:
@@ -19,10 +19,12 @@ const helper = require('./helper');
|
||||
class NavigatorWatcher {
|
||||
/**
|
||||
* @param {!Connection} client
|
||||
* @param {boolean} ignoreHTTPSErrors
|
||||
* @param {!Object=} options
|
||||
*/
|
||||
constructor(client, options = {}) {
|
||||
constructor(client, ignoreHTTPSErrors, options = {}) {
|
||||
this._client = client;
|
||||
this._ignoreHTTPSErrors = ignoreHTTPSErrors;
|
||||
this._timeout = typeof options['timeout'] === 'number' ? options['timeout'] : 30000;
|
||||
this._idleTime = typeof options['networkIdleTimeout'] === 'number' ? options['networkIdleTimeout'] : 1000;
|
||||
this._idleInflight = typeof options['networkIdleInflight'] === 'number' ? options['networkIdleInflight'] : 2;
|
||||
@@ -30,7 +32,6 @@ class NavigatorWatcher {
|
||||
console.assert(this._waitUntil === 'load' || this._waitUntil === 'networkidle', 'Unknown value for options.waitUntil: ' + this._waitUntil);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return {!Promise<!Map<string, !Response>>}
|
||||
*/
|
||||
@@ -38,34 +39,40 @@ class NavigatorWatcher {
|
||||
this._inflightRequests = 0;
|
||||
this._requestIds = new Set();
|
||||
|
||||
this._eventListeners = [
|
||||
helper.addEventListener(this._client, 'Network.requestWillBeSent', this._onLoadingStarted.bind(this)),
|
||||
helper.addEventListener(this._client, 'Network.loadingFinished', this._onLoadingCompleted.bind(this)),
|
||||
helper.addEventListener(this._client, 'Network.loadingFailed', this._onLoadingCompleted.bind(this)),
|
||||
helper.addEventListener(this._client, 'Network.webSocketCreated', this._onLoadingStarted.bind(this)),
|
||||
helper.addEventListener(this._client, 'Network.webSocketClosed', this._onLoadingCompleted.bind(this)),
|
||||
];
|
||||
|
||||
let certificateError = new Promise(fulfill => {
|
||||
this._eventListeners.push(helper.addEventListener(this._client, 'Security.certificateError', fulfill));
|
||||
}).then(error => 'SSL Certificate error: ' + error.errorType);
|
||||
|
||||
let networkIdle = new Promise(fulfill => this._networkIdleCallback = fulfill).then(() => null);
|
||||
let loadEventFired = new Promise(fulfill => {
|
||||
this._eventListeners.push(helper.addEventListener(this._client, 'Page.loadEventFired', fulfill));
|
||||
}).then(() => null);
|
||||
this._eventListeners = [];
|
||||
|
||||
let watchdog = new Promise(fulfill => this._maximumTimer = setTimeout(fulfill, this._timeout))
|
||||
.then(() => 'Navigation Timeout Exceeded: ' + this._timeout + 'ms exceeded');
|
||||
let navigationPromises = [watchdog];
|
||||
|
||||
try {
|
||||
// Await for the command to throw exception in case of illegal arguments.
|
||||
const error = await Promise.race([certificateError, watchdog, this._waitUntil === 'load' ? loadEventFired : networkIdle]);
|
||||
if (error)
|
||||
throw new Error(error);
|
||||
} finally {
|
||||
this._cleanup();
|
||||
if (!this._ignoreHTTPSErrors) {
|
||||
let certificateError = new Promise(fulfill => {
|
||||
this._eventListeners.push(helper.addEventListener(this._client, 'Security.certificateError', fulfill));
|
||||
}).then(error => 'SSL Certificate error: ' + error.errorType);
|
||||
navigationPromises.push(certificateError);
|
||||
}
|
||||
|
||||
if (this._waitUntil === 'load') {
|
||||
let loadEventFired = new Promise(fulfill => {
|
||||
this._eventListeners.push(helper.addEventListener(this._client, 'Page.loadEventFired', fulfill));
|
||||
}).then(() => null);
|
||||
navigationPromises.push(loadEventFired);
|
||||
} else {
|
||||
this._eventListeners.push(...[
|
||||
helper.addEventListener(this._client, 'Network.requestWillBeSent', this._onLoadingStarted.bind(this)),
|
||||
helper.addEventListener(this._client, 'Network.loadingFinished', this._onLoadingCompleted.bind(this)),
|
||||
helper.addEventListener(this._client, 'Network.loadingFailed', this._onLoadingCompleted.bind(this)),
|
||||
helper.addEventListener(this._client, 'Network.webSocketCreated', this._onLoadingStarted.bind(this)),
|
||||
helper.addEventListener(this._client, 'Network.webSocketClosed', this._onLoadingCompleted.bind(this)),
|
||||
]);
|
||||
let networkIdle = new Promise(fulfill => this._networkIdleCallback = fulfill).then(() => null);
|
||||
navigationPromises.push(networkIdle);
|
||||
}
|
||||
|
||||
const error = await Promise.race(navigationPromises);
|
||||
this._cleanup();
|
||||
if (error)
|
||||
throw new Error(error);
|
||||
}
|
||||
|
||||
cancel() {
|
||||
@@ -97,9 +104,6 @@ class NavigatorWatcher {
|
||||
this._idleTimer = setTimeout(this._networkIdleCallback, this._idleTime);
|
||||
}
|
||||
|
||||
_init() {
|
||||
}
|
||||
|
||||
_cleanup() {
|
||||
helper.removeEventListeners(this._eventListeners);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user