2022-09-21 06:10:50 +00:00
|
|
|
/**
|
|
|
|
* Copyright 2022 Google Inc. All rights reserved.
|
|
|
|
*
|
|
|
|
* 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 {Page as PageBase} from '../../api/Page.js';
|
|
|
|
import {Connection} from './Connection.js';
|
2023-02-15 10:29:18 +00:00
|
|
|
import type {EvaluateFunc, HandleFor} from '../types.js';
|
2023-02-15 15:33:18 +00:00
|
|
|
import {isString} from '../util.js';
|
2023-02-02 14:14:28 +00:00
|
|
|
import {BidiSerializer} from './Serializer.js';
|
2023-02-15 10:29:18 +00:00
|
|
|
import {JSHandle} from './JSHandle.js';
|
|
|
|
import {Reference} from './types.js';
|
2023-02-15 15:33:18 +00:00
|
|
|
import {stringifyFunction} from '../../util/Function.js';
|
2023-02-15 10:29:18 +00:00
|
|
|
|
2022-09-21 06:10:50 +00:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
export class Page extends PageBase {
|
|
|
|
#connection: Connection;
|
2023-02-15 10:29:18 +00:00
|
|
|
_contextId: string;
|
2022-09-21 06:10:50 +00:00
|
|
|
|
|
|
|
constructor(connection: Connection, contextId: string) {
|
|
|
|
super();
|
|
|
|
this.#connection = connection;
|
2023-02-15 10:29:18 +00:00
|
|
|
this._contextId = contextId;
|
2022-09-21 06:10:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
override async close(): Promise<void> {
|
|
|
|
await this.#connection.send('browsingContext.close', {
|
2023-02-15 10:29:18 +00:00
|
|
|
context: this._contextId,
|
2022-09-21 06:10:50 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-02-15 10:29:18 +00:00
|
|
|
get connection(): Connection {
|
|
|
|
return this.#connection;
|
|
|
|
}
|
|
|
|
|
|
|
|
override async evaluateHandle<
|
|
|
|
Params extends unknown[],
|
|
|
|
Func extends EvaluateFunc<Params> = EvaluateFunc<Params>
|
|
|
|
>(
|
|
|
|
pageFunction: Func | string,
|
|
|
|
...args: Params
|
|
|
|
): Promise<HandleFor<Awaited<ReturnType<Func>>>> {
|
|
|
|
return this.#evaluate(false, pageFunction, ...args);
|
|
|
|
}
|
|
|
|
|
2022-09-21 06:10:50 +00:00
|
|
|
override async evaluate<
|
|
|
|
Params extends unknown[],
|
|
|
|
Func extends EvaluateFunc<Params> = EvaluateFunc<Params>
|
|
|
|
>(
|
|
|
|
pageFunction: Func | string,
|
2023-02-02 14:14:28 +00:00
|
|
|
...args: Params
|
2022-09-21 06:10:50 +00:00
|
|
|
): Promise<Awaited<ReturnType<Func>>> {
|
2023-02-15 10:29:18 +00:00
|
|
|
return this.#evaluate(true, pageFunction, ...args);
|
|
|
|
}
|
|
|
|
|
|
|
|
async #evaluate<
|
|
|
|
Params extends unknown[],
|
|
|
|
Func extends EvaluateFunc<Params> = EvaluateFunc<Params>
|
|
|
|
>(
|
|
|
|
returnByValue: true,
|
|
|
|
pageFunction: Func | string,
|
|
|
|
...args: Params
|
|
|
|
): Promise<Awaited<ReturnType<Func>>>;
|
|
|
|
async #evaluate<
|
|
|
|
Params extends unknown[],
|
|
|
|
Func extends EvaluateFunc<Params> = EvaluateFunc<Params>
|
|
|
|
>(
|
|
|
|
returnByValue: false,
|
|
|
|
pageFunction: Func | string,
|
|
|
|
...args: Params
|
|
|
|
): Promise<HandleFor<Awaited<ReturnType<Func>>>>;
|
|
|
|
async #evaluate<
|
|
|
|
Params extends unknown[],
|
|
|
|
Func extends EvaluateFunc<Params> = EvaluateFunc<Params>
|
|
|
|
>(
|
|
|
|
returnByValue: boolean,
|
|
|
|
pageFunction: Func | string,
|
|
|
|
...args: Params
|
|
|
|
): Promise<HandleFor<Awaited<ReturnType<Func>>> | Awaited<ReturnType<Func>>> {
|
2023-02-02 14:14:28 +00:00
|
|
|
let responsePromise;
|
2023-02-15 10:29:18 +00:00
|
|
|
const resultOwnership = returnByValue ? 'none' : 'root';
|
2023-02-02 14:14:28 +00:00
|
|
|
if (isString(pageFunction)) {
|
|
|
|
responsePromise = this.#connection.send('script.evaluate', {
|
|
|
|
expression: pageFunction,
|
2023-02-15 10:29:18 +00:00
|
|
|
target: {context: this._contextId},
|
|
|
|
resultOwnership,
|
2023-02-02 14:14:28 +00:00
|
|
|
awaitPromise: true,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
responsePromise = this.#connection.send('script.callFunction', {
|
|
|
|
functionDeclaration: stringifyFunction(pageFunction),
|
2023-02-15 10:29:18 +00:00
|
|
|
arguments: await Promise.all(
|
|
|
|
args.map(arg => {
|
|
|
|
return BidiSerializer.serialize(arg, this);
|
|
|
|
})
|
|
|
|
),
|
|
|
|
target: {context: this._contextId},
|
|
|
|
resultOwnership,
|
2023-02-02 14:14:28 +00:00
|
|
|
awaitPromise: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const {result} = await responsePromise;
|
|
|
|
|
|
|
|
if ('type' in result && result.type === 'exception') {
|
|
|
|
throw new Error(result.exceptionDetails.text);
|
|
|
|
}
|
|
|
|
|
2023-02-15 10:29:18 +00:00
|
|
|
return returnByValue
|
|
|
|
? BidiSerializer.deserialize(result.result)
|
|
|
|
: getBidiHandle(this, result.result as Reference);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
export function getBidiHandle(context: Page, result: Reference): JSHandle {
|
|
|
|
// TODO: | ElementHandle<Node>
|
|
|
|
if (
|
|
|
|
(result.type === 'node' || result.type === 'window') &&
|
|
|
|
context._contextId
|
|
|
|
) {
|
|
|
|
throw new Error('ElementHandle not implemented');
|
2022-09-21 06:10:50 +00:00
|
|
|
}
|
2023-02-15 10:29:18 +00:00
|
|
|
return new JSHandle(context, result);
|
2022-09-21 06:10:50 +00:00
|
|
|
}
|