chore: addInitScript and auth (unsuccessful) in bidi (#32500)

This commit is contained in:
Yury Semikhatsky 2024-09-09 10:13:26 -07:00 committed by GitHub
parent 728083b435
commit d030965688
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 26 additions and 9 deletions

View File

@ -195,8 +195,12 @@ export class BidiBrowserContext extends BrowserContext {
this._authenticateProxyViaHeader(); this._authenticateProxyViaHeader();
} }
private _bidiPages() {
return [...this._browser._bidiPages.values()].filter(bidiPage => bidiPage._browserContext === this);
}
pages(): Page[] { pages(): Page[] {
return []; return this._bidiPages().map(bidiPage => bidiPage._initializedPage).filter(Boolean) as Page[];
} }
async newPageDelegate(): Promise<PageDelegate> { async newPageDelegate(): Promise<PageDelegate> {
@ -269,11 +273,13 @@ export class BidiBrowserContext extends BrowserContext {
} }
async doSetHTTPCredentials(httpCredentials?: types.Credentials): Promise<void> { async doSetHTTPCredentials(httpCredentials?: types.Credentials): Promise<void> {
this._options.httpCredentials = httpCredentials;
for (const page of this.pages())
await (page._delegate as BidiPage).updateHttpCredentials();
} }
async doAddInitScript(initScript: InitScript) { async doAddInitScript(initScript: InitScript) {
// for (const page of this.pages()) await Promise.all(this.pages().map(page => (page._delegate as BidiPage).addInitScript(initScript)));
// await (page._delegate as WKPage)._updateBootstrapScript();
} }
async doRemoveNonInternalInitScripts() { async doRemoveNonInternalInitScripts() {

View File

@ -47,6 +47,7 @@ export class BidiPage implements PageDelegate {
readonly _browserContext: BidiBrowserContext; readonly _browserContext: BidiBrowserContext;
readonly _networkManager: BidiNetworkManager; readonly _networkManager: BidiNetworkManager;
_initializedPage: Page | null = null; _initializedPage: Page | null = null;
private _initScriptIds: string[] = [];
constructor(browserContext: BidiBrowserContext, bidiSession: BidiSession, opener: BidiPage | null) { constructor(browserContext: BidiBrowserContext, bidiSession: BidiSession, opener: BidiPage | null) {
this._session = bidiSession; this._session = bidiSession;
@ -92,9 +93,14 @@ export class BidiPage implements PageDelegate {
this.updateHttpCredentials(), this.updateHttpCredentials(),
this.updateRequestInterception(), this.updateRequestInterception(),
this._updateViewport(), this._updateViewport(),
this._addAllInitScripts(),
]); ]);
} }
private async _addAllInitScripts() {
return Promise.all(this._page.allInitScripts().map(initScript => this.addInitScript(initScript)));
}
potentiallyUninitializedPage(): Page { potentiallyUninitializedPage(): Page {
return this._page; return this._page;
} }
@ -318,15 +324,20 @@ export class BidiPage implements PageDelegate {
} }
async addInitScript(initScript: InitScript): Promise<void> { async addInitScript(initScript: InitScript): Promise<void> {
await this._updateBootstrapScript(); const { script } = await this._session.send('script.addPreloadScript', {
// TODO: remove function call from the source.
functionDeclaration: `() => { return ${initScript.source} }`,
// TODO: push to iframes?
contexts: [this._session.sessionId],
});
if (!initScript.internal)
this._initScriptIds.push(script);
} }
async removeNonInternalInitScripts() { async removeNonInternalInitScripts() {
await this._updateBootstrapScript(); const promises = this._initScriptIds.map(script => this._session.send('script.removePreloadScript', { script }));
} this._initScriptIds = [];
await Promise.all(promises);
async _updateBootstrapScript(): Promise<void> {
throw new Error('Method not implemented.');
} }
async closePage(runBeforeUnload: boolean): Promise<void> { async closePage(runBeforeUnload: boolean): Promise<void> {