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.
|
|
|
|
*/
|
|
|
|
|
2019-01-15 04:34:50 +00:00
|
|
|
const {helper, assert} = require('./helper');
|
|
|
|
const {createJSHandle, JSHandle} = require('./JSHandle');
|
2017-10-06 22:35:02 +00:00
|
|
|
|
2018-07-12 01:38:34 +00:00
|
|
|
const EVALUATION_SCRIPT_URL = '__puppeteer_evaluation_script__';
|
|
|
|
const SOURCE_URL_REGEX = /^[\040\t]*\/\/[@#] sourceURL=\s*(\S*?)\s*$/m;
|
|
|
|
|
2017-10-06 22:35:02 +00:00
|
|
|
class ExecutionContext {
|
|
|
|
/**
|
2018-01-11 03:33:22 +00:00
|
|
|
* @param {!Puppeteer.CDPSession} client
|
2018-04-07 01:20:48 +00:00
|
|
|
* @param {!Protocol.Runtime.ExecutionContextDescription} contextPayload
|
2019-01-22 22:55:33 +00:00
|
|
|
* @param {?Puppeteer.DOMWorld} world
|
2017-10-06 22:35:02 +00:00
|
|
|
*/
|
2019-01-22 22:55:33 +00:00
|
|
|
constructor(client, contextPayload, world) {
|
2017-10-06 22:35:02 +00:00
|
|
|
this._client = client;
|
2019-01-22 22:55:33 +00:00
|
|
|
this._world = world;
|
2017-11-19 00:27:52 +00:00
|
|
|
this._contextId = contextPayload.id;
|
2017-10-06 22:35:02 +00:00
|
|
|
}
|
|
|
|
|
2018-02-13 22:02:44 +00:00
|
|
|
/**
|
|
|
|
* @return {?Puppeteer.Frame}
|
|
|
|
*/
|
|
|
|
frame() {
|
2019-01-22 22:55:33 +00:00
|
|
|
return this._world ? this._world.frame() : null;
|
2018-02-13 22:02:44 +00:00
|
|
|
}
|
|
|
|
|
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);
|
2018-02-23 03:10:17 +00:00
|
|
|
const result = await handle.jsonValue().catch(error => {
|
|
|
|
if (error.message.includes('Object reference chain is too long'))
|
|
|
|
return;
|
2018-04-10 05:02:35 +00:00
|
|
|
if (error.message.includes('Object couldn\'t be returned by value'))
|
|
|
|
return;
|
2018-02-23 03:10:17 +00:00
|
|
|
throw error;
|
|
|
|
});
|
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) {
|
2018-07-12 01:38:34 +00:00
|
|
|
const suffix = `//# sourceURL=${EVALUATION_SCRIPT_URL}`;
|
|
|
|
|
2017-10-06 22:35:02 +00:00
|
|
|
if (helper.isString(pageFunction)) {
|
|
|
|
const contextId = this._contextId;
|
2018-04-08 00:58:52 +00:00
|
|
|
const expression = /** @type {string} */ (pageFunction);
|
2018-07-12 01:38:34 +00:00
|
|
|
const expressionWithSourceUrl = SOURCE_URL_REGEX.test(expression) ? expression : expression + '\n' + suffix;
|
2018-06-14 22:27:59 +00:00
|
|
|
const {exceptionDetails, result: remoteObject} = await this._client.send('Runtime.evaluate', {
|
2018-07-12 01:38:34 +00:00
|
|
|
expression: expressionWithSourceUrl,
|
2018-05-04 18:45:16 +00:00
|
|
|
contextId,
|
|
|
|
returnByValue: false,
|
|
|
|
awaitPromise: true,
|
|
|
|
userGesture: true
|
2018-06-14 22:27:59 +00:00
|
|
|
}).catch(rewriteError);
|
2017-10-06 22:35:02 +00:00
|
|
|
if (exceptionDetails)
|
|
|
|
throw new Error('Evaluation failed: ' + helper.getExceptionMessage(exceptionDetails));
|
2018-08-16 23:16:27 +00:00
|
|
|
return createJSHandle(this, remoteObject);
|
2017-10-06 22:35:02 +00:00
|
|
|
}
|
|
|
|
|
2018-07-12 01:38:34 +00:00
|
|
|
if (typeof pageFunction !== 'function')
|
|
|
|
throw new Error('The following is not a function: ' + pageFunction);
|
|
|
|
|
2018-11-01 23:43:21 +00:00
|
|
|
let functionText = pageFunction.toString();
|
|
|
|
try {
|
|
|
|
new Function('(' + functionText + ')');
|
|
|
|
} catch (e1) {
|
|
|
|
// This means we might have a function shorthand. Try another
|
|
|
|
// time prefixing 'function '.
|
|
|
|
if (functionText.startsWith('async '))
|
|
|
|
functionText = 'async function ' + functionText.substring('async '.length);
|
|
|
|
else
|
|
|
|
functionText = 'function ' + functionText;
|
|
|
|
try {
|
|
|
|
new Function('(' + functionText + ')');
|
|
|
|
} catch (e2) {
|
|
|
|
// We tried hard to serialize, but there's a weird beast here.
|
|
|
|
throw new Error('Passed function is not well-serializable!');
|
|
|
|
}
|
|
|
|
}
|
2019-01-14 22:30:50 +00:00
|
|
|
let callFunctionOnPromise;
|
|
|
|
try {
|
|
|
|
callFunctionOnPromise = this._client.send('Runtime.callFunctionOn', {
|
|
|
|
functionDeclaration: functionText + '\n' + suffix + '\n',
|
|
|
|
executionContextId: this._contextId,
|
|
|
|
arguments: args.map(convertArgument.bind(this)),
|
|
|
|
returnByValue: false,
|
|
|
|
awaitPromise: true,
|
|
|
|
userGesture: true
|
|
|
|
});
|
|
|
|
} catch (err) {
|
|
|
|
if (err instanceof TypeError && err.message === 'Converting circular structure to JSON')
|
|
|
|
err.message += ' Are you passing a nested JSHandle?';
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
const { exceptionDetails, result: remoteObject } = await callFunctionOnPromise.catch(rewriteError);
|
2017-10-06 22:35:02 +00:00
|
|
|
if (exceptionDetails)
|
|
|
|
throw new Error('Evaluation failed: ' + helper.getExceptionMessage(exceptionDetails));
|
2018-08-16 23:16:27 +00:00
|
|
|
return createJSHandle(this, remoteObject);
|
2017-10-06 22:35:02 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {*} arg
|
|
|
|
* @return {*}
|
2018-08-06 18:31:33 +00:00
|
|
|
* @this {ExecutionContext}
|
2017-10-06 22:35:02 +00:00
|
|
|
*/
|
|
|
|
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 };
|
|
|
|
}
|
2018-06-14 22:27:59 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {!Error} error
|
|
|
|
* @return {!Protocol.Runtime.evaluateReturnValue}
|
|
|
|
*/
|
|
|
|
function rewriteError(error) {
|
|
|
|
if (error.message.endsWith('Cannot find context with specified id'))
|
|
|
|
throw new Error('Execution context was destroyed, most likely because of a navigation.');
|
|
|
|
throw error;
|
|
|
|
}
|
2017-10-06 22:35:02 +00:00
|
|
|
}
|
2017-10-11 21:41:20 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {!JSHandle} prototypeHandle
|
|
|
|
* @return {!Promise<!JSHandle>}
|
|
|
|
*/
|
|
|
|
async queryObjects(prototypeHandle) {
|
2018-05-31 23:53:51 +00:00
|
|
|
assert(!prototypeHandle._disposed, 'Prototype JSHandle is disposed!');
|
|
|
|
assert(prototypeHandle._remoteObject.objectId, 'Prototype JSHandle must not be referencing primitive value');
|
2017-10-11 21:41:20 +00:00
|
|
|
const response = await this._client.send('Runtime.queryObjects', {
|
|
|
|
prototypeObjectId: prototypeHandle._remoteObject.objectId
|
|
|
|
});
|
2018-08-16 23:16:27 +00:00
|
|
|
return createJSHandle(this, response.objects);
|
2017-10-11 21:41:20 +00:00
|
|
|
}
|
2019-01-28 20:24:27 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {Puppeteer.ElementHandle} elementHandle
|
|
|
|
* @return {Promise<Puppeteer.ElementHandle>}
|
|
|
|
*/
|
|
|
|
async _adoptElementHandle(elementHandle) {
|
|
|
|
assert(elementHandle.executionContext() !== this, 'Cannot adopt handle that already belongs to this execution context');
|
|
|
|
assert(this._world, 'Cannot adopt handle without DOMWorld');
|
|
|
|
const nodeInfo = await this._client.send('DOM.describeNode', {
|
|
|
|
objectId: elementHandle._remoteObject.objectId,
|
|
|
|
});
|
|
|
|
const {object} = await this._client.send('DOM.resolveNode', {
|
|
|
|
backendNodeId: nodeInfo.node.backendNodeId,
|
|
|
|
});
|
|
|
|
return /** @type {Puppeteer.ElementHandle}*/(createJSHandle(this, object));
|
|
|
|
}
|
2017-10-06 22:35:02 +00:00
|
|
|
}
|
|
|
|
|
2019-01-15 04:34:50 +00:00
|
|
|
module.exports = {ExecutionContext, EVALUATION_SCRIPT_URL};
|