2019-02-09 01:15:52 +00:00
|
|
|
const {helper, assert, debugError} = require('./helper');
|
|
|
|
const {JSHandle, createHandle} = require('./JSHandle');
|
|
|
|
|
|
|
|
class ExecutionContext {
|
|
|
|
/**
|
|
|
|
* @param {!PageSession} session
|
|
|
|
* @param {?Frame} frame
|
|
|
|
* @param {string} executionContextId
|
|
|
|
*/
|
|
|
|
constructor(session, frame, executionContextId) {
|
|
|
|
this._session = session;
|
|
|
|
this._frame = frame;
|
|
|
|
this._executionContextId = executionContextId;
|
|
|
|
}
|
|
|
|
|
|
|
|
async evaluateHandle(pageFunction, ...args) {
|
|
|
|
if (helper.isString(pageFunction)) {
|
2019-02-25 07:07:24 +00:00
|
|
|
const payload = await this._session.send('Runtime.evaluate', {
|
|
|
|
expression: pageFunction,
|
2019-02-09 01:15:52 +00:00
|
|
|
executionContextId: this._executionContextId,
|
2019-02-25 07:07:24 +00:00
|
|
|
}).catch(rewriteError);
|
2019-02-09 01:15:52 +00:00
|
|
|
return createHandle(this, payload.result, payload.exceptionDetails);
|
|
|
|
}
|
2019-02-14 07:41:53 +00:00
|
|
|
if (typeof pageFunction !== 'function')
|
|
|
|
throw new Error(`Expected to get |string| or |function| as the first argument, but got "${pageFunction}" instead.`);
|
|
|
|
|
|
|
|
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-02-09 01:15:52 +00:00
|
|
|
args = args.map(arg => {
|
2019-02-14 07:41:53 +00:00
|
|
|
if (arg instanceof JSHandle) {
|
|
|
|
if (arg._context !== this)
|
|
|
|
throw new Error('JSHandles can be evaluated only in the context they were created!');
|
|
|
|
if (arg._disposed)
|
|
|
|
throw new Error('JSHandle is disposed!');
|
2019-02-09 01:15:52 +00:00
|
|
|
return arg._protocolValue;
|
2019-02-14 07:41:53 +00:00
|
|
|
}
|
2019-02-09 01:15:52 +00:00
|
|
|
if (Object.is(arg, Infinity))
|
|
|
|
return {unserializableValue: 'Infinity'};
|
|
|
|
if (Object.is(arg, -Infinity))
|
|
|
|
return {unserializableValue: '-Infinity'};
|
|
|
|
if (Object.is(arg, -0))
|
|
|
|
return {unserializableValue: '-0'};
|
|
|
|
if (Object.is(arg, NaN))
|
|
|
|
return {unserializableValue: 'NaN'};
|
|
|
|
return {value: arg};
|
|
|
|
});
|
2019-02-25 07:07:24 +00:00
|
|
|
let callFunctionPromise;
|
2019-02-14 07:59:38 +00:00
|
|
|
try {
|
2019-02-25 07:07:24 +00:00
|
|
|
callFunctionPromise = this._session.send('Runtime.callFunction', {
|
|
|
|
functionDeclaration: functionText,
|
2019-02-14 07:59:38 +00:00
|
|
|
args,
|
|
|
|
executionContextId: this._executionContextId
|
|
|
|
});
|
|
|
|
} catch (err) {
|
2019-07-13 00:19:02 +00:00
|
|
|
if (err instanceof TypeError && err.message.startsWith('Converting circular structure to JSON'))
|
2019-02-14 07:59:38 +00:00
|
|
|
err.message += ' Are you passing a nested JSHandle?';
|
|
|
|
throw err;
|
|
|
|
}
|
2019-02-25 07:07:24 +00:00
|
|
|
const payload = await callFunctionPromise.catch(rewriteError);
|
2019-02-09 01:15:52 +00:00
|
|
|
return createHandle(this, payload.result, payload.exceptionDetails);
|
2019-02-25 07:07:24 +00:00
|
|
|
|
|
|
|
function rewriteError(error) {
|
|
|
|
if (error.message.includes('Failed to find execution context with id'))
|
|
|
|
throw new Error('Execution context was destroyed, most likely because of a navigation.');
|
|
|
|
throw error;
|
|
|
|
}
|
2019-02-09 01:15:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
frame() {
|
|
|
|
return this._frame;
|
|
|
|
}
|
|
|
|
|
|
|
|
async evaluate(pageFunction, ...args) {
|
|
|
|
try {
|
|
|
|
const handle = await this.evaluateHandle(pageFunction, ...args);
|
|
|
|
const result = await handle.jsonValue();
|
|
|
|
await handle.dispose();
|
|
|
|
return result;
|
|
|
|
} catch (e) {
|
|
|
|
if (e.message.includes('cyclic object value') || e.message.includes('Object is not serializable'))
|
|
|
|
return undefined;
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {ExecutionContext};
|