chore: reuse SetContent across implementations (#9894)

This commit is contained in:
Nikolay Vitkov 2023-03-22 10:49:39 +01:00 committed by GitHub
parent 8bd73175e0
commit c6a9404bb4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 16 deletions

View File

@ -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,

View File

@ -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<void>(resolve => {
this.once(waitUntilCommand, () => {

View File

@ -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<Page, 'evaluate'>,
content: string
): Promise<void> {
// 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);
}