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