From c6a9404bb4a6fdf78a47d1555026d80236d145b7 Mon Sep 17 00:00:00 2001 From: Nikolay Vitkov <34244704+Lightning00Blade@users.noreply.github.com> Date: Wed, 22 Mar 2023 10:49:39 +0100 Subject: [PATCH] chore: reuse SetContent across implementations (#9894) --- .../puppeteer-core/src/common/IsolatedWorld.ts | 17 +++++++++-------- .../puppeteer-core/src/common/bidi/Context.ts | 10 ++-------- packages/puppeteer-core/src/common/util.ts | 17 +++++++++++++++++ 3 files changed, 28 insertions(+), 16 deletions(-) diff --git a/packages/puppeteer-core/src/common/IsolatedWorld.ts b/packages/puppeteer-core/src/common/IsolatedWorld.ts index 6fab87fe270..a74f5fd3ee1 100644 --- a/packages/puppeteer-core/src/common/IsolatedWorld.ts +++ b/packages/puppeteer-core/src/common/IsolatedWorld.ts @@ -38,7 +38,12 @@ import { InnerLazyParams, NodeFor, } from './types.js'; -import {addPageBinding, createJSHandle, debugError} from './util.js'; +import { + addPageBinding, + createJSHandle, + debugError, + setPageContent, +} from './util.js'; import {TaskManager, WaitTask} from './WaitTask.js'; /** @@ -276,13 +281,9 @@ export class IsolatedWorld { waitUntil = ['load'], timeout = this.#timeoutSettings.navigationTimeout(), } = options; - // We rely upon the fact that document.open() will reset frame lifecycle with "init" - // lifecycle event. @see https://crrev.com/608658 - await this.evaluate(html => { - document.open(); - document.write(html); - document.close(); - }, html); + + await setPageContent(this, html); + const watcher = new LifecycleWatcher( this.#frameManager, this.#frame, diff --git a/packages/puppeteer-core/src/common/bidi/Context.ts b/packages/puppeteer-core/src/common/bidi/Context.ts index 5620db2ee9e..82dc23cc2c5 100644 --- a/packages/puppeteer-core/src/common/bidi/Context.ts +++ b/packages/puppeteer-core/src/common/bidi/Context.ts @@ -25,7 +25,7 @@ import {EventEmitter} from '../EventEmitter.js'; import {PuppeteerLifeCycleEvent} from '../LifecycleWatcher.js'; import {TimeoutSettings} from '../TimeoutSettings.js'; import {EvaluateFunc, HandleFor} from '../types.js'; -import {isString, waitWithTimeout} from '../util.js'; +import {isString, setPageContent, waitWithTimeout} from '../util.js'; import {Connection} from './Connection.js'; import {ElementHandle} from './ElementHandle.js'; @@ -210,13 +210,7 @@ export class Context extends EventEmitter { ) as string; await Promise.all([ - // We rely upon the fact that document.open() will reset frame lifecycle with "init" - // lifecycle event. @see https://crrev.com/608658 - this.evaluate(html => { - document.open(); - document.write(html); - document.close(); - }, html), + setPageContent(this, html), waitWithTimeout( new Promise(resolve => { this.once(waitUntilCommand, () => { diff --git a/packages/puppeteer-core/src/common/util.ts b/packages/puppeteer-core/src/common/util.ts index a13c803f742..77f05497590 100644 --- a/packages/puppeteer-core/src/common/util.ts +++ b/packages/puppeteer-core/src/common/util.ts @@ -20,6 +20,7 @@ import type {Protocol} from 'devtools-protocol'; import type {ElementHandle} from '../api/ElementHandle.js'; import type {JSHandle} from '../api/JSHandle.js'; +import {Page} from '../api/Page.js'; import {isNode} from '../environment.js'; import {assert} from '../util/assert.js'; import {isErrorLike} from '../util/ErrorLike.js'; @@ -441,3 +442,19 @@ export async function getReadableFromProtocolStream( }, }); } + +/** + * @internal + */ +export async function setPageContent( + page: Pick, + content: string +): Promise { + // We rely upon the fact that document.open() will reset frame lifecycle with "init" + // lifecycle event. @see https://crrev.com/608658 + return page.evaluate(html => { + document.open(); + document.write(html); + document.close(); + }, content); +}