diff --git a/packages/puppeteer-core/src/api/Frame.ts b/packages/puppeteer-core/src/api/Frame.ts index 394d1915d00..fd8afce2325 100644 --- a/packages/puppeteer-core/src/api/Frame.ts +++ b/packages/puppeteer-core/src/api/Frame.ts @@ -804,6 +804,17 @@ export abstract class Frame extends EventEmitter { } ): Promise; + /** + * @internal + */ + async setFrameContent(content: string): Promise { + return await this.evaluate(html => { + document.open(); + document.write(html); + document.close(); + }, content); + } + /** * The frame's `name` attribute as specified in the tag. * diff --git a/packages/puppeteer-core/src/bidi/Frame.ts b/packages/puppeteer-core/src/bidi/Frame.ts index 9e50931ff2c..36e39bb1c56 100644 --- a/packages/puppeteer-core/src/bidi/Frame.ts +++ b/packages/puppeteer-core/src/bidi/Frame.ts @@ -39,7 +39,7 @@ import type {WaitForSelectorOptions} from '../api/Page.js'; import {UnsupportedOperation} from '../common/Errors.js'; import type {TimeoutSettings} from '../common/TimeoutSettings.js'; import type {Awaitable, NodeFor} from '../common/types.js'; -import {UTILITY_WORLD_NAME, setPageContent, timeout} from '../common/util.js'; +import {UTILITY_WORLD_NAME, timeout} from '../common/util.js'; import {Deferred} from '../util/Deferred.js'; import {disposeSymbol} from '../util/disposable.js'; @@ -174,7 +174,7 @@ export class BidiFrame extends Frame { ._waitWithNetworkIdle( forkJoin([ fromEvent(this.#context, waitEvent).pipe(first()), - from(setPageContent(this, html)), + from(this.setFrameContent(html)), ]).pipe( map(() => { return null; diff --git a/packages/puppeteer-core/src/cdp/Frame.ts b/packages/puppeteer-core/src/cdp/Frame.ts index 5d995c4ca5a..3596766cb29 100644 --- a/packages/puppeteer-core/src/cdp/Frame.ts +++ b/packages/puppeteer-core/src/cdp/Frame.ts @@ -21,7 +21,6 @@ import {Frame, FrameEvent, throwIfDetached} from '../api/Frame.js'; import type {HTTPResponse} from '../api/HTTPResponse.js'; import type {WaitTimeoutOptions} from '../api/Page.js'; import {UnsupportedOperation} from '../common/Errors.js'; -import {setPageContent} from '../common/util.js'; import {Deferred} from '../util/Deferred.js'; import {disposeSymbol} from '../util/disposable.js'; import {isErrorLike} from '../util/ErrorLike.js'; @@ -262,7 +261,9 @@ export class CdpFrame extends Frame { timeout = this._frameManager.timeoutSettings.navigationTimeout(), } = options; - await setPageContent(this.isolatedRealm(), html); + // We rely upon the fact that document.open() will reset frame lifecycle with "init" + // lifecycle event. @see https://crrev.com/608658 + await this.setFrameContent(html); const watcher = new LifecycleWatcher( this._frameManager.networkManager, diff --git a/packages/puppeteer-core/src/common/util.ts b/packages/puppeteer-core/src/common/util.ts index f6cca6059f0..475d83aac88 100644 --- a/packages/puppeteer-core/src/common/util.ts +++ b/packages/puppeteer-core/src/common/util.ts @@ -31,7 +31,6 @@ import { raceWith, } from '../../third_party/rxjs/rxjs.js'; import type {CDPSession} from '../api/CDPSession.js'; -import type {Page} from '../api/Page.js'; import {isNode} from '../environment.js'; import {assert} from '../util/assert.js'; import type {Deferred} from '../util/Deferred.js'; @@ -504,22 +503,6 @@ 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 await page.evaluate(html => { - document.open(); - document.write(html); - document.close(); - }, content); -} - /** * @internal */