puppeteer/lib/FrameManager.js

353 lines
9.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.
*/
let EventEmitter = require('events');
let helper = require('./helper');
class FrameManager extends EventEmitter {
2017-06-21 20:51:06 +00:00
/**
2017-06-21 20:58:49 +00:00
* @param {!Connection} client
* @return {!Promise<!FrameManager>}
2017-06-21 20:58:49 +00:00
*/
2017-06-21 20:51:06 +00:00
static async create(client) {
let mainFramePayload = await client.send('Page.getResourceTree');
2017-06-21 20:51:06 +00:00
return new FrameManager(client, mainFramePayload.frameTree);
}
2017-06-21 20:51:06 +00:00
/**
2017-06-21 20:58:49 +00:00
* @param {!Connection} client
* @param {!Object} frameTree
*/
2017-06-21 20:51:06 +00:00
constructor(client, frameTree) {
super();
this._client = client;
/** @type {!Map<string, !Frame>} */
this._frames = new Map();
this._mainFrame = this._addFramesRecursively(null, frameTree);
/** @type {!Map<string, string>} */
this._frameIdToExecutionContextId = new Map();
this._client.on('Page.frameAttached', event => this._onFrameAttached(event.frameId, event.parentFrameId));
this._client.on('Page.frameNavigated', event => this._onFrameNavigated(event.frame));
this._client.on('Page.frameDetached', event => this._onFrameDetached(event.frameId));
this._client.on('Runtime.executionContextCreated', event => this._onExecutionContextCreated(event.context));
2017-06-21 20:51:06 +00:00
}
/**
2017-06-21 20:58:49 +00:00
* @return {!Frame}
*/
2017-06-21 20:51:06 +00:00
mainFrame() {
return this._mainFrame;
}
2017-06-21 20:51:06 +00:00
/**
2017-06-21 20:58:49 +00:00
* @return {!Array<!Frame>}
*/
2017-06-21 20:51:06 +00:00
frames() {
return Array.from(this._frames.values());
}
2017-06-21 20:51:06 +00:00
/**
2017-06-21 20:58:49 +00:00
* @param {string} frameId
* @param {?string} parentFrameId
* @return {?Frame}
*/
_onFrameAttached(frameId, parentFrameId) {
2017-06-21 20:51:06 +00:00
if (this._frames.has(frameId))
return;
if (!parentFrameId) {
// Navigation to the new backend process.
this._navigateFrame(this._mainFrame, frameId, null);
return;
}
let parentFrame = this._frames.get(parentFrameId);
let frame = new Frame(this, parentFrame, frameId, null);
2017-06-21 20:51:06 +00:00
this._frames.set(frame._id, frame);
this.emit(FrameManager.Events.FrameAttached, frame);
}
/**
2017-06-21 20:58:49 +00:00
* @param {!Object} framePayload
*/
_onFrameNavigated(framePayload) {
let frame = this._frames.get(framePayload.id);
2017-06-21 20:51:06 +00:00
if (!frame) {
// Simulate missed "frameAttached" for a main frame navigation to the new backend process.
console.assert(!framePayload.parentId, 'Main frame shouldn\'t have parent frame id.');
frame = this._mainFrame;
}
2017-06-21 20:51:06 +00:00
this._navigateFrame(frame, framePayload.id, framePayload);
}
2017-06-21 20:51:06 +00:00
/**
2017-06-21 20:58:49 +00:00
* @param {string} frameId
*/
_onFrameDetached(frameId) {
let frame = this._frames.get(frameId);
2017-06-21 20:51:06 +00:00
if (frame)
this._removeFramesRecursively(frame);
}
_onExecutionContextCreated(context) {
if (context.auxData && context.auxData.isDefault && context.auxData.frameId)
this._frameIdToExecutionContextId.set(context.auxData.frameId, context.id);
}
2017-06-21 20:51:06 +00:00
/**
2017-06-21 20:58:49 +00:00
* @param {!Frame} frame
* @param {string} newFrameId
* @param {?Object} newFramePayload
*/
2017-06-21 20:51:06 +00:00
_navigateFrame(frame, newFrameId, newFramePayload) {
// Detach all child frames first.
for (let child of frame.childFrames())
2017-06-21 20:51:06 +00:00
this._removeFramesRecursively(child);
this._frames.delete(frame._id, frame);
this._frameIdToExecutionContextId.delete(frame._id);
2017-06-21 20:51:06 +00:00
frame._id = newFrameId;
frame._adoptPayload(newFramePayload);
this._frames.set(newFrameId, frame);
this.emit(FrameManager.Events.FrameNavigated, frame);
}
/**
2017-06-21 20:58:49 +00:00
* @param {?Frame} parentFrame
* @param {!Object} frameTreePayload
* @return {!Frame}
*/
2017-06-21 20:51:06 +00:00
_addFramesRecursively(parentFrame, frameTreePayload) {
let framePayload = frameTreePayload.frame;
let frame = new Frame(this, parentFrame, framePayload.id, framePayload);
2017-06-21 20:51:06 +00:00
this._frames.set(frame._id, frame);
for (let i = 0; frameTreePayload.childFrames && i < frameTreePayload.childFrames.length; ++i)
2017-06-21 20:51:06 +00:00
this._addFramesRecursively(frame, frameTreePayload.childFrames[i]);
return frame;
}
2017-06-21 20:51:06 +00:00
/**
2017-06-21 20:58:49 +00:00
* @param {!Frame} frame
*/
2017-06-21 20:51:06 +00:00
_removeFramesRecursively(frame) {
for (let child of frame.childFrames())
2017-06-21 20:51:06 +00:00
this._removeFramesRecursively(child);
frame._detach();
this._frames.delete(frame._id);
this._frameIdToExecutionContextId.delete(frame._id);
2017-06-21 20:51:06 +00:00
this.emit(FrameManager.Events.FrameDetached, frame);
}
/**
* @param {!Frame} frame
* @param {function()} fun
* @param {!Array<*>} args
* @return {!Promise<(!Object|undefined)>}
*/
async _evaluateOnFrame(frame, fun, ...args) {
let contextId = undefined;
if (!frame.isMainFrame()) {
contextId = this._frameIdToExecutionContextId.get(frame._id);
console.assert(contextId, 'Frame does not have default context to evaluate in!');
}
let syncExpression = helper.evaluationString(fun, ...args);
let expression = `Promise.resolve(${syncExpression})`;
let { exceptionDetails, result: remoteObject } = await this._client.send('Runtime.evaluate', { expression, contextId, returnByValue: false, awaitPromise: true });
if (exceptionDetails) {
let message = await helper.getExceptionMessage(this._client, exceptionDetails);
throw new Error('Evaluation failed: ' + message);
}
if (remoteObject.unserializableValue) {
switch (remoteObject.unserializableValue) {
case '-0':
return -0;
case 'NaN':
return NaN;
case 'Infinity':
return Infinity;
case '-Infinity':
return -Infinity;
default:
throw new Error('Unsupported unserializable value: ' + remoteObject.unserializableValue);
}
}
if (!remoteObject.objectId)
return remoteObject.value;
let response = await this._client.send('Runtime.callFunctionOn', {
objectId: remoteObject.objectId,
functionDeclaration: 'function() { return this; }',
returnByValue: true,
});
return response.result.value;
}
/**
* @param {string} selector
* @param {!Frame} frame
* @return {!Promise<undefined>}
*/
async _waitForInFrame(selector, frame) {
let code = selector => new Promise((fulfill, reject) => {
if (document.querySelector(selector)) {
fulfill();
return;
}
new MutationObserver((mutations, observer) => {
for (let mutation of mutations) {
for (let node of mutation.addedNodes) {
if (node.matches(selector)) {
observer.disconnect();
fulfill();
return;
}
}
}
}).observe(document.documentElement, {
childList: true,
subtree: true
});
});
await this._client.send('Runtime.evaluate', {
expression: helper.evaluationString(code, selector),
awaitPromise: true,
returnByValue: true,
});
}
}
/** @enum {string} */
FrameManager.Events = {
2017-06-21 20:51:06 +00:00
FrameAttached: 'frameattached',
FrameNavigated: 'framenavigated',
FrameDetached: 'framedetached'
};
/**
* @unrestricted
*/
class Frame {
2017-06-21 20:51:06 +00:00
/**
* @param {!FrameManager} frameManager
2017-06-21 20:58:49 +00:00
* @param {?Frame} parentFrame
* @param {string} frameId
* @param {?Object} payload
*/
constructor(frameManager, parentFrame, frameId, payload) {
this._frameManager = frameManager;
2017-06-21 20:51:06 +00:00
this._parentFrame = parentFrame;
this._url = '';
this._id = frameId;
this._adoptPayload(payload);
/** @type {!Set<!Frame>} */
this._childFrames = new Set();
if (this._parentFrame)
this._parentFrame._childFrames.add(this);
}
/**
* @param {function()} fun
* @param {!Array<*>} args
* @return {!Promise<(!Object|undefined)>}
*/
async evaluate(fun, ...args) {
return this._frameManager._evaluateOnFrame(this, fun, ...args);
}
2017-06-21 20:51:06 +00:00
/**
2017-06-21 20:58:49 +00:00
* @return {string}
*/
2017-06-21 20:51:06 +00:00
name() {
return this._name || '';
}
2017-06-21 20:51:06 +00:00
/**
2017-06-21 20:58:49 +00:00
* @return {string}
*/
2017-06-21 20:51:06 +00:00
url() {
return this._url;
}
2017-06-21 20:51:06 +00:00
/**
2017-06-21 20:58:49 +00:00
* @return {string}
*/
2017-06-21 20:51:06 +00:00
securityOrigin() {
return this._securityOrigin;
}
2017-06-21 20:51:06 +00:00
/**
2017-06-21 20:58:49 +00:00
* @return {?Frame}
*/
2017-06-21 20:51:06 +00:00
parentFrame() {
return this._parentFrame;
}
2017-06-21 20:51:06 +00:00
/**
2017-06-21 20:58:49 +00:00
* @return {!Array.<!Frame>}
*/
2017-06-21 20:51:06 +00:00
childFrames() {
return Array.from(this._childFrames);
}
2017-06-21 20:51:06 +00:00
/**
2017-06-21 20:58:49 +00:00
* @return {boolean}
*/
2017-06-21 20:51:06 +00:00
isMainFrame() {
return !this._detached && !this._parentFrame;
}
2017-06-21 20:51:06 +00:00
/**
2017-06-21 20:58:49 +00:00
* @return {boolean}
*/
2017-06-21 20:51:06 +00:00
isDetached() {
return this._detached;
}
/**
* @param {string} selector
* @return {!Promise<undefined>}
*/
async waitFor(selector) {
await this._frameManager._waitForInFrame(selector, this);
}
2017-06-21 20:51:06 +00:00
/**
2017-06-21 20:58:49 +00:00
* @param {?Object} framePayload
*/
2017-06-21 20:51:06 +00:00
_adoptPayload(framePayload) {
framePayload = framePayload || {
name: '',
url: '',
securityOrigin: '',
mimeType: ''
};
this._name = framePayload.name;
this._url = framePayload.url;
this._securityOrigin = framePayload.securityOrigin;
this._mimeType = framePayload.mimeType;
}
_detach() {
this._detached = true;
if (this._parentFrame)
this._parentFrame._childFrames.delete(this);
this._parentFrame = null;
}
}
module.exports = FrameManager;