mirror of
https://github.com/microsoft/playwright.git
synced 2025-06-26 21:40:17 +00:00
chore: fix removeInitScript implementation (#36159)
This commit is contained in:
parent
c9d329155d
commit
f78918ab03
@ -209,6 +209,7 @@ export class BidiBrowserContext extends BrowserContext {
|
||||
declare readonly _browser: BidiBrowser;
|
||||
private _originToPermissions = new Map<string, string[]>();
|
||||
private _blockingPageCreations: Set<Promise<unknown>> = new Set();
|
||||
private _initScriptIds = new Map<InitScript, string>();
|
||||
|
||||
constructor(browser: BidiBrowser, browserContextId: string | undefined, options: types.BrowserContextOptions) {
|
||||
super(browser, options, browserContextId);
|
||||
@ -371,11 +372,18 @@ export class BidiBrowserContext extends BrowserContext {
|
||||
functionDeclaration: `() => { return ${initScript.source} }`,
|
||||
userContexts: [this._browserContextId || 'default'],
|
||||
});
|
||||
initScript.auxData = script;
|
||||
this._initScriptIds.set(initScript, script);
|
||||
}
|
||||
|
||||
async doRemoveInitScripts(initScripts: InitScript[]) {
|
||||
await Promise.all(initScripts.map(script => this._browser._browserSession.send('script.removePreloadScript', { script: script.auxData })));
|
||||
const ids: string[] = [];
|
||||
for (const script of initScripts) {
|
||||
const id = this._initScriptIds.get(script);
|
||||
if (id)
|
||||
ids.push(id);
|
||||
this._initScriptIds.delete(script);
|
||||
}
|
||||
await Promise.all(ids.map(script => this._browser._browserSession.send('script.removePreloadScript', { script })));
|
||||
}
|
||||
|
||||
async doUpdateRequestInterception(): Promise<void> {
|
||||
|
@ -50,6 +50,7 @@ export class BidiPage implements PageDelegate {
|
||||
readonly _browserContext: BidiBrowserContext;
|
||||
readonly _networkManager: BidiNetworkManager;
|
||||
private readonly _pdf: BidiPDF;
|
||||
private _initScriptIds = new Map<InitScript, string>();
|
||||
|
||||
constructor(browserContext: BidiBrowserContext, bidiSession: BidiSession, opener: BidiPage | null) {
|
||||
this._session = bidiSession;
|
||||
@ -341,11 +342,18 @@ export class BidiPage implements PageDelegate {
|
||||
// TODO: push to iframes?
|
||||
contexts: [this._session.sessionId],
|
||||
});
|
||||
initScript.auxData = script;
|
||||
this._initScriptIds.set(initScript, script);
|
||||
}
|
||||
|
||||
async removeInitScripts(initScripts: InitScript[]): Promise<void> {
|
||||
await Promise.all(initScripts.map(script => this._session.send('script.removePreloadScript', { script: script.auxData })));
|
||||
const ids: string[] = [];
|
||||
for (const script of initScripts) {
|
||||
const id = this._initScriptIds.get(script);
|
||||
if (id)
|
||||
ids.push(id);
|
||||
this._initScriptIds.delete(script);
|
||||
}
|
||||
await Promise.all(ids.map(script => this._session.send('script.removePreloadScript', { script })));
|
||||
}
|
||||
|
||||
async closePage(runBeforeUnload: boolean): Promise<void> {
|
||||
|
@ -394,6 +394,7 @@ class FrameSession {
|
||||
private _screencastClients = new Set<any>();
|
||||
private _metricsOverride: Protocol.Emulation.setDeviceMetricsOverrideParameters | undefined;
|
||||
private _workerSessions = new Map<string, CRSession>();
|
||||
private _initScriptIds = new Map<InitScript, string>();
|
||||
|
||||
constructor(crPage: CRPage, client: CRSession, targetId: string, parentSession: FrameSession | null) {
|
||||
this._client = client;
|
||||
@ -1058,11 +1059,18 @@ class FrameSession {
|
||||
async _evaluateOnNewDocument(initScript: InitScript, world: types.World, runImmediately?: boolean): Promise<void> {
|
||||
const worldName = world === 'utility' ? this._crPage.utilityWorldName : undefined;
|
||||
const { identifier } = await this._client.send('Page.addScriptToEvaluateOnNewDocument', { source: initScript.source, worldName, runImmediately });
|
||||
initScript.auxData = identifier;
|
||||
this._initScriptIds.set(initScript, identifier);
|
||||
}
|
||||
|
||||
async _removeEvaluatesOnNewDocument(initScripts: InitScript[]): Promise<void> {
|
||||
await Promise.all(initScripts.map(script => this._client.send('Page.removeScriptToEvaluateOnNewDocument', { identifier: script.auxData }).catch(() => {}))); // target can be closed
|
||||
const ids: string[] = [];
|
||||
for (const script of initScripts) {
|
||||
const id = this._initScriptIds.get(script);
|
||||
if (id)
|
||||
ids.push(id);
|
||||
this._initScriptIds.delete(script);
|
||||
}
|
||||
await Promise.all(ids.map(identifier => this._client.send('Page.removeScriptToEvaluateOnNewDocument', { identifier }).catch(() => {}))); // target can be closed
|
||||
}
|
||||
|
||||
async exposePlaywrightBinding() {
|
||||
|
@ -914,7 +914,6 @@ export class PageBinding {
|
||||
export class InitScript {
|
||||
readonly source: string;
|
||||
readonly name?: string;
|
||||
auxData: any; // Can be arbitrarily used by a browser-specific implementation.
|
||||
|
||||
constructor(source: string, name?: string) {
|
||||
this.source = `(() => {
|
||||
|
Loading…
x
Reference in New Issue
Block a user