2020-06-26 01:05:36 +02:00
|
|
|
/**
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
|
|
|
|
|
2022-04-07 07:21:27 +02:00
|
|
|
import type * as js from '../javascript';
|
2022-09-21 03:41:51 +02:00
|
|
|
import type * as channels from '@protocol/channels';
|
2022-04-06 23:57:14 +02:00
|
|
|
import { Dispatcher } from './dispatcher';
|
2021-04-21 08:03:56 +02:00
|
|
|
import { ElementHandleDispatcher } from './elementHandlerDispatcher';
|
2022-04-07 07:21:27 +02:00
|
|
|
import { parseSerializedValue, serializeValue } from '../../protocol/serializers';
|
2022-08-25 20:58:41 +02:00
|
|
|
import type { PageDispatcher, WorkerDispatcher } from './pageDispatcher';
|
|
|
|
|
import type { ElectronApplicationDispatcher } from './electronDispatcher';
|
2023-07-12 23:51:13 +02:00
|
|
|
import type { FrameDispatcher } from './frameDispatcher';
|
2023-11-02 00:36:39 +01:00
|
|
|
import type { CallMetadata } from '../instrumentation';
|
2020-06-26 01:05:36 +02:00
|
|
|
|
2023-07-12 23:51:13 +02:00
|
|
|
export type JSHandleDispatcherParentScope = PageDispatcher | FrameDispatcher | WorkerDispatcher | ElectronApplicationDispatcher;
|
2022-08-25 20:58:41 +02:00
|
|
|
|
|
|
|
|
export class JSHandleDispatcher extends Dispatcher<js.JSHandle, channels.JSHandleChannel, JSHandleDispatcherParentScope> implements channels.JSHandleChannel {
|
2021-11-18 00:26:01 +01:00
|
|
|
_type_JSHandle = true;
|
2020-06-26 01:05:36 +02:00
|
|
|
|
2022-08-25 20:58:41 +02:00
|
|
|
protected constructor(scope: JSHandleDispatcherParentScope, jsHandle: js.JSHandle) {
|
2021-01-04 22:54:55 +01:00
|
|
|
// Do not call this directly, use createHandle() instead.
|
2020-07-22 19:37:21 +02:00
|
|
|
super(scope, jsHandle, jsHandle.asElement() ? 'ElementHandle' : 'JSHandle', {
|
2020-06-26 21:28:27 +02:00
|
|
|
preview: jsHandle.toString(),
|
|
|
|
|
});
|
2020-07-15 03:26:50 +02:00
|
|
|
jsHandle._setPreviewCallback(preview => this._dispatchEvent('previewUpdated', { preview }));
|
2020-06-26 01:05:36 +02:00
|
|
|
}
|
|
|
|
|
|
2020-08-20 20:25:33 +02:00
|
|
|
async evaluateExpression(params: channels.JSHandleEvaluateExpressionParams): Promise<channels.JSHandleEvaluateExpressionResult> {
|
2023-05-31 23:08:44 +02:00
|
|
|
return { value: serializeResult(await this._object.evaluateExpression(params.expression, { isFunction: params.isFunction }, parseArgument(params.arg))) };
|
2020-06-26 01:05:36 +02:00
|
|
|
}
|
|
|
|
|
|
2020-08-20 20:25:33 +02:00
|
|
|
async evaluateExpressionHandle(params: channels.JSHandleEvaluateExpressionHandleParams): Promise<channels.JSHandleEvaluateExpressionHandleResult> {
|
2023-05-31 23:08:44 +02:00
|
|
|
const jsHandle = await this._object.evaluateExpressionHandle(params.expression, { isFunction: params.isFunction }, parseArgument(params.arg));
|
2022-08-26 18:30:27 +02:00
|
|
|
return { handle: ElementHandleDispatcher.fromJSHandle(this.parentScope(), jsHandle) };
|
2020-06-26 01:05:36 +02:00
|
|
|
}
|
|
|
|
|
|
2020-08-20 20:25:33 +02:00
|
|
|
async getProperty(params: channels.JSHandleGetPropertyParams): Promise<channels.JSHandleGetPropertyResult> {
|
2020-07-04 03:04:08 +02:00
|
|
|
const jsHandle = await this._object.getProperty(params.name);
|
2022-08-26 18:30:27 +02:00
|
|
|
return { handle: ElementHandleDispatcher.fromJSHandle(this.parentScope(), jsHandle) };
|
2020-07-04 03:04:08 +02:00
|
|
|
}
|
|
|
|
|
|
2020-08-20 20:25:33 +02:00
|
|
|
async getPropertyList(): Promise<channels.JSHandleGetPropertyListResult> {
|
2020-06-27 02:24:21 +02:00
|
|
|
const map = await this._object.getProperties();
|
2020-07-15 03:26:50 +02:00
|
|
|
const properties = [];
|
2020-06-26 01:05:36 +02:00
|
|
|
for (const [name, value] of map)
|
2022-08-26 18:30:27 +02:00
|
|
|
properties.push({ name, value: ElementHandleDispatcher.fromJSHandle(this.parentScope(), value) });
|
2020-07-15 03:26:50 +02:00
|
|
|
return { properties };
|
2020-06-26 01:05:36 +02:00
|
|
|
}
|
|
|
|
|
|
2020-08-20 20:25:33 +02:00
|
|
|
async jsonValue(): Promise<channels.JSHandleJsonValueResult> {
|
2020-07-15 03:26:50 +02:00
|
|
|
return { value: serializeResult(await this._object.jsonValue()) };
|
2020-06-26 01:05:36 +02:00
|
|
|
}
|
|
|
|
|
|
2023-04-01 00:52:01 +02:00
|
|
|
async objectCount(params?: channels.JSHandleObjectCountParams | undefined): Promise<channels.JSHandleObjectCountResult> {
|
|
|
|
|
return { count: await this._object.objectCount() };
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-02 00:36:39 +01:00
|
|
|
async dispose(_: any, metadata: CallMetadata) {
|
2023-11-07 00:13:41 +01:00
|
|
|
metadata.potentiallyClosesScope = true;
|
2023-11-02 00:36:39 +01:00
|
|
|
this._object.dispose();
|
|
|
|
|
this._dispose();
|
2020-06-26 01:05:36 +02:00
|
|
|
}
|
|
|
|
|
}
|
2020-06-27 20:10:07 +02:00
|
|
|
|
2020-07-17 18:53:13 +02:00
|
|
|
// Generic channel parser converts guids to JSHandleDispatchers,
|
2024-05-08 20:40:03 +02:00
|
|
|
// and this function takes care of converting them into underlying JSHandles.
|
2020-08-20 20:25:33 +02:00
|
|
|
export function parseArgument(arg: channels.SerializedArgument): any {
|
2020-07-21 21:44:30 +02:00
|
|
|
return parseSerializedValue(arg.value, arg.handles.map(a => (a as JSHandleDispatcher)._object));
|
2020-06-27 20:10:07 +02:00
|
|
|
}
|
2020-08-20 20:25:33 +02:00
|
|
|
|
|
|
|
|
export function parseValue(v: channels.SerializedValue): any {
|
2020-07-23 02:20:00 +02:00
|
|
|
return parseSerializedValue(v, []);
|
|
|
|
|
}
|
2020-06-27 20:10:07 +02:00
|
|
|
|
2020-08-20 20:25:33 +02:00
|
|
|
export function serializeResult(arg: any): channels.SerializedValue {
|
2022-05-10 03:51:53 +02:00
|
|
|
return serializeValue(arg, value => ({ fallThrough: value }));
|
2020-06-27 20:10:07 +02:00
|
|
|
}
|