2017-10-06 22:35:02 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
const {helper} = require('./helper');
|
|
|
|
|
|
|
|
class ExecutionContext {
|
|
|
|
/**
|
2018-01-11 03:33:22 +00:00
|
|
|
* @param {!Puppeteer.CDPSession} client
|
2017-11-19 00:27:52 +00:00
|
|
|
* @param {!Object} contextPayload
|
2017-10-06 22:35:02 +00:00
|
|
|
* @param {function(*):!JSHandle} objectHandleFactory
|
2018-02-13 22:02:44 +00:00
|
|
|
* @param {?Puppeteer.Frame} frame
|
2017-10-06 22:35:02 +00:00
|
|
|
*/
|
2018-02-13 22:02:44 +00:00
|
|
|
constructor(client, contextPayload, objectHandleFactory, frame) {
|
2017-10-06 22:35:02 +00:00
|
|
|
this._client = client;
|
2018-02-13 22:02:44 +00:00
|
|
|
this._frame = frame;
|
2017-11-19 00:27:52 +00:00
|
|
|
this._contextId = contextPayload.id;
|
2017-10-06 22:35:02 +00:00
|
|
|
this._objectHandleFactory = objectHandleFactory;
|
|
|
|
}
|
|
|
|
|
2018-02-13 22:02:44 +00:00
|
|
|
/**
|
|
|
|
* @return {?Puppeteer.Frame}
|
|
|
|
*/
|
|
|
|
frame() {
|
|
|
|
return this._frame;
|
|
|
|
}
|
|
|
|
|
2017-10-06 22:35:02 +00:00
|
|
|
/**
|
2017-12-20 00:23:45 +00:00
|
|
|
* @param {Function|string} pageFunction
|
2017-10-10 06:14:09 +00:00
|
|
|
* @param {...*} args
|
2017-10-06 22:35:02 +00:00
|
|
|
* @return {!Promise<(!Object|undefined)>}
|
|
|
|
*/
|
|
|
|
async evaluate(pageFunction, ...args) {
|
|
|
|
const handle = await this.evaluateHandle(pageFunction, ...args);
|
2017-10-09 19:30:05 +00:00
|
|
|
const result = await handle.jsonValue().catch(error => undefined);
|
2017-10-06 22:35:02 +00:00
|
|
|
await handle.dispose();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-12-20 00:23:45 +00:00
|
|
|
* @param {Function|string} pageFunction
|
2017-10-10 06:14:09 +00:00
|
|
|
* @param {...*} args
|
2017-10-06 22:35:02 +00:00
|
|
|
* @return {!Promise<!JSHandle>}
|
|
|
|
*/
|
|
|
|
async evaluateHandle(pageFunction, ...args) {
|
|
|
|
if (helper.isString(pageFunction)) {
|
|
|
|
const contextId = this._contextId;
|
|
|
|
const expression = pageFunction;
|
|
|
|
const { exceptionDetails, result: remoteObject } = await this._client.send('Runtime.evaluate', { expression, contextId, returnByValue: false, awaitPromise: true});
|
|
|
|
if (exceptionDetails)
|
|
|
|
throw new Error('Evaluation failed: ' + helper.getExceptionMessage(exceptionDetails));
|
|
|
|
return this._objectHandleFactory(remoteObject);
|
|
|
|
}
|
|
|
|
|
|
|
|
const { exceptionDetails, result: remoteObject } = await this._client.send('Runtime.callFunctionOn', {
|
|
|
|
functionDeclaration: pageFunction.toString(),
|
|
|
|
executionContextId: this._contextId,
|
|
|
|
arguments: args.map(convertArgument.bind(this)),
|
|
|
|
returnByValue: false,
|
|
|
|
awaitPromise: true
|
|
|
|
});
|
|
|
|
if (exceptionDetails)
|
|
|
|
throw new Error('Evaluation failed: ' + helper.getExceptionMessage(exceptionDetails));
|
|
|
|
return this._objectHandleFactory(remoteObject);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {*} arg
|
|
|
|
* @return {*}
|
|
|
|
* @this {Frame}
|
|
|
|
*/
|
|
|
|
function convertArgument(arg) {
|
|
|
|
if (Object.is(arg, -0))
|
|
|
|
return { unserializableValue: '-0' };
|
|
|
|
if (Object.is(arg, Infinity))
|
|
|
|
return { unserializableValue: 'Infinity' };
|
|
|
|
if (Object.is(arg, -Infinity))
|
|
|
|
return { unserializableValue: '-Infinity' };
|
|
|
|
if (Object.is(arg, NaN))
|
|
|
|
return { unserializableValue: 'NaN' };
|
|
|
|
const objectHandle = arg && (arg instanceof JSHandle) ? arg : null;
|
|
|
|
if (objectHandle) {
|
|
|
|
if (objectHandle._context !== this)
|
|
|
|
throw new Error('JSHandles can be evaluated only in the context they were created!');
|
|
|
|
if (objectHandle._disposed)
|
|
|
|
throw new Error('JSHandle is disposed!');
|
|
|
|
if (objectHandle._remoteObject.unserializableValue)
|
|
|
|
return { unserializableValue: objectHandle._remoteObject.unserializableValue };
|
|
|
|
if (!objectHandle._remoteObject.objectId)
|
|
|
|
return { value: objectHandle._remoteObject.value };
|
|
|
|
return { objectId: objectHandle._remoteObject.objectId };
|
|
|
|
}
|
|
|
|
return { value: arg };
|
|
|
|
}
|
|
|
|
}
|
2017-10-11 21:41:20 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {!JSHandle} prototypeHandle
|
|
|
|
* @return {!Promise<!JSHandle>}
|
|
|
|
*/
|
|
|
|
async queryObjects(prototypeHandle) {
|
|
|
|
console.assert(!prototypeHandle._disposed, 'Prototype JSHandle is disposed!');
|
|
|
|
console.assert(prototypeHandle._remoteObject.objectId, 'Prototype JSHandle must not be referencing primitive value');
|
|
|
|
const response = await this._client.send('Runtime.queryObjects', {
|
|
|
|
prototypeObjectId: prototypeHandle._remoteObject.objectId
|
|
|
|
});
|
|
|
|
return this._objectHandleFactory(response.objects);
|
|
|
|
}
|
2017-10-06 22:35:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class JSHandle {
|
|
|
|
/**
|
|
|
|
* @param {!ExecutionContext} context
|
2018-01-11 03:33:22 +00:00
|
|
|
* @param {!Puppeteer.CDPSession} client
|
2017-10-06 22:35:02 +00:00
|
|
|
* @param {!Object} remoteObject
|
|
|
|
*/
|
|
|
|
constructor(context, client, remoteObject) {
|
|
|
|
this._context = context;
|
|
|
|
this._client = client;
|
|
|
|
this._remoteObject = remoteObject;
|
|
|
|
this._disposed = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return {!ExecutionContext}
|
|
|
|
*/
|
|
|
|
executionContext() {
|
|
|
|
return this._context;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} propertyName
|
|
|
|
* @return {!Promise<?JSHandle>}
|
|
|
|
*/
|
|
|
|
async getProperty(propertyName) {
|
|
|
|
const objectHandle = await this._context.evaluateHandle((object, propertyName) => {
|
|
|
|
const result = {__proto__: null};
|
|
|
|
result[propertyName] = object[propertyName];
|
|
|
|
return result;
|
|
|
|
}, this, propertyName);
|
|
|
|
const properties = await objectHandle.getProperties();
|
|
|
|
const result = properties.get(propertyName) || null;
|
|
|
|
await objectHandle.dispose();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-10-10 05:31:40 +00:00
|
|
|
* @return {!Promise<Map<string, !JSHandle>>}
|
2017-10-06 22:35:02 +00:00
|
|
|
*/
|
|
|
|
async getProperties() {
|
|
|
|
const response = await this._client.send('Runtime.getProperties', {
|
|
|
|
objectId: this._remoteObject.objectId,
|
|
|
|
ownProperties: true
|
|
|
|
});
|
|
|
|
const result = new Map();
|
|
|
|
for (const property of response.result) {
|
|
|
|
if (!property.enumerable)
|
|
|
|
continue;
|
|
|
|
result.set(property.name, this._context._objectHandleFactory(property.value));
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return {!Promise<?Object>}
|
|
|
|
*/
|
|
|
|
async jsonValue() {
|
|
|
|
if (this._remoteObject.objectId) {
|
2017-10-20 17:45:49 +00:00
|
|
|
const response = await this._client.send('Runtime.callFunctionOn', {
|
|
|
|
functionDeclaration: 'function() { return this; }',
|
|
|
|
objectId: this._remoteObject.objectId,
|
|
|
|
returnByValue: true,
|
|
|
|
awaitPromise: true,
|
|
|
|
});
|
|
|
|
return helper.valueFromRemoteObject(response.result);
|
2017-10-06 22:35:02 +00:00
|
|
|
}
|
|
|
|
return helper.valueFromRemoteObject(this._remoteObject);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-10-10 05:31:40 +00:00
|
|
|
* @return {?Puppeteer.ElementHandle}
|
2017-10-06 22:35:02 +00:00
|
|
|
*/
|
|
|
|
asElement() {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
async dispose() {
|
|
|
|
if (this._disposed)
|
|
|
|
return;
|
|
|
|
this._disposed = true;
|
|
|
|
await helper.releaseObject(this._client, this._remoteObject);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @override
|
|
|
|
* @return {string}
|
|
|
|
*/
|
|
|
|
toString() {
|
|
|
|
if (this._remoteObject.objectId) {
|
|
|
|
const type = this._remoteObject.subtype || this._remoteObject.type;
|
|
|
|
return 'JSHandle@' + type;
|
|
|
|
}
|
2017-10-10 17:54:20 +00:00
|
|
|
return 'JSHandle:' + helper.valueFromRemoteObject(this._remoteObject);
|
2017-10-06 22:35:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
helper.tracePublicAPI(JSHandle);
|
|
|
|
module.exports = {ExecutionContext, JSHandle};
|