mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
fix: mimic rejection for PuppeteerUtil on early call (#9589)
This commit is contained in:
parent
6edd996768
commit
1980de91a1
@ -25,7 +25,6 @@ import {Frame} from './Frame.js';
|
|||||||
import {FrameManager} from './FrameManager.js';
|
import {FrameManager} from './FrameManager.js';
|
||||||
import {MouseButton} from './Input.js';
|
import {MouseButton} from './Input.js';
|
||||||
import {JSHandle} from './JSHandle.js';
|
import {JSHandle} from './JSHandle.js';
|
||||||
import {LazyArg} from './LazyArg.js';
|
|
||||||
import {LifecycleWatcher, PuppeteerLifeCycleEvent} from './LifecycleWatcher.js';
|
import {LifecycleWatcher, PuppeteerLifeCycleEvent} from './LifecycleWatcher.js';
|
||||||
import {TimeoutSettings} from './TimeoutSettings.js';
|
import {TimeoutSettings} from './TimeoutSettings.js';
|
||||||
import {EvaluateFunc, HandleFor, InnerLazyParams, NodeFor} from './types.js';
|
import {EvaluateFunc, HandleFor, InnerLazyParams, NodeFor} from './types.js';
|
||||||
@ -35,6 +34,7 @@ import {MAIN_WORLD, PUPPETEER_WORLD} from './IsolatedWorlds.js';
|
|||||||
|
|
||||||
import type PuppeteerUtil from '../injected/injected.js';
|
import type PuppeteerUtil from '../injected/injected.js';
|
||||||
import type {ElementHandle} from './ElementHandle.js';
|
import type {ElementHandle} from './ElementHandle.js';
|
||||||
|
import {LazyArg} from './LazyArg.js';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @public
|
* @public
|
||||||
@ -96,10 +96,20 @@ export class IsolatedWorld {
|
|||||||
// Contains mapping from functions that should be bound to Puppeteer functions.
|
// Contains mapping from functions that should be bound to Puppeteer functions.
|
||||||
#boundFunctions = new Map<string, Function>();
|
#boundFunctions = new Map<string, Function>();
|
||||||
#taskManager = new TaskManager();
|
#taskManager = new TaskManager();
|
||||||
#puppeteerUtil = createDeferredPromise<JSHandle<PuppeteerUtil>>();
|
#puppeteerUtil?: Promise<JSHandle<PuppeteerUtil> | undefined>;
|
||||||
|
|
||||||
get puppeteerUtil(): Promise<JSHandle<PuppeteerUtil>> {
|
get puppeteerUtil(): Promise<JSHandle<PuppeteerUtil>> {
|
||||||
return this.#puppeteerUtil;
|
/**
|
||||||
|
* This is supposed to mimic what happens when evaluating Puppeteer utilities
|
||||||
|
* break due to navigation.
|
||||||
|
*/
|
||||||
|
return (async () => {
|
||||||
|
const util = await this.#puppeteerUtil;
|
||||||
|
if (util) {
|
||||||
|
return util;
|
||||||
|
}
|
||||||
|
throw new Error('Execution context was destroyed!');
|
||||||
|
})();
|
||||||
}
|
}
|
||||||
|
|
||||||
get taskManager(): TaskManager {
|
get taskManager(): TaskManager {
|
||||||
@ -151,15 +161,19 @@ export class IsolatedWorld {
|
|||||||
|
|
||||||
async #injectPuppeteerUtil(context: ExecutionContext): Promise<void> {
|
async #injectPuppeteerUtil(context: ExecutionContext): Promise<void> {
|
||||||
try {
|
try {
|
||||||
this.#puppeteerUtil.resolve(
|
this.#puppeteerUtil = (async () => {
|
||||||
(await context.evaluateHandle(
|
try {
|
||||||
`(() => {
|
return (await context.evaluateHandle(
|
||||||
const module = {};
|
`(() => {
|
||||||
${injectedSource}
|
const module = {};
|
||||||
return module.exports.default;
|
${injectedSource}
|
||||||
})()`
|
return module.exports.default;
|
||||||
)) as JSHandle<PuppeteerUtil>
|
})()`
|
||||||
);
|
)) as JSHandle<PuppeteerUtil>;
|
||||||
|
} catch {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
})();
|
||||||
this.#taskManager.rerunAll();
|
this.#taskManager.rerunAll();
|
||||||
} catch (error: unknown) {
|
} catch (error: unknown) {
|
||||||
debugError(error);
|
debugError(error);
|
||||||
|
@ -15,6 +15,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import PuppeteerUtil from '../injected/injected.js';
|
import PuppeteerUtil from '../injected/injected.js';
|
||||||
|
import {assert} from '../util/assert.js';
|
||||||
import {ariaHandler} from './AriaQueryHandler.js';
|
import {ariaHandler} from './AriaQueryHandler.js';
|
||||||
import {ElementHandle} from './ElementHandle.js';
|
import {ElementHandle} from './ElementHandle.js';
|
||||||
import {Frame} from './Frame.js';
|
import {Frame} from './Frame.js';
|
||||||
@ -99,10 +100,12 @@ function createPuppeteerQueryHandler(
|
|||||||
if (handler.queryOne) {
|
if (handler.queryOne) {
|
||||||
const queryOne = handler.queryOne;
|
const queryOne = handler.queryOne;
|
||||||
internalHandler.queryOne = async (element, selector) => {
|
internalHandler.queryOne = async (element, selector) => {
|
||||||
|
const world = element.executionContext()._world;
|
||||||
|
assert(world);
|
||||||
const jsHandle = await element.evaluateHandle(
|
const jsHandle = await element.evaluateHandle(
|
||||||
queryOne,
|
queryOne,
|
||||||
selector,
|
selector,
|
||||||
await element.executionContext()._world!.puppeteerUtil
|
await world.puppeteerUtil
|
||||||
);
|
);
|
||||||
const elementHandle = jsHandle.asElement();
|
const elementHandle = jsHandle.asElement();
|
||||||
if (elementHandle) {
|
if (elementHandle) {
|
||||||
@ -145,6 +148,8 @@ function createPuppeteerQueryHandler(
|
|||||||
if (handler.queryAll) {
|
if (handler.queryAll) {
|
||||||
const queryAll = handler.queryAll;
|
const queryAll = handler.queryAll;
|
||||||
internalHandler.queryAll = async (element, selector) => {
|
internalHandler.queryAll = async (element, selector) => {
|
||||||
|
const world = element.executionContext()._world;
|
||||||
|
assert(world);
|
||||||
const jsHandle = await element.evaluateHandle(
|
const jsHandle = await element.evaluateHandle(
|
||||||
queryAll,
|
queryAll,
|
||||||
selector,
|
selector,
|
||||||
|
@ -170,11 +170,10 @@ export class WaitTask<T = unknown> {
|
|||||||
async terminate(error?: unknown): Promise<void> {
|
async terminate(error?: unknown): Promise<void> {
|
||||||
this.#world.taskManager.delete(this);
|
this.#world.taskManager.delete(this);
|
||||||
|
|
||||||
if (this.#timeout) {
|
|
||||||
clearTimeout(this.#timeout);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (error && !this.#result.finished()) {
|
if (error && !this.#result.finished()) {
|
||||||
|
if (this.#timeout) {
|
||||||
|
clearTimeout(this.#timeout);
|
||||||
|
}
|
||||||
this.#result.reject(error);
|
this.#result.reject(error);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user