2017-10-06 22:35:02 +00:00
|
|
|
/**
|
|
|
|
* Copyright 2017 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.
|
|
|
|
*/
|
|
|
|
|
2022-06-22 13:25:44 +00:00
|
|
|
import {Protocol} from 'devtools-protocol';
|
2022-08-17 12:39:41 +00:00
|
|
|
import {assert} from '../util/assert.js';
|
2022-06-22 13:25:44 +00:00
|
|
|
import {CDPSession} from './Connection.js';
|
2022-08-17 15:45:45 +00:00
|
|
|
import {Frame} from './Frame.js';
|
2022-08-11 09:45:35 +00:00
|
|
|
import {IsolatedWorld} from './IsolatedWorld.js';
|
2022-06-23 09:31:43 +00:00
|
|
|
import {JSHandle} from './JSHandle.js';
|
2022-06-24 06:40:08 +00:00
|
|
|
import {EvaluateFunc, HandleFor} from './types.js';
|
2022-06-23 09:31:43 +00:00
|
|
|
import {
|
2022-08-11 09:45:35 +00:00
|
|
|
createJSHandle,
|
2022-06-23 09:31:43 +00:00
|
|
|
getExceptionMessage,
|
|
|
|
isString,
|
|
|
|
valueFromRemoteObject,
|
|
|
|
} from './util.js';
|
2022-06-14 11:16:21 +00:00
|
|
|
|
2021-04-06 08:58:01 +00:00
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
2022-06-07 13:36:51 +00:00
|
|
|
export const EVALUATION_SCRIPT_URL = 'pptr://__puppeteer_evaluation_script__';
|
2018-07-12 01:38:34 +00:00
|
|
|
const SOURCE_URL_REGEX = /^[\040\t]*\/\/[@#] sourceURL=\s*(\S*?)\s*$/m;
|
|
|
|
|
2020-06-24 15:11:10 +00:00
|
|
|
/**
|
2022-08-11 09:45:35 +00:00
|
|
|
* Represents a context for JavaScript execution.
|
|
|
|
*
|
|
|
|
* @example
|
|
|
|
* A {@link Page} can have several execution contexts:
|
2020-06-24 15:11:10 +00:00
|
|
|
*
|
2022-08-11 09:45:35 +00:00
|
|
|
* - Each {@link Frame} of a {@link Page | page} has a "default" execution
|
|
|
|
* context that is always created after frame is attached to DOM. This context
|
|
|
|
* is returned by the {@link Frame.executionContext} method.
|
|
|
|
* - Each {@link https://developer.chrome.com/extensions | Chrome extensions}
|
|
|
|
* creates additional execution contexts to isolate their code.
|
|
|
|
*
|
|
|
|
* @remarks
|
|
|
|
* By definition, each context is isolated from one another, however they are
|
|
|
|
* all able to manipulate non-JavaScript resources (such as DOM).
|
|
|
|
*
|
|
|
|
* @remarks
|
|
|
|
* Besides pages, execution contexts can be found in
|
|
|
|
* {@link WebWorker | workers}.
|
2022-08-25 15:02:44 +00:00
|
|
|
*
|
|
|
|
* @internal
|
2020-06-24 15:11:10 +00:00
|
|
|
*/
|
chore: migrate src/ExecutionContext (#5705)
* chore: migrate src/ExecutionContext to TypeScript
I spent a while trying to decide on the best course of action for
typing the `evaluate` function.
Ideally I wanted to use generics so that as a user you could type
something like:
```
handle.evaluate<HTMLElement, number, boolean>((node, x) => true, 5)
```
And have TypeScript know the arguments of `node` and `x` based on those
generics. But I hit two problems with that:
* you have to have n overloads of `evaluate` to cope for as many number
of arguments as you can be bothered too (e.g. we'd need an overload for
1 arg, 2 args, 3 args, etc)
* I decided it's actually confusing because you don't know as a user
what those generics actually map to.
So in the end I went with one generic which is the return type of the
function:
```
handle.evaluate<boolean>((node, x) => true, 5)
```
And `node` and `x` get typed as `any` which means you can tell TS
yourself:
```
handle.evaluate<boolean>((node: HTMLElement, x: number) => true, 5)
```
I'd like to find a way to force that the arguments after the function do
match the arguments you've given (in the above example, TS would moan if
I swapped that `5` for `"foo"`), but I tried a few things and to be
honest the complexity of the types wasn't worth it, I don't think.
I'm very open to tweaking these but I'd rather ship this and tweak going
forwards rather than spend hours now tweaking. Once we ship these
typedefs and get feedback from the community I'm sure we can improve
them.
2020-04-22 09:33:44 +00:00
|
|
|
export class ExecutionContext {
|
2020-06-24 15:11:10 +00:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
chore: migrate src/ExecutionContext (#5705)
* chore: migrate src/ExecutionContext to TypeScript
I spent a while trying to decide on the best course of action for
typing the `evaluate` function.
Ideally I wanted to use generics so that as a user you could type
something like:
```
handle.evaluate<HTMLElement, number, boolean>((node, x) => true, 5)
```
And have TypeScript know the arguments of `node` and `x` based on those
generics. But I hit two problems with that:
* you have to have n overloads of `evaluate` to cope for as many number
of arguments as you can be bothered too (e.g. we'd need an overload for
1 arg, 2 args, 3 args, etc)
* I decided it's actually confusing because you don't know as a user
what those generics actually map to.
So in the end I went with one generic which is the return type of the
function:
```
handle.evaluate<boolean>((node, x) => true, 5)
```
And `node` and `x` get typed as `any` which means you can tell TS
yourself:
```
handle.evaluate<boolean>((node: HTMLElement, x: number) => true, 5)
```
I'd like to find a way to force that the arguments after the function do
match the arguments you've given (in the above example, TS would moan if
I swapped that `5` for `"foo"`), but I tried a few things and to be
honest the complexity of the types wasn't worth it, I don't think.
I'm very open to tweaking these but I'd rather ship this and tweak going
forwards rather than spend hours now tweaking. Once we ship these
typedefs and get feedback from the community I'm sure we can improve
them.
2020-04-22 09:33:44 +00:00
|
|
|
_client: CDPSession;
|
2020-06-24 15:11:10 +00:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2022-08-09 13:17:42 +00:00
|
|
|
_world?: IsolatedWorld;
|
2020-10-07 08:49:11 +00:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
_contextId: number;
|
2021-01-25 12:01:59 +00:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
_contextName: string;
|
chore: migrate src/ExecutionContext (#5705)
* chore: migrate src/ExecutionContext to TypeScript
I spent a while trying to decide on the best course of action for
typing the `evaluate` function.
Ideally I wanted to use generics so that as a user you could type
something like:
```
handle.evaluate<HTMLElement, number, boolean>((node, x) => true, 5)
```
And have TypeScript know the arguments of `node` and `x` based on those
generics. But I hit two problems with that:
* you have to have n overloads of `evaluate` to cope for as many number
of arguments as you can be bothered too (e.g. we'd need an overload for
1 arg, 2 args, 3 args, etc)
* I decided it's actually confusing because you don't know as a user
what those generics actually map to.
So in the end I went with one generic which is the return type of the
function:
```
handle.evaluate<boolean>((node, x) => true, 5)
```
And `node` and `x` get typed as `any` which means you can tell TS
yourself:
```
handle.evaluate<boolean>((node: HTMLElement, x: number) => true, 5)
```
I'd like to find a way to force that the arguments after the function do
match the arguments you've given (in the above example, TS would moan if
I swapped that `5` for `"foo"`), but I tried a few things and to be
honest the complexity of the types wasn't worth it, I don't think.
I'm very open to tweaking these but I'd rather ship this and tweak going
forwards rather than spend hours now tweaking. Once we ship these
typedefs and get feedback from the community I'm sure we can improve
them.
2020-04-22 09:33:44 +00:00
|
|
|
|
2020-06-24 15:11:10 +00:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2020-05-07 10:54:55 +00:00
|
|
|
constructor(
|
|
|
|
client: CDPSession,
|
|
|
|
contextPayload: Protocol.Runtime.ExecutionContextDescription,
|
2022-08-09 13:17:42 +00:00
|
|
|
world?: IsolatedWorld
|
2020-05-07 10:54:55 +00:00
|
|
|
) {
|
2017-10-06 22:35:02 +00:00
|
|
|
this._client = client;
|
2019-01-22 22:55:33 +00:00
|
|
|
this._world = world;
|
2017-11-19 00:27:52 +00:00
|
|
|
this._contextId = contextPayload.id;
|
2021-01-25 12:01:59 +00:00
|
|
|
this._contextName = contextPayload.name;
|
2017-10-06 22:35:02 +00:00
|
|
|
}
|
|
|
|
|
2020-06-24 15:11:10 +00:00
|
|
|
/**
|
2022-08-11 09:45:35 +00:00
|
|
|
* @returns The frame associated with this execution context.
|
2020-06-24 15:11:10 +00:00
|
|
|
*
|
2022-08-11 09:45:35 +00:00
|
|
|
* @remarks
|
2022-08-09 12:55:18 +00:00
|
|
|
* Not every execution context is associated with a frame. For example,
|
2022-08-11 09:45:35 +00:00
|
|
|
* {@link WebWorker | workers} have execution contexts that are not associated
|
|
|
|
* with frames.
|
2020-06-24 15:11:10 +00:00
|
|
|
*/
|
2020-04-29 11:28:16 +00:00
|
|
|
frame(): Frame | null {
|
2019-01-22 22:55:33 +00:00
|
|
|
return this._world ? this._world.frame() : null;
|
2018-02-13 22:02:44 +00:00
|
|
|
}
|
|
|
|
|
2020-06-24 15:11:10 +00:00
|
|
|
/**
|
2022-08-11 09:45:35 +00:00
|
|
|
* Evaluates the given function.
|
2020-06-24 15:11:10 +00:00
|
|
|
*
|
|
|
|
* @example
|
2022-08-12 12:15:26 +00:00
|
|
|
*
|
2022-07-01 11:52:39 +00:00
|
|
|
* ```ts
|
2020-06-24 15:11:10 +00:00
|
|
|
* const executionContext = await page.mainFrame().executionContext();
|
|
|
|
* const result = await executionContext.evaluate(() => Promise.resolve(8 * 7))* ;
|
|
|
|
* console.log(result); // prints "56"
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @example
|
2022-08-11 09:45:35 +00:00
|
|
|
* A string can also be passed in instead of a function:
|
2022-08-12 12:15:26 +00:00
|
|
|
*
|
2022-07-01 11:52:39 +00:00
|
|
|
* ```ts
|
2020-06-24 15:11:10 +00:00
|
|
|
* console.log(await executionContext.evaluate('1 + 2')); // prints "3"
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @example
|
2022-08-11 09:45:35 +00:00
|
|
|
* Handles can also be passed as `args`. They resolve to their referenced object:
|
2022-08-12 12:15:26 +00:00
|
|
|
*
|
2022-07-01 11:52:39 +00:00
|
|
|
* ```ts
|
2020-06-24 15:11:10 +00:00
|
|
|
* const oneHandle = await executionContext.evaluateHandle(() => 1);
|
|
|
|
* const twoHandle = await executionContext.evaluateHandle(() => 2);
|
|
|
|
* const result = await executionContext.evaluate(
|
2022-08-12 12:15:26 +00:00
|
|
|
* (a, b) => a + b,
|
|
|
|
* oneHandle,
|
|
|
|
* twoHandle
|
2020-06-24 15:11:10 +00:00
|
|
|
* );
|
|
|
|
* await oneHandle.dispose();
|
|
|
|
* await twoHandle.dispose();
|
|
|
|
* console.log(result); // prints '3'.
|
|
|
|
* ```
|
|
|
|
*
|
2022-08-11 09:45:35 +00:00
|
|
|
* @param pageFunction - The function to evaluate.
|
|
|
|
* @param args - Additional arguments to pass into the function.
|
|
|
|
* @returns The result of evaluating the function. If the result is an object,
|
|
|
|
* a vanilla object containing the serializable properties of the result is
|
|
|
|
* returned.
|
2020-06-24 15:11:10 +00:00
|
|
|
*/
|
feat!: type inference for evaluation types (#8547)
This PR greatly improves the types within Puppeteer:
- **Almost everything** is auto-deduced.
- Parameters don't need to be specified in the function. They are deduced from the spread.
- Return types don't need to be specified. They are deduced from the function. (More on this below)
- Selections based on tag names correctly deduce element type, similar to TypeScript's mechanism for `getElementByTagName`.
- [**BREAKING CHANGE**] We've removed the ability to declare return types in type arguments for the following reasons:
1. Setting them will indubitably break auto-deduction.
2. You can just use `as ...` in TypeScript to coerce the correct type (given it makes sense).
- [**BREAKING CHANGE**] `waitFor` is officially gone.
To migrate to these changes, there are only four things you may need to change:
- If you set a return type using the `ReturnType` type parameter, remove it and use `as ...` and `HandleFor` (if necessary).
⛔ `evaluate<ReturnType>(a: number, b: number) => {...}, a, b)`
✅ `(await evaluate(a, b) => {...}, a, b)) as ReturnType`
⛔ `evaluateHandle<ReturnType>(a: number, b: number) => {...}, a, b)`
✅ `(await evaluateHandle(a, b) => {...}, a, b)) as HandleFor<ReturnType>`
- If you set any type parameters in the *parameters* of an evaluation function, remove them.
⛔ `evaluate(a: number, b: number) => {...}, a, b)`
✅ `evaluate(a, b) => {...}, a, b)`
- If you set any type parameters in the method's declaration, remove them.
⛔ `evaluate<(a: number, b: number) => void>((a, b) => {...}, a, b)`
✅ `evaluate(a, b) => {...}, a, b)`
2022-06-23 09:29:46 +00:00
|
|
|
async evaluate<
|
|
|
|
Params extends unknown[],
|
|
|
|
Func extends EvaluateFunc<Params> = EvaluateFunc<Params>
|
|
|
|
>(
|
|
|
|
pageFunction: Func | string,
|
2022-06-24 06:40:08 +00:00
|
|
|
...args: Params
|
feat!: type inference for evaluation types (#8547)
This PR greatly improves the types within Puppeteer:
- **Almost everything** is auto-deduced.
- Parameters don't need to be specified in the function. They are deduced from the spread.
- Return types don't need to be specified. They are deduced from the function. (More on this below)
- Selections based on tag names correctly deduce element type, similar to TypeScript's mechanism for `getElementByTagName`.
- [**BREAKING CHANGE**] We've removed the ability to declare return types in type arguments for the following reasons:
1. Setting them will indubitably break auto-deduction.
2. You can just use `as ...` in TypeScript to coerce the correct type (given it makes sense).
- [**BREAKING CHANGE**] `waitFor` is officially gone.
To migrate to these changes, there are only four things you may need to change:
- If you set a return type using the `ReturnType` type parameter, remove it and use `as ...` and `HandleFor` (if necessary).
⛔ `evaluate<ReturnType>(a: number, b: number) => {...}, a, b)`
✅ `(await evaluate(a, b) => {...}, a, b)) as ReturnType`
⛔ `evaluateHandle<ReturnType>(a: number, b: number) => {...}, a, b)`
✅ `(await evaluateHandle(a, b) => {...}, a, b)) as HandleFor<ReturnType>`
- If you set any type parameters in the *parameters* of an evaluation function, remove them.
⛔ `evaluate(a: number, b: number) => {...}, a, b)`
✅ `evaluate(a, b) => {...}, a, b)`
- If you set any type parameters in the method's declaration, remove them.
⛔ `evaluate<(a: number, b: number) => void>((a, b) => {...}, a, b)`
✅ `evaluate(a, b) => {...}, a, b)`
2022-06-23 09:29:46 +00:00
|
|
|
): Promise<Awaited<ReturnType<Func>>> {
|
|
|
|
return await this.#evaluate(true, pageFunction, ...args);
|
2017-10-06 22:35:02 +00:00
|
|
|
}
|
|
|
|
|
2020-06-24 15:11:10 +00:00
|
|
|
/**
|
2022-08-11 09:45:35 +00:00
|
|
|
* Evaluates the given function.
|
|
|
|
*
|
|
|
|
* Unlike {@link ExecutionContext.evaluate | evaluate}, this method returns a
|
|
|
|
* handle to the result of the function.
|
|
|
|
*
|
|
|
|
* This method may be better suited if the object cannot be serialized (e.g.
|
|
|
|
* `Map`) and requires further manipulation.
|
2020-06-24 15:11:10 +00:00
|
|
|
*
|
|
|
|
* @example
|
2022-08-12 12:15:26 +00:00
|
|
|
*
|
2022-07-01 11:52:39 +00:00
|
|
|
* ```ts
|
2020-06-24 15:11:10 +00:00
|
|
|
* const context = await page.mainFrame().executionContext();
|
2022-08-12 12:15:26 +00:00
|
|
|
* const handle: JSHandle<typeof globalThis> = await context.evaluateHandle(
|
|
|
|
* () => Promise.resolve(self)
|
2022-08-11 09:45:35 +00:00
|
|
|
* );
|
2020-06-24 15:11:10 +00:00
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @example
|
|
|
|
* A string can also be passed in instead of a function.
|
2022-08-12 12:15:26 +00:00
|
|
|
*
|
2022-07-01 11:52:39 +00:00
|
|
|
* ```ts
|
2022-08-11 09:45:35 +00:00
|
|
|
* const handle: JSHandle<number> = await context.evaluateHandle('1 + 2');
|
2020-06-24 15:11:10 +00:00
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @example
|
2022-08-11 09:45:35 +00:00
|
|
|
* Handles can also be passed as `args`. They resolve to their referenced object:
|
2022-08-12 12:15:26 +00:00
|
|
|
*
|
2022-07-01 11:52:39 +00:00
|
|
|
* ```ts
|
2022-08-12 12:15:26 +00:00
|
|
|
* const bodyHandle: ElementHandle<HTMLBodyElement> =
|
|
|
|
* await context.evaluateHandle(() => {
|
2022-08-11 09:45:35 +00:00
|
|
|
* return document.body;
|
2022-08-12 12:15:26 +00:00
|
|
|
* });
|
2022-08-11 09:45:35 +00:00
|
|
|
* const stringHandle: JSHandle<string> = await context.evaluateHandle(
|
|
|
|
* body => body.innerHTML,
|
|
|
|
* body
|
|
|
|
* );
|
|
|
|
* console.log(await stringHandle.jsonValue()); // prints body's innerHTML
|
|
|
|
* // Always dispose your garbage! :)
|
|
|
|
* await bodyHandle.dispose();
|
|
|
|
* await stringHandle.dispose();
|
2020-06-24 15:11:10 +00:00
|
|
|
* ```
|
|
|
|
*
|
2022-08-11 09:45:35 +00:00
|
|
|
* @param pageFunction - The function to evaluate.
|
|
|
|
* @param args - Additional arguments to pass into the function.
|
|
|
|
* @returns A {@link JSHandle | handle} to the result of evaluating the
|
|
|
|
* function. If the result is a `Node`, then this will return an
|
|
|
|
* {@link ElementHandle | element handle}.
|
2020-06-24 15:11:10 +00:00
|
|
|
*/
|
feat!: type inference for evaluation types (#8547)
This PR greatly improves the types within Puppeteer:
- **Almost everything** is auto-deduced.
- Parameters don't need to be specified in the function. They are deduced from the spread.
- Return types don't need to be specified. They are deduced from the function. (More on this below)
- Selections based on tag names correctly deduce element type, similar to TypeScript's mechanism for `getElementByTagName`.
- [**BREAKING CHANGE**] We've removed the ability to declare return types in type arguments for the following reasons:
1. Setting them will indubitably break auto-deduction.
2. You can just use `as ...` in TypeScript to coerce the correct type (given it makes sense).
- [**BREAKING CHANGE**] `waitFor` is officially gone.
To migrate to these changes, there are only four things you may need to change:
- If you set a return type using the `ReturnType` type parameter, remove it and use `as ...` and `HandleFor` (if necessary).
⛔ `evaluate<ReturnType>(a: number, b: number) => {...}, a, b)`
✅ `(await evaluate(a, b) => {...}, a, b)) as ReturnType`
⛔ `evaluateHandle<ReturnType>(a: number, b: number) => {...}, a, b)`
✅ `(await evaluateHandle(a, b) => {...}, a, b)) as HandleFor<ReturnType>`
- If you set any type parameters in the *parameters* of an evaluation function, remove them.
⛔ `evaluate(a: number, b: number) => {...}, a, b)`
✅ `evaluate(a, b) => {...}, a, b)`
- If you set any type parameters in the method's declaration, remove them.
⛔ `evaluate<(a: number, b: number) => void>((a, b) => {...}, a, b)`
✅ `evaluate(a, b) => {...}, a, b)`
2022-06-23 09:29:46 +00:00
|
|
|
async evaluateHandle<
|
|
|
|
Params extends unknown[],
|
|
|
|
Func extends EvaluateFunc<Params> = EvaluateFunc<Params>
|
|
|
|
>(
|
|
|
|
pageFunction: Func | string,
|
2022-06-24 06:40:08 +00:00
|
|
|
...args: Params
|
feat!: type inference for evaluation types (#8547)
This PR greatly improves the types within Puppeteer:
- **Almost everything** is auto-deduced.
- Parameters don't need to be specified in the function. They are deduced from the spread.
- Return types don't need to be specified. They are deduced from the function. (More on this below)
- Selections based on tag names correctly deduce element type, similar to TypeScript's mechanism for `getElementByTagName`.
- [**BREAKING CHANGE**] We've removed the ability to declare return types in type arguments for the following reasons:
1. Setting them will indubitably break auto-deduction.
2. You can just use `as ...` in TypeScript to coerce the correct type (given it makes sense).
- [**BREAKING CHANGE**] `waitFor` is officially gone.
To migrate to these changes, there are only four things you may need to change:
- If you set a return type using the `ReturnType` type parameter, remove it and use `as ...` and `HandleFor` (if necessary).
⛔ `evaluate<ReturnType>(a: number, b: number) => {...}, a, b)`
✅ `(await evaluate(a, b) => {...}, a, b)) as ReturnType`
⛔ `evaluateHandle<ReturnType>(a: number, b: number) => {...}, a, b)`
✅ `(await evaluateHandle(a, b) => {...}, a, b)) as HandleFor<ReturnType>`
- If you set any type parameters in the *parameters* of an evaluation function, remove them.
⛔ `evaluate(a: number, b: number) => {...}, a, b)`
✅ `evaluate(a, b) => {...}, a, b)`
- If you set any type parameters in the method's declaration, remove them.
⛔ `evaluate<(a: number, b: number) => void>((a, b) => {...}, a, b)`
✅ `evaluate(a, b) => {...}, a, b)`
2022-06-23 09:29:46 +00:00
|
|
|
): Promise<HandleFor<Awaited<ReturnType<Func>>>> {
|
|
|
|
return this.#evaluate(false, pageFunction, ...args);
|
2019-06-07 20:46:43 +00:00
|
|
|
}
|
|
|
|
|
feat!: type inference for evaluation types (#8547)
This PR greatly improves the types within Puppeteer:
- **Almost everything** is auto-deduced.
- Parameters don't need to be specified in the function. They are deduced from the spread.
- Return types don't need to be specified. They are deduced from the function. (More on this below)
- Selections based on tag names correctly deduce element type, similar to TypeScript's mechanism for `getElementByTagName`.
- [**BREAKING CHANGE**] We've removed the ability to declare return types in type arguments for the following reasons:
1. Setting them will indubitably break auto-deduction.
2. You can just use `as ...` in TypeScript to coerce the correct type (given it makes sense).
- [**BREAKING CHANGE**] `waitFor` is officially gone.
To migrate to these changes, there are only four things you may need to change:
- If you set a return type using the `ReturnType` type parameter, remove it and use `as ...` and `HandleFor` (if necessary).
⛔ `evaluate<ReturnType>(a: number, b: number) => {...}, a, b)`
✅ `(await evaluate(a, b) => {...}, a, b)) as ReturnType`
⛔ `evaluateHandle<ReturnType>(a: number, b: number) => {...}, a, b)`
✅ `(await evaluateHandle(a, b) => {...}, a, b)) as HandleFor<ReturnType>`
- If you set any type parameters in the *parameters* of an evaluation function, remove them.
⛔ `evaluate(a: number, b: number) => {...}, a, b)`
✅ `evaluate(a, b) => {...}, a, b)`
- If you set any type parameters in the method's declaration, remove them.
⛔ `evaluate<(a: number, b: number) => void>((a, b) => {...}, a, b)`
✅ `evaluate(a, b) => {...}, a, b)`
2022-06-23 09:29:46 +00:00
|
|
|
async #evaluate<
|
|
|
|
Params extends unknown[],
|
|
|
|
Func extends EvaluateFunc<Params> = EvaluateFunc<Params>
|
|
|
|
>(
|
|
|
|
returnByValue: true,
|
|
|
|
pageFunction: Func | string,
|
2022-06-24 06:40:08 +00:00
|
|
|
...args: Params
|
feat!: type inference for evaluation types (#8547)
This PR greatly improves the types within Puppeteer:
- **Almost everything** is auto-deduced.
- Parameters don't need to be specified in the function. They are deduced from the spread.
- Return types don't need to be specified. They are deduced from the function. (More on this below)
- Selections based on tag names correctly deduce element type, similar to TypeScript's mechanism for `getElementByTagName`.
- [**BREAKING CHANGE**] We've removed the ability to declare return types in type arguments for the following reasons:
1. Setting them will indubitably break auto-deduction.
2. You can just use `as ...` in TypeScript to coerce the correct type (given it makes sense).
- [**BREAKING CHANGE**] `waitFor` is officially gone.
To migrate to these changes, there are only four things you may need to change:
- If you set a return type using the `ReturnType` type parameter, remove it and use `as ...` and `HandleFor` (if necessary).
⛔ `evaluate<ReturnType>(a: number, b: number) => {...}, a, b)`
✅ `(await evaluate(a, b) => {...}, a, b)) as ReturnType`
⛔ `evaluateHandle<ReturnType>(a: number, b: number) => {...}, a, b)`
✅ `(await evaluateHandle(a, b) => {...}, a, b)) as HandleFor<ReturnType>`
- If you set any type parameters in the *parameters* of an evaluation function, remove them.
⛔ `evaluate(a: number, b: number) => {...}, a, b)`
✅ `evaluate(a, b) => {...}, a, b)`
- If you set any type parameters in the method's declaration, remove them.
⛔ `evaluate<(a: number, b: number) => void>((a, b) => {...}, a, b)`
✅ `evaluate(a, b) => {...}, a, b)`
2022-06-23 09:29:46 +00:00
|
|
|
): Promise<Awaited<ReturnType<Func>>>;
|
|
|
|
async #evaluate<
|
|
|
|
Params extends unknown[],
|
|
|
|
Func extends EvaluateFunc<Params> = EvaluateFunc<Params>
|
|
|
|
>(
|
|
|
|
returnByValue: false,
|
|
|
|
pageFunction: Func | string,
|
2022-06-24 06:40:08 +00:00
|
|
|
...args: Params
|
feat!: type inference for evaluation types (#8547)
This PR greatly improves the types within Puppeteer:
- **Almost everything** is auto-deduced.
- Parameters don't need to be specified in the function. They are deduced from the spread.
- Return types don't need to be specified. They are deduced from the function. (More on this below)
- Selections based on tag names correctly deduce element type, similar to TypeScript's mechanism for `getElementByTagName`.
- [**BREAKING CHANGE**] We've removed the ability to declare return types in type arguments for the following reasons:
1. Setting them will indubitably break auto-deduction.
2. You can just use `as ...` in TypeScript to coerce the correct type (given it makes sense).
- [**BREAKING CHANGE**] `waitFor` is officially gone.
To migrate to these changes, there are only four things you may need to change:
- If you set a return type using the `ReturnType` type parameter, remove it and use `as ...` and `HandleFor` (if necessary).
⛔ `evaluate<ReturnType>(a: number, b: number) => {...}, a, b)`
✅ `(await evaluate(a, b) => {...}, a, b)) as ReturnType`
⛔ `evaluateHandle<ReturnType>(a: number, b: number) => {...}, a, b)`
✅ `(await evaluateHandle(a, b) => {...}, a, b)) as HandleFor<ReturnType>`
- If you set any type parameters in the *parameters* of an evaluation function, remove them.
⛔ `evaluate(a: number, b: number) => {...}, a, b)`
✅ `evaluate(a, b) => {...}, a, b)`
- If you set any type parameters in the method's declaration, remove them.
⛔ `evaluate<(a: number, b: number) => void>((a, b) => {...}, a, b)`
✅ `evaluate(a, b) => {...}, a, b)`
2022-06-23 09:29:46 +00:00
|
|
|
): Promise<HandleFor<Awaited<ReturnType<Func>>>>;
|
|
|
|
async #evaluate<
|
|
|
|
Params extends unknown[],
|
|
|
|
Func extends EvaluateFunc<Params> = EvaluateFunc<Params>
|
|
|
|
>(
|
2020-07-01 11:44:08 +00:00
|
|
|
returnByValue: boolean,
|
feat!: type inference for evaluation types (#8547)
This PR greatly improves the types within Puppeteer:
- **Almost everything** is auto-deduced.
- Parameters don't need to be specified in the function. They are deduced from the spread.
- Return types don't need to be specified. They are deduced from the function. (More on this below)
- Selections based on tag names correctly deduce element type, similar to TypeScript's mechanism for `getElementByTagName`.
- [**BREAKING CHANGE**] We've removed the ability to declare return types in type arguments for the following reasons:
1. Setting them will indubitably break auto-deduction.
2. You can just use `as ...` in TypeScript to coerce the correct type (given it makes sense).
- [**BREAKING CHANGE**] `waitFor` is officially gone.
To migrate to these changes, there are only four things you may need to change:
- If you set a return type using the `ReturnType` type parameter, remove it and use `as ...` and `HandleFor` (if necessary).
⛔ `evaluate<ReturnType>(a: number, b: number) => {...}, a, b)`
✅ `(await evaluate(a, b) => {...}, a, b)) as ReturnType`
⛔ `evaluateHandle<ReturnType>(a: number, b: number) => {...}, a, b)`
✅ `(await evaluateHandle(a, b) => {...}, a, b)) as HandleFor<ReturnType>`
- If you set any type parameters in the *parameters* of an evaluation function, remove them.
⛔ `evaluate(a: number, b: number) => {...}, a, b)`
✅ `evaluate(a, b) => {...}, a, b)`
- If you set any type parameters in the method's declaration, remove them.
⛔ `evaluate<(a: number, b: number) => void>((a, b) => {...}, a, b)`
✅ `evaluate(a, b) => {...}, a, b)`
2022-06-23 09:29:46 +00:00
|
|
|
pageFunction: Func | string,
|
2022-06-24 06:40:08 +00:00
|
|
|
...args: Params
|
feat!: type inference for evaluation types (#8547)
This PR greatly improves the types within Puppeteer:
- **Almost everything** is auto-deduced.
- Parameters don't need to be specified in the function. They are deduced from the spread.
- Return types don't need to be specified. They are deduced from the function. (More on this below)
- Selections based on tag names correctly deduce element type, similar to TypeScript's mechanism for `getElementByTagName`.
- [**BREAKING CHANGE**] We've removed the ability to declare return types in type arguments for the following reasons:
1. Setting them will indubitably break auto-deduction.
2. You can just use `as ...` in TypeScript to coerce the correct type (given it makes sense).
- [**BREAKING CHANGE**] `waitFor` is officially gone.
To migrate to these changes, there are only four things you may need to change:
- If you set a return type using the `ReturnType` type parameter, remove it and use `as ...` and `HandleFor` (if necessary).
⛔ `evaluate<ReturnType>(a: number, b: number) => {...}, a, b)`
✅ `(await evaluate(a, b) => {...}, a, b)) as ReturnType`
⛔ `evaluateHandle<ReturnType>(a: number, b: number) => {...}, a, b)`
✅ `(await evaluateHandle(a, b) => {...}, a, b)) as HandleFor<ReturnType>`
- If you set any type parameters in the *parameters* of an evaluation function, remove them.
⛔ `evaluate(a: number, b: number) => {...}, a, b)`
✅ `evaluate(a, b) => {...}, a, b)`
- If you set any type parameters in the method's declaration, remove them.
⛔ `evaluate<(a: number, b: number) => void>((a, b) => {...}, a, b)`
✅ `evaluate(a, b) => {...}, a, b)`
2022-06-23 09:29:46 +00:00
|
|
|
): Promise<HandleFor<Awaited<ReturnType<Func>>> | Awaited<ReturnType<Func>>> {
|
2018-07-12 01:38:34 +00:00
|
|
|
const suffix = `//# sourceURL=${EVALUATION_SCRIPT_URL}`;
|
|
|
|
|
2022-06-14 11:16:21 +00:00
|
|
|
if (isString(pageFunction)) {
|
2017-10-06 22:35:02 +00:00
|
|
|
const contextId = this._contextId;
|
chore: migrate src/ExecutionContext (#5705)
* chore: migrate src/ExecutionContext to TypeScript
I spent a while trying to decide on the best course of action for
typing the `evaluate` function.
Ideally I wanted to use generics so that as a user you could type
something like:
```
handle.evaluate<HTMLElement, number, boolean>((node, x) => true, 5)
```
And have TypeScript know the arguments of `node` and `x` based on those
generics. But I hit two problems with that:
* you have to have n overloads of `evaluate` to cope for as many number
of arguments as you can be bothered too (e.g. we'd need an overload for
1 arg, 2 args, 3 args, etc)
* I decided it's actually confusing because you don't know as a user
what those generics actually map to.
So in the end I went with one generic which is the return type of the
function:
```
handle.evaluate<boolean>((node, x) => true, 5)
```
And `node` and `x` get typed as `any` which means you can tell TS
yourself:
```
handle.evaluate<boolean>((node: HTMLElement, x: number) => true, 5)
```
I'd like to find a way to force that the arguments after the function do
match the arguments you've given (in the above example, TS would moan if
I swapped that `5` for `"foo"`), but I tried a few things and to be
honest the complexity of the types wasn't worth it, I don't think.
I'm very open to tweaking these but I'd rather ship this and tweak going
forwards rather than spend hours now tweaking. Once we ship these
typedefs and get feedback from the community I'm sure we can improve
them.
2020-04-22 09:33:44 +00:00
|
|
|
const expression = pageFunction;
|
2020-05-07 10:54:55 +00:00
|
|
|
const expressionWithSourceUrl = SOURCE_URL_REGEX.test(expression)
|
|
|
|
? expression
|
|
|
|
: expression + '\n' + suffix;
|
|
|
|
|
2022-06-22 13:25:44 +00:00
|
|
|
const {exceptionDetails, result: remoteObject} = await this._client
|
2020-05-07 10:54:55 +00:00
|
|
|
.send('Runtime.evaluate', {
|
|
|
|
expression: expressionWithSourceUrl,
|
|
|
|
contextId,
|
|
|
|
returnByValue,
|
|
|
|
awaitPromise: true,
|
|
|
|
userGesture: true,
|
|
|
|
})
|
|
|
|
.catch(rewriteError);
|
chore: migrate src/ExecutionContext (#5705)
* chore: migrate src/ExecutionContext to TypeScript
I spent a while trying to decide on the best course of action for
typing the `evaluate` function.
Ideally I wanted to use generics so that as a user you could type
something like:
```
handle.evaluate<HTMLElement, number, boolean>((node, x) => true, 5)
```
And have TypeScript know the arguments of `node` and `x` based on those
generics. But I hit two problems with that:
* you have to have n overloads of `evaluate` to cope for as many number
of arguments as you can be bothered too (e.g. we'd need an overload for
1 arg, 2 args, 3 args, etc)
* I decided it's actually confusing because you don't know as a user
what those generics actually map to.
So in the end I went with one generic which is the return type of the
function:
```
handle.evaluate<boolean>((node, x) => true, 5)
```
And `node` and `x` get typed as `any` which means you can tell TS
yourself:
```
handle.evaluate<boolean>((node: HTMLElement, x: number) => true, 5)
```
I'd like to find a way to force that the arguments after the function do
match the arguments you've given (in the above example, TS would moan if
I swapped that `5` for `"foo"`), but I tried a few things and to be
honest the complexity of the types wasn't worth it, I don't think.
I'm very open to tweaking these but I'd rather ship this and tweak going
forwards rather than spend hours now tweaking. Once we ship these
typedefs and get feedback from the community I'm sure we can improve
them.
2020-04-22 09:33:44 +00:00
|
|
|
|
2022-06-14 11:55:35 +00:00
|
|
|
if (exceptionDetails) {
|
2020-05-07 10:54:55 +00:00
|
|
|
throw new Error(
|
2022-06-14 11:16:21 +00:00
|
|
|
'Evaluation failed: ' + getExceptionMessage(exceptionDetails)
|
2020-05-07 10:54:55 +00:00
|
|
|
);
|
2022-06-14 11:55:35 +00:00
|
|
|
}
|
chore: migrate src/ExecutionContext (#5705)
* chore: migrate src/ExecutionContext to TypeScript
I spent a while trying to decide on the best course of action for
typing the `evaluate` function.
Ideally I wanted to use generics so that as a user you could type
something like:
```
handle.evaluate<HTMLElement, number, boolean>((node, x) => true, 5)
```
And have TypeScript know the arguments of `node` and `x` based on those
generics. But I hit two problems with that:
* you have to have n overloads of `evaluate` to cope for as many number
of arguments as you can be bothered too (e.g. we'd need an overload for
1 arg, 2 args, 3 args, etc)
* I decided it's actually confusing because you don't know as a user
what those generics actually map to.
So in the end I went with one generic which is the return type of the
function:
```
handle.evaluate<boolean>((node, x) => true, 5)
```
And `node` and `x` get typed as `any` which means you can tell TS
yourself:
```
handle.evaluate<boolean>((node: HTMLElement, x: number) => true, 5)
```
I'd like to find a way to force that the arguments after the function do
match the arguments you've given (in the above example, TS would moan if
I swapped that `5` for `"foo"`), but I tried a few things and to be
honest the complexity of the types wasn't worth it, I don't think.
I'm very open to tweaking these but I'd rather ship this and tweak going
forwards rather than spend hours now tweaking. Once we ship these
typedefs and get feedback from the community I'm sure we can improve
them.
2020-04-22 09:33:44 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
return returnByValue
|
2022-06-14 11:16:21 +00:00
|
|
|
? valueFromRemoteObject(remoteObject)
|
2022-06-27 07:24:23 +00:00
|
|
|
: createJSHandle(this, remoteObject);
|
2017-10-06 22:35:02 +00:00
|
|
|
}
|
|
|
|
|
2018-11-01 23:43:21 +00:00
|
|
|
let functionText = pageFunction.toString();
|
|
|
|
try {
|
|
|
|
new Function('(' + functionText + ')');
|
2020-04-28 13:16:28 +00:00
|
|
|
} catch (error) {
|
2018-11-01 23:43:21 +00:00
|
|
|
// This means we might have a function shorthand. Try another
|
|
|
|
// time prefixing 'function '.
|
2022-06-14 11:55:35 +00:00
|
|
|
if (functionText.startsWith('async ')) {
|
2020-05-07 10:54:55 +00:00
|
|
|
functionText =
|
|
|
|
'async function ' + functionText.substring('async '.length);
|
2022-06-14 11:55:35 +00:00
|
|
|
} else {
|
|
|
|
functionText = 'function ' + functionText;
|
|
|
|
}
|
2018-11-01 23:43:21 +00:00
|
|
|
try {
|
2020-05-07 10:54:55 +00:00
|
|
|
new Function('(' + functionText + ')');
|
2020-04-28 13:16:28 +00:00
|
|
|
} catch (error) {
|
2018-11-01 23:43:21 +00:00
|
|
|
// We tried hard to serialize, but there's a weird beast here.
|
|
|
|
throw new Error('Passed function is not well-serializable!');
|
|
|
|
}
|
|
|
|
}
|
2019-01-14 22:30:50 +00:00
|
|
|
let callFunctionOnPromise;
|
|
|
|
try {
|
|
|
|
callFunctionOnPromise = this._client.send('Runtime.callFunctionOn', {
|
|
|
|
functionDeclaration: functionText + '\n' + suffix + '\n',
|
|
|
|
executionContextId: this._contextId,
|
|
|
|
arguments: args.map(convertArgument.bind(this)),
|
2019-06-07 20:46:43 +00:00
|
|
|
returnByValue,
|
2019-01-14 22:30:50 +00:00
|
|
|
awaitPromise: true,
|
2020-05-07 10:54:55 +00:00
|
|
|
userGesture: true,
|
2019-01-14 22:30:50 +00:00
|
|
|
});
|
2020-04-28 13:16:28 +00:00
|
|
|
} catch (error) {
|
2020-05-07 10:54:55 +00:00
|
|
|
if (
|
|
|
|
error instanceof TypeError &&
|
|
|
|
error.message.startsWith('Converting circular structure to JSON')
|
2022-06-13 09:16:25 +00:00
|
|
|
) {
|
|
|
|
error.message += ' Recursive objects are not allowed.';
|
|
|
|
}
|
2020-04-28 13:16:28 +00:00
|
|
|
throw error;
|
2019-01-14 22:30:50 +00:00
|
|
|
}
|
2022-06-22 13:25:44 +00:00
|
|
|
const {exceptionDetails, result: remoteObject} =
|
2021-05-12 14:48:30 +00:00
|
|
|
await callFunctionOnPromise.catch(rewriteError);
|
2022-06-14 11:55:35 +00:00
|
|
|
if (exceptionDetails) {
|
2020-05-07 10:54:55 +00:00
|
|
|
throw new Error(
|
2022-06-14 11:16:21 +00:00
|
|
|
'Evaluation failed: ' + getExceptionMessage(exceptionDetails)
|
2020-05-07 10:54:55 +00:00
|
|
|
);
|
2022-06-14 11:55:35 +00:00
|
|
|
}
|
2020-05-07 10:54:55 +00:00
|
|
|
return returnByValue
|
2022-06-14 11:16:21 +00:00
|
|
|
? valueFromRemoteObject(remoteObject)
|
2022-06-27 07:24:23 +00:00
|
|
|
: createJSHandle(this, remoteObject);
|
2017-10-06 22:35:02 +00:00
|
|
|
|
2022-05-31 14:34:16 +00:00
|
|
|
function convertArgument(
|
|
|
|
this: ExecutionContext,
|
|
|
|
arg: unknown
|
|
|
|
): Protocol.Runtime.CallArgument {
|
2022-06-14 11:55:35 +00:00
|
|
|
if (typeof arg === 'bigint') {
|
2020-05-07 10:54:55 +00:00
|
|
|
// eslint-disable-line valid-typeof
|
2022-06-22 13:25:44 +00:00
|
|
|
return {unserializableValue: `${arg.toString()}n`};
|
2022-06-14 11:55:35 +00:00
|
|
|
}
|
|
|
|
if (Object.is(arg, -0)) {
|
2022-06-22 13:25:44 +00:00
|
|
|
return {unserializableValue: '-0'};
|
2022-06-14 11:55:35 +00:00
|
|
|
}
|
|
|
|
if (Object.is(arg, Infinity)) {
|
2022-06-22 13:25:44 +00:00
|
|
|
return {unserializableValue: 'Infinity'};
|
2022-06-14 11:55:35 +00:00
|
|
|
}
|
|
|
|
if (Object.is(arg, -Infinity)) {
|
2022-06-22 13:25:44 +00:00
|
|
|
return {unserializableValue: '-Infinity'};
|
2022-06-14 11:55:35 +00:00
|
|
|
}
|
|
|
|
if (Object.is(arg, NaN)) {
|
2022-06-22 13:25:44 +00:00
|
|
|
return {unserializableValue: 'NaN'};
|
2022-06-14 11:55:35 +00:00
|
|
|
}
|
2020-05-07 10:54:55 +00:00
|
|
|
const objectHandle = arg && arg instanceof JSHandle ? arg : null;
|
2017-10-06 22:35:02 +00:00
|
|
|
if (objectHandle) {
|
2022-08-11 09:45:35 +00:00
|
|
|
if (objectHandle.executionContext() !== this) {
|
2020-05-07 10:54:55 +00:00
|
|
|
throw new Error(
|
|
|
|
'JSHandles can be evaluated only in the context they were created!'
|
|
|
|
);
|
2022-06-14 11:55:35 +00:00
|
|
|
}
|
2022-08-11 09:45:35 +00:00
|
|
|
if (objectHandle.disposed) {
|
2022-06-14 11:55:35 +00:00
|
|
|
throw new Error('JSHandle is disposed!');
|
|
|
|
}
|
2022-08-11 09:45:35 +00:00
|
|
|
if (objectHandle.remoteObject().unserializableValue) {
|
2020-05-07 10:54:55 +00:00
|
|
|
return {
|
2022-08-11 09:45:35 +00:00
|
|
|
unserializableValue:
|
|
|
|
objectHandle.remoteObject().unserializableValue,
|
2020-05-07 10:54:55 +00:00
|
|
|
};
|
2022-06-14 11:55:35 +00:00
|
|
|
}
|
2022-08-11 09:45:35 +00:00
|
|
|
if (!objectHandle.remoteObject().objectId) {
|
|
|
|
return {value: objectHandle.remoteObject().value};
|
2022-06-14 11:55:35 +00:00
|
|
|
}
|
2022-08-11 09:45:35 +00:00
|
|
|
return {objectId: objectHandle.remoteObject().objectId};
|
2017-10-06 22:35:02 +00:00
|
|
|
}
|
2022-06-22 13:25:44 +00:00
|
|
|
return {value: arg};
|
2017-10-06 22:35:02 +00:00
|
|
|
}
|
2018-06-14 22:27:59 +00:00
|
|
|
|
2020-07-10 10:51:52 +00:00
|
|
|
function rewriteError(error: Error): Protocol.Runtime.EvaluateResponse {
|
2022-06-14 11:55:35 +00:00
|
|
|
if (error.message.includes('Object reference chain is too long')) {
|
2022-06-22 13:25:44 +00:00
|
|
|
return {result: {type: 'undefined'}};
|
2022-06-14 11:55:35 +00:00
|
|
|
}
|
|
|
|
if (error.message.includes("Object couldn't be returned by value")) {
|
2022-06-22 13:25:44 +00:00
|
|
|
return {result: {type: 'undefined'}};
|
2022-06-14 11:55:35 +00:00
|
|
|
}
|
2020-05-07 10:54:55 +00:00
|
|
|
|
|
|
|
if (
|
|
|
|
error.message.endsWith('Cannot find context with specified id') ||
|
|
|
|
error.message.endsWith('Inspected target navigated or closed')
|
2022-06-14 11:55:35 +00:00
|
|
|
) {
|
2020-05-07 10:54:55 +00:00
|
|
|
throw new Error(
|
|
|
|
'Execution context was destroyed, most likely because of a navigation.'
|
|
|
|
);
|
2022-06-14 11:55:35 +00:00
|
|
|
}
|
2018-06-14 22:27:59 +00:00
|
|
|
throw error;
|
|
|
|
}
|
2017-10-06 22:35:02 +00:00
|
|
|
}
|
2017-10-11 21:41:20 +00:00
|
|
|
|
2020-06-24 15:11:10 +00:00
|
|
|
/**
|
2022-08-11 09:45:35 +00:00
|
|
|
* Iterates through the JavaScript heap and finds all the objects with the
|
2020-06-24 15:11:10 +00:00
|
|
|
* given prototype.
|
2022-08-11 09:45:35 +00:00
|
|
|
*
|
2020-06-24 15:11:10 +00:00
|
|
|
* @example
|
2022-08-12 12:15:26 +00:00
|
|
|
*
|
2022-07-01 11:52:39 +00:00
|
|
|
* ```ts
|
2020-06-24 15:11:10 +00:00
|
|
|
* // Create a Map object
|
2022-08-12 12:15:26 +00:00
|
|
|
* await page.evaluate(() => (window.map = new Map()));
|
2020-06-24 15:11:10 +00:00
|
|
|
* // Get a handle to the Map object prototype
|
|
|
|
* const mapPrototype = await page.evaluateHandle(() => Map.prototype);
|
|
|
|
* // Query all map instances into an array
|
|
|
|
* const mapInstances = await page.queryObjects(mapPrototype);
|
|
|
|
* // Count amount of map objects in heap
|
|
|
|
* const count = await page.evaluate(maps => maps.length, mapInstances);
|
|
|
|
* await mapInstances.dispose();
|
|
|
|
* await mapPrototype.dispose();
|
|
|
|
* ```
|
|
|
|
*
|
2021-04-06 08:58:01 +00:00
|
|
|
* @param prototypeHandle - a handle to the object prototype
|
2020-06-24 15:11:10 +00:00
|
|
|
* @returns A handle to an array of objects with the given prototype.
|
|
|
|
*/
|
feat!: type inference for evaluation types (#8547)
This PR greatly improves the types within Puppeteer:
- **Almost everything** is auto-deduced.
- Parameters don't need to be specified in the function. They are deduced from the spread.
- Return types don't need to be specified. They are deduced from the function. (More on this below)
- Selections based on tag names correctly deduce element type, similar to TypeScript's mechanism for `getElementByTagName`.
- [**BREAKING CHANGE**] We've removed the ability to declare return types in type arguments for the following reasons:
1. Setting them will indubitably break auto-deduction.
2. You can just use `as ...` in TypeScript to coerce the correct type (given it makes sense).
- [**BREAKING CHANGE**] `waitFor` is officially gone.
To migrate to these changes, there are only four things you may need to change:
- If you set a return type using the `ReturnType` type parameter, remove it and use `as ...` and `HandleFor` (if necessary).
⛔ `evaluate<ReturnType>(a: number, b: number) => {...}, a, b)`
✅ `(await evaluate(a, b) => {...}, a, b)) as ReturnType`
⛔ `evaluateHandle<ReturnType>(a: number, b: number) => {...}, a, b)`
✅ `(await evaluateHandle(a, b) => {...}, a, b)) as HandleFor<ReturnType>`
- If you set any type parameters in the *parameters* of an evaluation function, remove them.
⛔ `evaluate(a: number, b: number) => {...}, a, b)`
✅ `evaluate(a, b) => {...}, a, b)`
- If you set any type parameters in the method's declaration, remove them.
⛔ `evaluate<(a: number, b: number) => void>((a, b) => {...}, a, b)`
✅ `evaluate(a, b) => {...}, a, b)`
2022-06-23 09:29:46 +00:00
|
|
|
async queryObjects<Prototype>(
|
|
|
|
prototypeHandle: JSHandle<Prototype>
|
|
|
|
): Promise<HandleFor<Prototype[]>> {
|
2022-08-11 09:45:35 +00:00
|
|
|
assert(!prototypeHandle.disposed, 'Prototype JSHandle is disposed!');
|
|
|
|
const remoteObject = prototypeHandle.remoteObject();
|
2020-05-07 10:54:55 +00:00
|
|
|
assert(
|
2022-08-11 09:45:35 +00:00
|
|
|
remoteObject.objectId,
|
2020-05-07 10:54:55 +00:00
|
|
|
'Prototype JSHandle must not be referencing primitive value'
|
|
|
|
);
|
2017-10-11 21:41:20 +00:00
|
|
|
const response = await this._client.send('Runtime.queryObjects', {
|
2022-08-11 09:45:35 +00:00
|
|
|
prototypeObjectId: remoteObject.objectId,
|
2017-10-11 21:41:20 +00:00
|
|
|
});
|
2022-06-27 07:24:23 +00:00
|
|
|
return createJSHandle(this, response.objects) as HandleFor<Prototype[]>;
|
2017-10-11 21:41:20 +00:00
|
|
|
}
|
2017-10-06 22:35:02 +00:00
|
|
|
}
|