2019-11-27 12:39:53 -08:00
|
|
|
// Copyright (c) Microsoft Corporation.
|
|
|
|
// Licensed under the MIT license.
|
|
|
|
|
|
|
|
import * as frames from './frames';
|
|
|
|
import * as types from './types';
|
2019-11-27 16:02:31 -08:00
|
|
|
import * as dom from './dom';
|
2019-11-27 12:39:53 -08:00
|
|
|
|
2019-11-27 16:02:31 -08:00
|
|
|
export interface ExecutionContextDelegate {
|
|
|
|
evaluate(context: ExecutionContext, returnByValue: boolean, pageFunction: string | Function, ...args: any[]): Promise<any>;
|
|
|
|
getProperties(handle: JSHandle): Promise<Map<string, JSHandle>>;
|
|
|
|
releaseHandle(handle: JSHandle): Promise<void>;
|
2019-12-02 13:01:01 -08:00
|
|
|
handleToString(handle: JSHandle, includeType: boolean): string;
|
2019-11-27 16:02:31 -08:00
|
|
|
handleJSONValue(handle: JSHandle): Promise<any>;
|
2019-11-27 12:39:53 -08:00
|
|
|
}
|
|
|
|
|
2019-11-27 16:02:31 -08:00
|
|
|
export class ExecutionContext {
|
2019-11-28 12:50:52 -08:00
|
|
|
readonly _delegate: ExecutionContextDelegate;
|
|
|
|
_domWorld?: dom.DOMWorld;
|
2019-11-27 12:39:53 -08:00
|
|
|
|
2019-11-28 12:50:52 -08:00
|
|
|
constructor(delegate: ExecutionContextDelegate) {
|
2019-11-27 12:39:53 -08:00
|
|
|
this._delegate = delegate;
|
|
|
|
}
|
|
|
|
|
2019-11-27 16:02:31 -08:00
|
|
|
frame(): frames.Frame | null {
|
2019-11-28 12:50:52 -08:00
|
|
|
return this._domWorld ? this._domWorld.delegate.frame : null;
|
2019-11-27 12:39:53 -08:00
|
|
|
}
|
|
|
|
|
2019-11-27 16:03:51 -08:00
|
|
|
evaluate: types.Evaluate = (pageFunction, ...args) => {
|
2019-11-27 12:39:53 -08:00
|
|
|
return this._delegate.evaluate(this, true /* returnByValue */, pageFunction, ...args);
|
|
|
|
}
|
|
|
|
|
2019-11-27 16:03:51 -08:00
|
|
|
evaluateHandle: types.EvaluateHandle = (pageFunction, ...args) => {
|
2019-11-27 12:39:53 -08:00
|
|
|
return this._delegate.evaluate(this, false /* returnByValue */, pageFunction, ...args);
|
|
|
|
}
|
2019-12-02 13:12:28 -08:00
|
|
|
|
|
|
|
_createHandle(remoteObject: any): JSHandle {
|
2019-12-02 17:33:44 -08:00
|
|
|
return (this._domWorld && this._domWorld.createHandle(remoteObject)) || new JSHandle(this, remoteObject);
|
2019-12-02 13:12:28 -08:00
|
|
|
}
|
2019-11-27 12:39:53 -08:00
|
|
|
}
|
|
|
|
|
2019-11-27 16:02:31 -08:00
|
|
|
export class JSHandle {
|
2019-11-28 12:50:52 -08:00
|
|
|
readonly _context: ExecutionContext;
|
2019-12-02 13:12:28 -08:00
|
|
|
readonly _remoteObject: any;
|
2019-11-27 12:41:26 -08:00
|
|
|
_disposed = false;
|
|
|
|
|
2019-12-02 13:12:28 -08:00
|
|
|
constructor(context: ExecutionContext, remoteObject: any) {
|
2019-11-27 12:41:26 -08:00
|
|
|
this._context = context;
|
2019-12-02 13:12:28 -08:00
|
|
|
this._remoteObject = remoteObject;
|
2019-11-27 12:41:26 -08:00
|
|
|
}
|
|
|
|
|
2019-11-27 16:02:31 -08:00
|
|
|
executionContext(): ExecutionContext {
|
2019-11-27 12:41:26 -08:00
|
|
|
return this._context;
|
|
|
|
}
|
|
|
|
|
2019-11-27 16:03:51 -08:00
|
|
|
evaluate: types.EvaluateOn = (pageFunction, ...args) => {
|
2019-11-27 12:41:26 -08:00
|
|
|
return this._context.evaluate(pageFunction, this, ...args);
|
|
|
|
}
|
|
|
|
|
2019-11-27 16:03:51 -08:00
|
|
|
evaluateHandle: types.EvaluateHandleOn = (pageFunction, ...args) => {
|
2019-11-27 12:41:26 -08:00
|
|
|
return this._context.evaluateHandle(pageFunction, this, ...args);
|
|
|
|
}
|
|
|
|
|
2019-11-27 16:02:31 -08:00
|
|
|
async getProperty(propertyName: string): Promise<JSHandle | null> {
|
2019-11-27 12:41:26 -08:00
|
|
|
const objectHandle = await this.evaluateHandle((object, propertyName) => {
|
|
|
|
const result = {__proto__: null};
|
|
|
|
result[propertyName] = object[propertyName];
|
|
|
|
return result;
|
|
|
|
}, propertyName);
|
|
|
|
const properties = await objectHandle.getProperties();
|
|
|
|
const result = properties.get(propertyName) || null;
|
|
|
|
await objectHandle.dispose();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-11-27 16:02:31 -08:00
|
|
|
getProperties(): Promise<Map<string, JSHandle>> {
|
2019-11-27 12:41:26 -08:00
|
|
|
return this._context._delegate.getProperties(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
jsonValue(): Promise<any> {
|
|
|
|
return this._context._delegate.handleJSONValue(this);
|
|
|
|
}
|
|
|
|
|
2019-11-27 16:02:31 -08:00
|
|
|
asElement(): dom.ElementHandle | null {
|
2019-11-27 12:41:26 -08:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
async dispose() {
|
|
|
|
if (this._disposed)
|
|
|
|
return;
|
|
|
|
this._disposed = true;
|
|
|
|
await this._context._delegate.releaseHandle(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
toString(): string {
|
2019-12-02 13:01:01 -08:00
|
|
|
return this._context._delegate.handleToString(this, true /* includeType */);
|
2019-11-27 12:41:26 -08:00
|
|
|
}
|
|
|
|
}
|