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-06-26 22:28:27 +03:00
|
|
|
import { JSHandleChannel, JSHandleInitializer } from '../channels';
|
2020-06-26 02:05:36 +03:00
|
|
|
import { ElementHandle } from './elementHandle';
|
|
|
|
import { ChannelOwner } from './channelOwner';
|
2020-07-01 23:55:29 +03:00
|
|
|
import { ConnectionScope } from './connection';
|
2020-06-27 21:10:07 +03:00
|
|
|
import { serializeAsCallArgument, parseEvaluationResultValue } from '../../common/utilityScriptSerializers';
|
2020-06-26 02:05:36 +03:00
|
|
|
|
|
|
|
type NoHandles<Arg> = Arg extends JSHandle ? never : (Arg extends object ? { [Key in keyof Arg]: NoHandles<Arg[Key]> } : Arg);
|
|
|
|
type Unboxed<Arg> =
|
|
|
|
Arg extends ElementHandle<infer T> ? T :
|
|
|
|
Arg extends JSHandle<infer T> ? T :
|
|
|
|
Arg extends NoHandles<Arg> ? Arg :
|
|
|
|
Arg extends [infer A0] ? [Unboxed<A0>] :
|
|
|
|
Arg extends [infer A0, infer A1] ? [Unboxed<A0>, Unboxed<A1>] :
|
|
|
|
Arg extends [infer A0, infer A1, infer A2] ? [Unboxed<A0>, Unboxed<A1>, Unboxed<A2>] :
|
|
|
|
Arg extends Array<infer T> ? Array<Unboxed<T>> :
|
|
|
|
Arg extends object ? { [Key in keyof Arg]: Unboxed<Arg[Key]> } :
|
|
|
|
Arg;
|
|
|
|
export type Func0<R> = string | (() => R | Promise<R>);
|
|
|
|
export type Func1<Arg, R> = string | ((arg: Unboxed<Arg>) => R | Promise<R>);
|
|
|
|
export type FuncOn<On, Arg2, R> = string | ((on: On, arg2: Unboxed<Arg2>) => R | Promise<R>);
|
|
|
|
export type SmartHandle<T> = T extends Node ? ElementHandle<T> : JSHandle<T>;
|
|
|
|
|
2020-06-26 22:28:27 +03:00
|
|
|
export class JSHandle<T = any> extends ChannelOwner<JSHandleChannel, JSHandleInitializer> {
|
2020-06-30 20:55:11 +03:00
|
|
|
private _preview: string;
|
|
|
|
|
2020-06-26 02:05:36 +03:00
|
|
|
static from(handle: JSHandleChannel): JSHandle {
|
2020-07-02 04:36:09 +03:00
|
|
|
return (handle as any)._object;
|
2020-06-26 02:05:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static fromNullable(handle: JSHandleChannel | null): JSHandle | null {
|
|
|
|
return handle ? JSHandle.from(handle) : null;
|
|
|
|
}
|
|
|
|
|
2020-07-01 23:55:29 +03:00
|
|
|
constructor(scope: ConnectionScope, guid: string, initializer: JSHandleInitializer) {
|
|
|
|
super(scope, guid, initializer);
|
2020-06-30 20:55:11 +03:00
|
|
|
this._preview = this._initializer.preview;
|
2020-07-01 23:55:29 +03:00
|
|
|
this._channel.on('previewUpdated', preview => this._preview = preview);
|
2020-06-26 02:05:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
async evaluate<R, Arg>(pageFunction: FuncOn<T, Arg, R>, arg: Arg): Promise<R>;
|
|
|
|
async evaluate<R>(pageFunction: FuncOn<T, void, R>, arg?: any): Promise<R>;
|
|
|
|
async evaluate<R, Arg>(pageFunction: FuncOn<T, Arg, R>, arg: Arg): Promise<R> {
|
2020-06-27 21:10:07 +03:00
|
|
|
return parseResult(await this._channel.evaluateExpression({ expression: String(pageFunction), isFunction: typeof pageFunction === 'function', arg: serializeArgument(arg) }));
|
2020-06-26 02:05:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
async evaluateHandle<R, Arg>(pageFunction: FuncOn<T, Arg, R>, arg: Arg): Promise<SmartHandle<R>>;
|
|
|
|
async evaluateHandle<R>(pageFunction: FuncOn<T, void, R>, arg?: any): Promise<SmartHandle<R>>;
|
|
|
|
async evaluateHandle<R, Arg>(pageFunction: FuncOn<T, Arg, R>, arg: Arg): Promise<SmartHandle<R>> {
|
2020-06-27 21:10:07 +03:00
|
|
|
const handleChannel = await this._channel.evaluateExpressionHandle({ expression: String(pageFunction), isFunction: typeof pageFunction === 'function', arg: serializeArgument(arg) });
|
2020-06-26 02:05:36 +03:00
|
|
|
return JSHandle.from(handleChannel) as SmartHandle<R>;
|
|
|
|
}
|
|
|
|
|
|
|
|
async getProperty(propertyName: string): Promise<JSHandle> {
|
|
|
|
const objectHandle = await this.evaluateHandle((object: any, propertyName: string) => {
|
|
|
|
const result: any = {__proto__: null};
|
|
|
|
result[propertyName] = object[propertyName];
|
|
|
|
return result;
|
|
|
|
}, propertyName);
|
|
|
|
const properties = await objectHandle.getProperties();
|
|
|
|
const result = properties.get(propertyName)!;
|
|
|
|
objectHandle.dispose();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
async getProperties(): Promise<Map<string, JSHandle>> {
|
|
|
|
const map = new Map<string, JSHandle>();
|
2020-06-26 22:28:27 +03:00
|
|
|
for (const { name, value } of await this._channel.getPropertyList())
|
2020-06-26 02:05:36 +03:00
|
|
|
map.set(name, JSHandle.from(value));
|
|
|
|
return map;
|
|
|
|
}
|
|
|
|
|
|
|
|
async jsonValue(): Promise<T> {
|
2020-06-30 02:37:38 +03:00
|
|
|
return parseResult(await this._channel.jsonValue());
|
2020-06-26 02:05:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
asElement(): ElementHandle | null {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
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-06-27 21:10:07 +03:00
|
|
|
export function serializeArgument(arg: any): any {
|
|
|
|
const guids: { guid: string }[] = [];
|
|
|
|
const pushHandle = (guid: string): number => {
|
|
|
|
guids.push({ guid });
|
|
|
|
return guids.length - 1;
|
|
|
|
};
|
|
|
|
const value = serializeAsCallArgument(arg, value => {
|
|
|
|
if (value instanceof ChannelOwner)
|
2020-07-02 04:36:09 +03:00
|
|
|
return { h: pushHandle(value.guid) };
|
2020-06-27 21:10:07 +03:00
|
|
|
return { fallThrough: value };
|
|
|
|
});
|
|
|
|
return { value, guids };
|
|
|
|
}
|
|
|
|
|
|
|
|
export function parseResult(arg: any): any {
|
|
|
|
return parseEvaluationResultValue(arg, []);
|
2020-06-26 02:05:36 +03:00
|
|
|
}
|