/** * 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. */ import { JSHandleChannel, JSHandleInitializer } from '../channels'; import { ElementHandle } from './elementHandle'; import { ChannelOwner } from './channelOwner'; import { ConnectionScope } from './connection'; import { serializeAsCallArgument, parseEvaluationResultValue } from '../../common/utilityScriptSerializers'; type NoHandles = Arg extends JSHandle ? never : (Arg extends object ? { [Key in keyof Arg]: NoHandles } : Arg); type Unboxed = Arg extends ElementHandle ? T : Arg extends JSHandle ? T : Arg extends NoHandles ? Arg : Arg extends [infer A0] ? [Unboxed] : Arg extends [infer A0, infer A1] ? [Unboxed, Unboxed] : Arg extends [infer A0, infer A1, infer A2] ? [Unboxed, Unboxed, Unboxed] : Arg extends Array ? Array> : Arg extends object ? { [Key in keyof Arg]: Unboxed } : Arg; export type Func0 = string | (() => R | Promise); export type Func1 = string | ((arg: Unboxed) => R | Promise); export type FuncOn = string | ((on: On, arg2: Unboxed) => R | Promise); export type SmartHandle = T extends Node ? ElementHandle : JSHandle; export class JSHandle extends ChannelOwner { private _preview: string; static from(handle: JSHandleChannel): JSHandle { return (handle as any)._object; } static fromNullable(handle: JSHandleChannel | null): JSHandle | null { return handle ? JSHandle.from(handle) : null; } constructor(scope: ConnectionScope, guid: string, initializer: JSHandleInitializer) { super(scope, guid, initializer); this._preview = this._initializer.preview; this._channel.on('previewUpdated', preview => this._preview = preview); } async evaluate(pageFunction: FuncOn, arg: Arg): Promise; async evaluate(pageFunction: FuncOn, arg?: any): Promise; async evaluate(pageFunction: FuncOn, arg: Arg): Promise { return parseResult(await this._channel.evaluateExpression({ expression: String(pageFunction), isFunction: typeof pageFunction === 'function', arg: serializeArgument(arg) })); } async evaluateHandle(pageFunction: FuncOn, arg: Arg): Promise>; async evaluateHandle(pageFunction: FuncOn, arg?: any): Promise>; async evaluateHandle(pageFunction: FuncOn, arg: Arg): Promise> { const handleChannel = await this._channel.evaluateExpressionHandle({ expression: String(pageFunction), isFunction: typeof pageFunction === 'function', arg: serializeArgument(arg) }); return JSHandle.from(handleChannel) as SmartHandle; } async getProperty(propertyName: string): Promise { 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> { const map = new Map(); for (const { name, value } of await this._channel.getPropertyList()) map.set(name, JSHandle.from(value)); return map; } async jsonValue(): Promise { return parseResult(await this._channel.jsonValue()); } asElement(): ElementHandle | null { return null; } async dispose() { return await this._channel.dispose(); } toString(): string { return this._preview; } } 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) return { h: pushHandle(value.guid) }; return { fallThrough: value }; }); return { value, guids }; } export function parseResult(arg: any): any { return parseEvaluationResultValue(arg, []); }