2019-12-19 16:53:24 -08:00
|
|
|
/**
|
|
|
|
* Copyright 2017 Google Inc. All rights reserved.
|
|
|
|
* Modifications copyright (c) Microsoft Corporation.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2021-06-02 20:17:24 -07:00
|
|
|
import { WKSession } from './wkConnection';
|
2019-12-19 16:53:24 -08:00
|
|
|
import { Protocol } from './protocol';
|
2020-08-24 06:51:51 -07:00
|
|
|
import * as js from '../javascript';
|
|
|
|
import { parseEvaluationResultValue } from '../common/utilityScriptSerializers';
|
2021-08-26 18:44:49 -07:00
|
|
|
import { isSessionClosedError } from '../common/protocolError';
|
2020-03-18 10:41:46 -07:00
|
|
|
|
2019-12-19 16:53:24 -08:00
|
|
|
export class WKExecutionContext implements js.ExecutionContextDelegate {
|
2020-01-31 17:23:17 -08:00
|
|
|
private readonly _session: WKSession;
|
|
|
|
readonly _contextId: number | undefined;
|
2019-12-19 16:53:24 -08:00
|
|
|
|
2020-01-31 17:23:17 -08:00
|
|
|
constructor(session: WKSession, contextId: number | undefined) {
|
|
|
|
this._session = session;
|
2020-01-07 12:59:01 -08:00
|
|
|
this._contextId = contextId;
|
2019-12-19 16:53:24 -08:00
|
|
|
}
|
|
|
|
|
2021-04-29 09:28:19 -07:00
|
|
|
async rawEvaluateJSON(expression: string): Promise<any> {
|
|
|
|
try {
|
|
|
|
const response = await this._session.send('Runtime.evaluate', {
|
|
|
|
expression,
|
|
|
|
contextId: this._contextId,
|
|
|
|
returnByValue: true
|
|
|
|
});
|
|
|
|
if (response.wasThrown)
|
2021-08-26 18:44:49 -07:00
|
|
|
throw new js.JavaScriptErrorInEvaluate(response.result.description);
|
2021-04-29 09:28:19 -07:00
|
|
|
return response.result.value;
|
|
|
|
} catch (error) {
|
|
|
|
throw rewriteError(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async rawEvaluateHandle(expression: string): Promise<js.ObjectId> {
|
2020-06-03 17:50:16 -07:00
|
|
|
try {
|
|
|
|
const response = await this._session.send('Runtime.evaluate', {
|
2021-01-25 16:36:57 -08:00
|
|
|
expression,
|
2020-06-03 17:50:16 -07:00
|
|
|
contextId: this._contextId,
|
|
|
|
returnByValue: false
|
|
|
|
});
|
|
|
|
if (response.wasThrown)
|
2021-08-26 18:44:49 -07:00
|
|
|
throw new js.JavaScriptErrorInEvaluate(response.result.description);
|
2020-06-03 17:50:16 -07:00
|
|
|
return response.result.objectId!;
|
|
|
|
} catch (error) {
|
|
|
|
throw rewriteError(error);
|
|
|
|
}
|
2020-05-20 15:55:33 -07:00
|
|
|
}
|
|
|
|
|
2021-03-10 11:43:26 -08:00
|
|
|
rawCallFunctionNoReply(func: Function, ...args: any[]) {
|
|
|
|
this._session.send('Runtime.callFunctionOn', {
|
|
|
|
functionDeclaration: func.toString(),
|
|
|
|
objectId: args.find(a => a instanceof js.JSHandle)!._objectId,
|
|
|
|
arguments: args.map(a => a instanceof js.JSHandle ? { objectId: a._objectId } : { value: a }),
|
|
|
|
returnByValue: true,
|
|
|
|
emulateUserGesture: true
|
|
|
|
}).catch(() => {});
|
|
|
|
}
|
|
|
|
|
2020-06-03 17:50:16 -07:00
|
|
|
async evaluateWithArguments(expression: string, returnByValue: boolean, utilityScript: js.JSHandle<any>, values: any[], objectIds: string[]): Promise<any> {
|
2020-01-31 17:23:17 -08:00
|
|
|
try {
|
2021-11-03 10:44:50 -07:00
|
|
|
const response = await this._session.send('Runtime.callFunctionOn', {
|
|
|
|
functionDeclaration: expression,
|
|
|
|
objectId: utilityScript._objectId!,
|
|
|
|
arguments: [
|
|
|
|
{ objectId: utilityScript._objectId },
|
|
|
|
...values.map(value => ({ value })),
|
|
|
|
...objectIds.map(objectId => ({ objectId })),
|
|
|
|
],
|
|
|
|
returnByValue,
|
|
|
|
emulateUserGesture: true,
|
|
|
|
awaitPromise: true
|
|
|
|
});
|
2020-01-31 17:23:17 -08:00
|
|
|
if (response.wasThrown)
|
2021-08-26 18:44:49 -07:00
|
|
|
throw new js.JavaScriptErrorInEvaluate(response.result.description);
|
2021-06-02 17:04:47 +00:00
|
|
|
if (returnByValue)
|
|
|
|
return parseEvaluationResultValue(response.result.value);
|
|
|
|
return utilityScript._context.createHandle(response.result);
|
2020-06-03 17:50:16 -07:00
|
|
|
} catch (error) {
|
2020-06-26 09:50:57 -07:00
|
|
|
throw rewriteError(error);
|
2020-01-31 17:23:17 -08:00
|
|
|
}
|
2019-12-19 16:53:24 -08:00
|
|
|
}
|
|
|
|
|
2021-04-07 07:01:38 +08:00
|
|
|
async getProperties(context: js.ExecutionContext, objectId: js.ObjectId): Promise<Map<string, js.JSHandle>> {
|
2019-12-19 16:53:24 -08:00
|
|
|
const response = await this._session.send('Runtime.getProperties', {
|
2020-01-13 13:33:25 -08:00
|
|
|
objectId,
|
2019-12-19 16:53:24 -08:00
|
|
|
ownProperties: true
|
|
|
|
});
|
|
|
|
const result = new Map();
|
|
|
|
for (const property of response.properties) {
|
2020-05-27 17:19:05 -07:00
|
|
|
if (!property.enumerable || !property.value)
|
2019-12-19 16:53:24 -08:00
|
|
|
continue;
|
2021-04-07 07:01:38 +08:00
|
|
|
result.set(property.name, context.createHandle(property.value));
|
2019-12-19 16:53:24 -08:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-05-27 22:19:05 -07:00
|
|
|
createHandle(context: js.ExecutionContext, remoteObject: Protocol.Runtime.RemoteObject): js.JSHandle {
|
|
|
|
const isPromise = remoteObject.className === 'Promise';
|
2021-08-06 11:37:36 -07:00
|
|
|
return new js.JSHandle(context, isPromise ? 'promise' : remoteObject.subtype || remoteObject.type, renderPreview(remoteObject), remoteObject.objectId, potentiallyUnserializableValue(remoteObject));
|
2020-05-27 22:19:05 -07:00
|
|
|
}
|
|
|
|
|
2021-04-07 07:01:38 +08:00
|
|
|
async releaseHandle(objectId: js.ObjectId): Promise<void> {
|
|
|
|
await this._session.send('Runtime.releaseObject', { objectId });
|
2019-12-19 16:53:24 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-27 22:19:05 -07:00
|
|
|
function potentiallyUnserializableValue(remoteObject: Protocol.Runtime.RemoteObject): any {
|
|
|
|
const value = remoteObject.value;
|
2020-07-17 09:49:12 -07:00
|
|
|
const isUnserializable = remoteObject.type === 'number' && ['NaN', '-Infinity', 'Infinity', '-0'].includes(remoteObject.description!);
|
|
|
|
return isUnserializable ? js.parseUnserializableValue(remoteObject.description!) : value;
|
2020-05-27 22:19:05 -07:00
|
|
|
}
|
2020-06-03 17:50:16 -07:00
|
|
|
|
|
|
|
function rewriteError(error: Error): Error {
|
2021-08-26 18:44:49 -07:00
|
|
|
if (!js.isJavaScriptErrorInEvaluate(error) && !isSessionClosedError(error))
|
2020-06-03 17:50:16 -07:00
|
|
|
return new Error('Execution context was destroyed, most likely because of a navigation.');
|
|
|
|
return error;
|
|
|
|
}
|
2021-08-06 11:37:36 -07:00
|
|
|
|
|
|
|
function renderPreview(object: Protocol.Runtime.RemoteObject): string | undefined {
|
|
|
|
if (object.type === 'undefined')
|
|
|
|
return 'undefined';
|
|
|
|
if ('value' in object)
|
|
|
|
return String(object.value);
|
|
|
|
|
|
|
|
if (object.description === 'Object' && object.preview) {
|
|
|
|
const tokens = [];
|
|
|
|
for (const { name, value } of object.preview.properties!)
|
|
|
|
tokens.push(`${name}: ${value}`);
|
|
|
|
return `{${tokens.join(', ')}}`;
|
|
|
|
}
|
|
|
|
if (object.subtype === 'array' && object.preview) {
|
|
|
|
const result = [];
|
|
|
|
for (const { name, value } of object.preview.properties!)
|
|
|
|
result[+name] = value;
|
|
|
|
return '[' + String(result) + ']';
|
|
|
|
}
|
|
|
|
return object.description;
|
|
|
|
}
|