2020-06-25 16:05:36 -07: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.
|
|
|
|
*/
|
|
|
|
|
2020-08-24 17:05:16 -07:00
|
|
|
import * as channels from '../protocol/channels';
|
2020-06-25 16:05:36 -07:00
|
|
|
import { ChannelOwner } from './channelOwner';
|
2020-08-22 15:13:51 -07:00
|
|
|
import { parseSerializedValue, serializeValue } from '../protocol/serializers';
|
2020-12-26 17:05:57 -08:00
|
|
|
import * as api from '../../types/types';
|
|
|
|
import * as structs from '../../types/structs';
|
2020-06-25 16:05:36 -07:00
|
|
|
|
2020-12-26 17:05:57 -08:00
|
|
|
export class JSHandle<T = any> extends ChannelOwner<channels.JSHandleChannel, channels.JSHandleInitializer> implements api.JSHandle {
|
2020-06-30 10:55:11 -07:00
|
|
|
private _preview: string;
|
|
|
|
|
2020-08-24 17:05:16 -07:00
|
|
|
static from(handle: channels.JSHandleChannel): JSHandle {
|
2020-07-01 18:36:09 -07:00
|
|
|
return (handle as any)._object;
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
2020-08-24 17:05:16 -07:00
|
|
|
constructor(parent: ChannelOwner, type: string, guid: string, initializer: channels.JSHandleInitializer) {
|
2020-07-10 18:00:10 -07:00
|
|
|
super(parent, type, guid, initializer);
|
2020-06-30 10:55:11 -07:00
|
|
|
this._preview = this._initializer.preview;
|
2020-07-14 18:26:50 -07:00
|
|
|
this._channel.on('previewUpdated', ({preview}) => this._preview = preview);
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
2020-12-26 17:05:57 -08:00
|
|
|
async evaluate<R, Arg>(pageFunction: structs.PageFunctionOn<T, Arg, R>, arg?: Arg): Promise<R> {
|
2021-06-28 13:27:38 -07:00
|
|
|
return this._wrapApiCall(async (channel: channels.JSHandleChannel) => {
|
2021-02-19 16:21:39 -08:00
|
|
|
const result = await channel.evaluateExpression({ expression: String(pageFunction), isFunction: typeof pageFunction === 'function', arg: serializeArgument(arg) });
|
2021-01-22 06:49:59 -08:00
|
|
|
return parseResult(result.value);
|
|
|
|
});
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
2020-12-26 17:05:57 -08:00
|
|
|
async evaluateHandle<R, Arg>(pageFunction: structs.PageFunctionOn<T, Arg, R>, arg?: Arg): Promise<structs.SmartHandle<R>> {
|
2021-06-28 13:27:38 -07:00
|
|
|
return this._wrapApiCall(async (channel: channels.JSHandleChannel) => {
|
2021-02-19 16:21:39 -08:00
|
|
|
const result = await channel.evaluateExpressionHandle({ expression: String(pageFunction), isFunction: typeof pageFunction === 'function', arg: serializeArgument(arg) });
|
2021-01-22 06:49:59 -08:00
|
|
|
return JSHandle.from(result.handle) as any as structs.SmartHandle<R>;
|
|
|
|
});
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
2020-07-31 17:00:36 -07:00
|
|
|
async getProperty(propertyName: string): Promise<JSHandle> {
|
2021-06-28 13:27:38 -07:00
|
|
|
return this._wrapApiCall(async (channel: channels.JSHandleChannel) => {
|
2021-02-19 16:21:39 -08:00
|
|
|
const result = await channel.getProperty({ name: propertyName });
|
2021-01-22 06:49:59 -08:00
|
|
|
return JSHandle.from(result.handle);
|
|
|
|
});
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
async getProperties(): Promise<Map<string, JSHandle>> {
|
2021-06-28 13:27:38 -07:00
|
|
|
return this._wrapApiCall(async (channel: channels.JSHandleChannel) => {
|
2021-01-22 06:49:59 -08:00
|
|
|
const map = new Map<string, JSHandle>();
|
2021-02-19 16:21:39 -08:00
|
|
|
for (const { name, value } of (await channel.getPropertyList()).properties)
|
2021-01-22 06:49:59 -08:00
|
|
|
map.set(name, JSHandle.from(value));
|
|
|
|
return map;
|
|
|
|
});
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
async jsonValue(): Promise<T> {
|
2021-06-28 13:27:38 -07:00
|
|
|
return this._wrapApiCall(async (channel: channels.JSHandleChannel) => {
|
2021-02-19 16:21:39 -08:00
|
|
|
return parseResult((await channel.jsonValue()).value);
|
2021-01-22 06:49:59 -08:00
|
|
|
});
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
2020-12-26 17:05:57 -08:00
|
|
|
asElement(): T extends Node ? api.ElementHandle<T> : null {
|
|
|
|
return null as any;
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
async dispose() {
|
2021-06-28 13:27:38 -07:00
|
|
|
return this._wrapApiCall(async (channel: channels.JSHandleChannel) => {
|
2021-02-19 16:21:39 -08:00
|
|
|
return await channel.dispose();
|
|
|
|
});
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
2021-08-25 10:11:18 -04:00
|
|
|
override toString(): string {
|
2020-06-30 10:55:11 -07:00
|
|
|
return this._preview;
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-17 09:53:13 -07:00
|
|
|
// This function takes care of converting all JSHandles to their channels,
|
|
|
|
// so that generic channel serializer converts them to guids.
|
2020-08-24 17:05:16 -07:00
|
|
|
export function serializeArgument(arg: any): channels.SerializedArgument {
|
|
|
|
const handles: channels.Channel[] = [];
|
|
|
|
const pushHandle = (channel: channels.Channel): number => {
|
2020-07-17 09:53:13 -07:00
|
|
|
handles.push(channel);
|
|
|
|
return handles.length - 1;
|
2020-06-27 11:10:07 -07:00
|
|
|
};
|
2020-07-21 12:44:30 -07:00
|
|
|
const value = serializeValue(arg, value => {
|
2020-07-17 09:53:13 -07:00
|
|
|
if (value instanceof JSHandle)
|
|
|
|
return { h: pushHandle(value._channel) };
|
2020-06-27 11:10:07 -07:00
|
|
|
return { fallThrough: value };
|
2020-07-21 12:44:30 -07:00
|
|
|
}, new Set());
|
2020-07-17 09:53:13 -07:00
|
|
|
return { value, handles };
|
2020-06-27 11:10:07 -07:00
|
|
|
}
|
|
|
|
|
2020-08-24 17:05:16 -07:00
|
|
|
export function parseResult(value: channels.SerializedValue): any {
|
2020-07-21 12:44:30 -07:00
|
|
|
return parseSerializedValue(value, undefined);
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
2020-08-17 14:36:51 -07:00
|
|
|
|
|
|
|
export function assertMaxArguments(count: number, max: number): asserts count {
|
|
|
|
if (count > max)
|
|
|
|
throw new Error('Too many arguments. If you need to pass more than 1 argument to the function wrap them in an object.');
|
|
|
|
}
|