puppeteer/lib/Navigator.js

126 lines
4.3 KiB
JavaScript
Raw Normal View History

/**
* Copyright 2017 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.
*/
class Navigator {
2017-06-21 20:51:06 +00:00
/**
2017-06-21 20:58:49 +00:00
* @param {!Connection} client
* @param {!Object=} options
*/
constructor(client, options = {}) {
2017-06-21 20:51:06 +00:00
this._client = client;
this._minTime = typeof options.minTime === 'number' ? options.minTime : 0;
this._maxTime = typeof options.maxTime === 'number' ? options.maxTime : 30000;
this._idleTime = typeof options.networkIdleTimeout === 'number' ? options.networkIdleTimeout : 1000;
this._idleInflight = typeof options.networkIdleInflight === 'number' ? options.networkIdleInflight : 2;
this._waitFor = typeof options.waitFor === 'string' ? options.waitFor : 'load';
2017-06-21 20:51:06 +00:00
this._inflightRequests = 0;
2017-06-29 19:28:05 +00:00
console.assert(this._waitFor === 'load' || this._waitFor === 'networkidle', 'Unknown value for options.waitFor: ' + this._waitFor);
if (this._waitFor === 'networkidle') {
2017-06-29 19:28:05 +00:00
client.on('Network.requestWillBeSent', event => this._onLoadingStarted(event));
client.on('Network.loadingFinished', event => this._onLoadingCompleted(event));
client.on('Network.loadingFailed', event => this._onLoadingCompleted(event));
client.on('Network.webSocketCreated', event => this._onLoadingStarted(event));
client.on('Network.webSocketClosed', event => this._onLoadingCompleted(event));
}
2017-06-21 20:51:06 +00:00
}
2017-06-21 20:51:06 +00:00
/**
2017-06-21 20:58:49 +00:00
* @param {string} url
* @param {string=} referrer
*/
2017-06-21 20:51:06 +00:00
async navigate(url, referrer) {
this._requestIds = new Set();
2017-06-21 20:51:06 +00:00
this._navigationStartTime = Date.now();
this._idleReached = false;
let navigationComplete;
let navigationFailure = new Promise(fulfill => this._client.once('Security.certificateError', fulfill)).then(() => false);
2017-06-29 19:28:05 +00:00
if (this._waitFor === 'load')
navigationComplete = new Promise(fulfill => this._client.once('Page.loadEventFired', fulfill));
else
navigationComplete = new Promise(fulfill => this._navigationLoadCallback = fulfill);
2017-06-21 20:51:06 +00:00
this._inflightRequests = 0;
this._minimumTimer = setTimeout(this._completeNavigation.bind(this, false), this._minTime);
this._maximumTimer = setTimeout(this._completeNavigation.bind(this, true), this._maxTime);
this._idleTimer = setTimeout(this._onIdleReached.bind(this), this._idleTime);
2017-06-21 20:51:06 +00:00
// Await for the command to throw exception in case of illegal arguments.
try {
await this._client.send('Page.navigate', {url, referrer});
} catch (e) {
return false;
}
return await Promise.race([navigationComplete.then(() => true), navigationFailure]).then(retVal => {
clearTimeout(this._idleTimer);
clearTimeout(this._minimumTimer);
clearTimeout(this._maximumTimer);
return retVal;
});
2017-06-21 20:51:06 +00:00
}
/**
* @param {!Object} event
*/
_onLoadingStarted(event) {
this._requestIds.add(event.requestId);
if (!event.redirectResponse)
++this._inflightRequests;
if (this._inflightRequests > this._idleInflight) {
clearTimeout(this._idleTimer);
this._idleTimer = null;
}
}
/**
* @param {!Object} event
*/
2017-06-21 20:51:06 +00:00
_onLoadingCompleted(event) {
if (!this._requestIds.has(event.requestId))
2017-06-21 20:51:06 +00:00
return;
--this._inflightRequests;
if (this._inflightRequests <= this._idleInflight && !this._idleTimer)
this._idleTimer = setTimeout(this._onIdleReached.bind(this), this._idleTime);
}
_onIdleReached() {
this._idleReached = true;
2017-06-21 20:51:06 +00:00
this._completeNavigation(false);
}
2017-06-21 20:51:06 +00:00
/**
2017-06-21 20:58:49 +00:00
* @param {boolean} force
*/
2017-06-21 20:51:06 +00:00
_completeNavigation(force) {
if (!this._navigationLoadCallback)
return;
const elapsedTime = Date.now() - this._navigationStartTime;
if ((elapsedTime >= this._minTime && this._idleReached) || force) {
2017-06-21 20:51:06 +00:00
this._navigationLoadCallback();
this._navigationLoadCallback = null;
}
2017-06-21 20:51:06 +00:00
}
}
module.exports = Navigator;