Skip to content

🎥 Make videos programmatically with React

execution context

Jonny Burger2f55b6c6547c
diff --git a/packages/renderer/src/browser/ExecutionContext.ts b/packages/renderer/src/browser/ExecutionContext.ts
index 5a440be3d6..b618674cd6 100644
--- a/packages/renderer/src/browser/ExecutionContext.ts
+++ b/packages/renderer/src/browser/ExecutionContext.ts
@@ -1,3 +1,5 @@
+/* eslint-disable no-new-func */
+/* eslint-disable no-new */
/**
* Copyright 2017 Google Inc. All rights reserved.
*
@@ -29,48 +31,16 @@ import {getExceptionMessage, isString, valueFromRemoteObject} from './util';
export const EVALUATION_SCRIPT_URL = 'pptr://__puppeteer_evaluation_script__';
const SOURCE_URL_REGEX = /^[\040\t]*\/\/[@#] sourceURL=\s*(\S*?)\s*$/m;
-/**
- * This class represents a context for JavaScript execution. A [Page] might have
- * many execution contexts:
- * - each
- * {@link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/iframe |
- * frame } has "default" execution context that is always created after frame is
- * attached to DOM. This context is returned by the
- * {@link Frame.executionContext} method.
- * - {@link https://developer.chrome.com/extensions | Extension}'s content scripts
- * create additional execution contexts.
- *
- * Besides pages, execution contexts can be found in
- * {@link https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API |
- * workers }.
- *
- * @public
- */
export class ExecutionContext {
- /**
- * @internal
- */
_client: CDPSession;
- /**
- * @internal
- */
- _world?: DOMWorld;
- /**
- * @internal
- */
+ _world: DOMWorld;
_contextId: number;
- /**
- * @internal
- */
_contextName: string;
- /**
- * @internal
- */
constructor(
client: CDPSession,
contextPayload: Protocol.Runtime.ExecutionContextDescription,
- world?: DOMWorld
+ world: DOMWorld
) {
this._client = client;
this._world = world;
@@ -78,111 +48,17 @@ export class ExecutionContext {
this._contextName = contextPayload.name;
}
- /**
- * @remarks
- *
- * Not every execution context is associated with a frame. For
- * example, workers and extensions have execution contexts that are not
- * associated with frames.
- *
- * @returns The frame associated with this execution context.
- */
frame(): Frame | null {
return this._world ? this._world.frame() : null;
}
- /**
- * @remarks
- * If the function passed to the `executionContext.evaluate` returns a
- * Promise, then `executionContext.evaluate` would wait for the promise to
- * resolve and return its value. If the function passed to the
- * `executionContext.evaluate` returns a non-serializable value, then
- * `executionContext.evaluate` resolves to `undefined`. DevTools Protocol also
- * supports transferring some additional values that are not serializable by
- * `JSON`: `-0`, `NaN`, `Infinity`, `-Infinity`, and bigint literals.
- *
- *
- * @example
- * ```js
- * const executionContext = await page.mainFrame().executionContext();
- * const result = await executionContext.evaluate(() => Promise.resolve(8 * 7))* ;
- * console.log(result); // prints "56"
- * ```
- *
- * @example
- * A string can also be passed in instead of a function.
- *
- * ```js
- * console.log(await executionContext.evaluate('1 + 2')); // prints "3"
- * ```
- *
- * @example
- * {@link JSHandle} instances can be passed as arguments to the
- * `executionContext.* evaluate`:
- * ```js
- * const oneHandle = await executionContext.evaluateHandle(() => 1);
- * const twoHandle = await executionContext.evaluateHandle(() => 2);
- * const result = await executionContext.evaluate(
- * (a, b) => a + b, oneHandle, * twoHandle
- * );
- * await oneHandle.dispose();
- * await twoHandle.dispose();
- * console.log(result); // prints '3'.
- * ```
- * @param pageFunction - a function to be evaluated in the `executionContext`
- * @param args - argument to pass to the page function
- *
- * @returns A promise that resolves to the return value of the given function.
- */
async evaluate<ReturnType>(
pageFunction: Function | string,
...args: unknown[]
): Promise<ReturnType> {
- return await this.#evaluate<ReturnType>(true, pageFunction, ...args);
+ return this.#evaluate<ReturnType>(true, pageFunction, ...args);
}
- /**
- * @remarks
- * The only difference between `executionContext.evaluate` and
- * `executionContext.evaluateHandle` is that `executionContext.evaluateHandle`
- * returns an in-page object (a {@link JSHandle}).
- * If the function passed to the `executionContext.evaluateHandle` returns a
- * Promise, then `executionContext.evaluateHandle` would wait for the
- * promise to resolve and return its value.
- *
- * @example
- * ```js
- * const context = await page.mainFrame().executionContext();
- * const aHandle = await context.evaluateHandle(() => Promise.resolve(self));
- * aHandle; // Handle for the global object.
- * ```
- *
- * @example
- * A string can also be passed in instead of a function.
- *
- * ```js
- * // Handle for the '3' * object.
- * const aHandle = await context.evaluateHandle('1 + 2');
- * ```
- *
- * @example
- * JSHandle instances can be passed as arguments
- * to the `executionContext.* evaluateHandle`:
- *
- * ```js
- * const aHandle = await context.evaluateHandle(() => document.body);
- * const resultHandle = await context.evaluateHandle(body => body.innerHTML, * aHandle);
- * console.log(await resultHandle.jsonValue()); // prints body's innerHTML
- * await aHandle.dispose();
- * await resultHandle.dispose();
- * ```
- *
- * @param pageFunction - a function to be evaluated in the `executionContext`
- * @param args - argument to pass to the page function
- *
- * @returns A promise that resolves to the return value of the given function
- * as an in-page object (a {@link JSHandle}).
- */
async evaluateHandle<HandleType extends JSHandle | ElementHandle = JSHandle>(
pageFunction: EvaluateHandleFn,
...args: SerializableOrJSHandle[]
@@ -204,25 +80,24 @@ export class ExecutionContext {
? expression
: expression + '\n' + suffix;
- const {exceptionDetails, result: remoteObject} = await this._client
- .send('Runtime.evaluate', {
- expression: expressionWithSourceUrl,
- contextId,
- returnByValue,
- awaitPromise: true,
- userGesture: true,
- })
- .catch(rewriteError);
-
- if (exceptionDetails) {
- throw new Error(
- 'Evaluation failed: ' + getExceptionMessage(exceptionDetails)
- );
+ const {exceptionDetails: _details, result: _remoteObject} =
+ await this._client
+ .send('Runtime.evaluate', {
+ expression: expressionWithSourceUrl,
+ contextId,
+ returnByValue,
+ awaitPromise: true,
+ userGesture: true,
+ })
+ .catch(rewriteError);
+
+ if (_details) {
+ throw new Error('Evaluation failed: ' + getExceptionMessage(_details));
}
return returnByValue
- ? valueFromRemoteObject(remoteObject)
- : _createJSHandle(this, remoteObject);
+ ? valueFromRemoteObject(_remoteObject)
+ : _createJSHandle(this, _remoteObject);
}
if (typeof pageFunction !== 'function') {
@@ -246,7 +121,7 @@ export class ExecutionContext {
try {
new Function('(' + functionText + ')');
- } catch (error) {
+ } catch (_error) {
// We tried hard to serialize, but there's a weird beast here.
throw new Error('Passed function is not well-serializable!');
}
diff --git a/packages/renderer/src/browser/FrameManager.ts b/packages/renderer/src/browser/FrameManager.ts
index 7aa66306fd..268afaba31 100644
--- a/packages/renderer/src/browser/FrameManager.ts
+++ b/packages/renderer/src/browser/FrameManager.ts
@@ -497,7 +497,7 @@ export class FrameManager extends EventEmitter {
const context = new ExecutionContext(
frame?._client() || this.#client,
contextPayload,
- world
+ world as DOMWorld
);
if (world) {
world._setContext(context);