mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
refactor: extract toggling of Network Interception (#12266)
This commit is contained in:
parent
df986ef2dd
commit
402b4a4812
@ -511,18 +511,13 @@ export class BidiPage extends Page {
|
|||||||
return [...this.#workers];
|
return [...this.#workers];
|
||||||
}
|
}
|
||||||
|
|
||||||
#interception?: string;
|
#userInterception?: string;
|
||||||
override async setRequestInterception(enable: boolean): Promise<void> {
|
override async setRequestInterception(enable: boolean): Promise<void> {
|
||||||
if (enable && !this.#interception) {
|
this.#userInterception = await this.#toggleInterception(
|
||||||
this.#interception = await this.#frame.browsingContext.addIntercept({
|
[Bidi.Network.InterceptPhase.BeforeRequestSent],
|
||||||
phases: [Bidi.Network.InterceptPhase.BeforeRequestSent],
|
this.#userInterception,
|
||||||
});
|
enable
|
||||||
} else if (!enable && this.#interception) {
|
);
|
||||||
await this.#frame.browsingContext.userContext.browser.removeIntercept(
|
|
||||||
this.#interception
|
|
||||||
);
|
|
||||||
this.#interception = undefined;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -531,19 +526,33 @@ export class BidiPage extends Page {
|
|||||||
_credentials: Credentials | null = null;
|
_credentials: Credentials | null = null;
|
||||||
#authInterception?: string;
|
#authInterception?: string;
|
||||||
override async authenticate(credentials: Credentials | null): Promise<void> {
|
override async authenticate(credentials: Credentials | null): Promise<void> {
|
||||||
if (credentials && !this.#authInterception) {
|
this.#authInterception = await this.#toggleInterception(
|
||||||
this.#authInterception = await this.#frame.browsingContext.addIntercept({
|
[Bidi.Network.InterceptPhase.AuthRequired],
|
||||||
phases: [Bidi.Network.InterceptPhase.AuthRequired],
|
this.#authInterception,
|
||||||
});
|
Boolean(credentials)
|
||||||
} else if (!credentials && this.#authInterception) {
|
);
|
||||||
await this.#frame.browsingContext.userContext.browser.removeIntercept(
|
|
||||||
this.#authInterception
|
|
||||||
);
|
|
||||||
this.#authInterception = undefined;
|
|
||||||
}
|
|
||||||
this._credentials = credentials;
|
this._credentials = credentials;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async #toggleInterception(
|
||||||
|
phases: [Bidi.Network.InterceptPhase, ...Bidi.Network.InterceptPhase[]],
|
||||||
|
interception: string | undefined,
|
||||||
|
expected: boolean
|
||||||
|
): Promise<string | undefined> {
|
||||||
|
if (expected && !interception) {
|
||||||
|
return await this.#frame.browsingContext.addIntercept({
|
||||||
|
phases,
|
||||||
|
});
|
||||||
|
} else if (!expected && interception) {
|
||||||
|
await this.#frame.browsingContext.userContext.browser.removeIntercept(
|
||||||
|
interception
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
return interception;
|
||||||
|
}
|
||||||
|
|
||||||
override setDragInterception(): never {
|
override setDragInterception(): never {
|
||||||
throw new UnsupportedOperation();
|
throw new UnsupportedOperation();
|
||||||
}
|
}
|
||||||
|
@ -140,18 +140,16 @@ export class NetworkManager extends EventEmitter<NetworkManagerEvents> {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
async setExtraHTTPHeaders(
|
async setExtraHTTPHeaders(headers: Record<string, string>): Promise<void> {
|
||||||
extraHTTPHeaders: Record<string, string>
|
const extraHTTPHeaders: Record<string, string> = {};
|
||||||
): Promise<void> {
|
for (const [key, value] of Object.entries(headers)) {
|
||||||
this.#extraHTTPHeaders = {};
|
|
||||||
for (const key of Object.keys(extraHTTPHeaders)) {
|
|
||||||
const value = extraHTTPHeaders[key];
|
|
||||||
assert(
|
assert(
|
||||||
isString(value),
|
isString(value),
|
||||||
`Expected value of header "${key}" to be String, but "${typeof value}" is found.`
|
`Expected value of header "${key}" to be String, but "${typeof value}" is found.`
|
||||||
);
|
);
|
||||||
this.#extraHTTPHeaders[key.toLowerCase()] = value;
|
extraHTTPHeaders[key.toLowerCase()] = value;
|
||||||
}
|
}
|
||||||
|
this.#extraHTTPHeaders = extraHTTPHeaders;
|
||||||
|
|
||||||
await this.#applyToAllClients(this.#applyExtraHTTPHeaders.bind(this));
|
await this.#applyToAllClients(this.#applyExtraHTTPHeaders.bind(this));
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user