refactor!: use Target for filters (#10601)
This commit is contained in:
parent
0715ad8281
commit
44712d1e6e
@ -7,7 +7,7 @@ sidebar_label: TargetFilterCallback
|
||||
#### Signature:
|
||||
|
||||
```typescript
|
||||
export type TargetFilterCallback = (
|
||||
target: Protocol.Target.TargetInfo
|
||||
) => boolean;
|
||||
export type TargetFilterCallback = (target: Target) => boolean;
|
||||
```
|
||||
|
||||
**References:** [Target](./puppeteer.target.md)
|
||||
|
@ -51,16 +51,12 @@ export type BrowserCloseCallback = () => Promise<void> | void;
|
||||
/**
|
||||
* @public
|
||||
*/
|
||||
export type TargetFilterCallback = (
|
||||
target: Protocol.Target.TargetInfo
|
||||
) => boolean;
|
||||
export type TargetFilterCallback = (target: Target) => boolean;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
export type IsPageTargetCallback = (
|
||||
target: Protocol.Target.TargetInfo
|
||||
) => boolean;
|
||||
export type IsPageTargetCallback = (target: Target) => boolean;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
|
@ -226,11 +226,11 @@ export class CDPBrowser extends BrowserBase {
|
||||
#setIsPageTargetCallback(isPageTargetCallback?: IsPageTargetCallback): void {
|
||||
this.#isPageTargetCallback =
|
||||
isPageTargetCallback ||
|
||||
((target: Protocol.Target.TargetInfo): boolean => {
|
||||
((target: Target): boolean => {
|
||||
return (
|
||||
target.type === 'page' ||
|
||||
target.type === 'background_page' ||
|
||||
target.type === 'webview'
|
||||
target.type() === 'page' ||
|
||||
target.type() === 'background_page' ||
|
||||
target.type() === 'webview'
|
||||
);
|
||||
});
|
||||
}
|
||||
@ -326,7 +326,14 @@ export class CDPBrowser extends BrowserBase {
|
||||
const createSession = (isAutoAttachEmulated: boolean) => {
|
||||
return this.#connection._createSession(targetInfo, isAutoAttachEmulated);
|
||||
};
|
||||
if (this.#isPageTargetCallback(targetInfo)) {
|
||||
const targetForFilter = new OtherTarget(
|
||||
targetInfo,
|
||||
session,
|
||||
context,
|
||||
this.#targetManager,
|
||||
createSession
|
||||
);
|
||||
if (this.#isPageTargetCallback(targetForFilter)) {
|
||||
return new PageTarget(
|
||||
targetInfo,
|
||||
session,
|
||||
@ -638,9 +645,7 @@ export class CDPBrowserContext extends BrowserContext {
|
||||
return (
|
||||
target.type() === 'page' ||
|
||||
(target.type() === 'other' &&
|
||||
this.#browser._getIsPageTargetCallback()?.(
|
||||
target._getTargetInfo()
|
||||
))
|
||||
this.#browser._getIsPageTargetCallback()?.(target))
|
||||
);
|
||||
})
|
||||
.map(target => {
|
||||
|
@ -115,9 +115,16 @@ export class ChromeTargetManager extends EventEmitter implements TargetManager {
|
||||
targetId,
|
||||
targetInfo,
|
||||
] of this.#discoveredTargetsByTargetId.entries()) {
|
||||
const targetForFilter = new Target(
|
||||
targetInfo,
|
||||
undefined,
|
||||
undefined,
|
||||
this,
|
||||
undefined
|
||||
);
|
||||
if (
|
||||
(!this.#targetFilterCallback ||
|
||||
this.#targetFilterCallback(targetInfo)) &&
|
||||
this.#targetFilterCallback(targetForFilter)) &&
|
||||
targetInfo.type !== 'browser'
|
||||
) {
|
||||
this.#targetsIdsForInit.add(targetId);
|
||||
@ -339,7 +346,7 @@ export class ChromeTargetManager extends EventEmitter implements TargetManager {
|
||||
? this.#attachedTargetsByTargetId.get(targetInfo.targetId)!
|
||||
: this.#targetFactory(targetInfo, session);
|
||||
|
||||
if (this.#targetFilterCallback && !this.#targetFilterCallback(targetInfo)) {
|
||||
if (this.#targetFilterCallback && !this.#targetFilterCallback(target)) {
|
||||
this.#ignoredTargets.add(targetInfo.targetId);
|
||||
this.#finishInitializationIfReady(targetInfo.targetId);
|
||||
await silentDetach();
|
||||
|
@ -194,10 +194,7 @@ export class FirefoxTargetManager
|
||||
}
|
||||
|
||||
const target = this.#targetFactory(event.targetInfo, undefined);
|
||||
if (
|
||||
this.#targetFilterCallback &&
|
||||
!this.#targetFilterCallback(event.targetInfo)
|
||||
) {
|
||||
if (this.#targetFilterCallback && !this.#targetFilterCallback(target)) {
|
||||
this.#ignoredTargets.add(event.targetInfo.targetId);
|
||||
this.#finishInitializationIfReady(event.targetInfo.targetId);
|
||||
return;
|
||||
|
@ -46,11 +46,13 @@ export enum InitializationStatus {
|
||||
* @public
|
||||
*/
|
||||
export class Target {
|
||||
#browserContext: BrowserContext;
|
||||
#browserContext?: BrowserContext;
|
||||
#session?: CDPSession;
|
||||
#targetInfo: Protocol.Target.TargetInfo;
|
||||
#targetManager: TargetManager;
|
||||
#sessionFactory: (isAutoAttachEmulated: boolean) => Promise<CDPSession>;
|
||||
#targetManager?: TargetManager;
|
||||
#sessionFactory:
|
||||
| ((isAutoAttachEmulated: boolean) => Promise<CDPSession>)
|
||||
| undefined;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
@ -73,9 +75,11 @@ export class Target {
|
||||
constructor(
|
||||
targetInfo: Protocol.Target.TargetInfo,
|
||||
session: CDPSession | undefined,
|
||||
browserContext: BrowserContext,
|
||||
targetManager: TargetManager,
|
||||
sessionFactory: (isAutoAttachEmulated: boolean) => Promise<CDPSession>
|
||||
browserContext: BrowserContext | undefined,
|
||||
targetManager: TargetManager | undefined,
|
||||
sessionFactory:
|
||||
| ((isAutoAttachEmulated: boolean) => Promise<CDPSession>)
|
||||
| undefined
|
||||
) {
|
||||
this.#session = session;
|
||||
this.#targetManager = targetManager;
|
||||
@ -98,6 +102,9 @@ export class Target {
|
||||
protected _sessionFactory(): (
|
||||
isAutoAttachEmulated: boolean
|
||||
) => Promise<CDPSession> {
|
||||
if (!this.#sessionFactory) {
|
||||
throw new Error('sessionFactory is not initialized');
|
||||
}
|
||||
return this.#sessionFactory;
|
||||
}
|
||||
|
||||
@ -105,6 +112,9 @@ export class Target {
|
||||
* Creates a Chrome Devtools Protocol session attached to the target.
|
||||
*/
|
||||
createCDPSession(): Promise<CDPSession> {
|
||||
if (!this.#sessionFactory) {
|
||||
throw new Error('sessionFactory is not initialized');
|
||||
}
|
||||
return this.#sessionFactory(false);
|
||||
}
|
||||
|
||||
@ -112,6 +122,9 @@ export class Target {
|
||||
* @internal
|
||||
*/
|
||||
_targetManager(): TargetManager {
|
||||
if (!this.#targetManager) {
|
||||
throw new Error('targetManager is not initialized');
|
||||
}
|
||||
return this.#targetManager;
|
||||
}
|
||||
|
||||
@ -166,6 +179,9 @@ export class Target {
|
||||
* Get the browser the target belongs to.
|
||||
*/
|
||||
browser(): Browser {
|
||||
if (!this.#browserContext) {
|
||||
throw new Error('browserContext is not initialised');
|
||||
}
|
||||
return this.#browserContext.browser();
|
||||
}
|
||||
|
||||
@ -173,6 +189,9 @@ export class Target {
|
||||
* Get the browser context the target belongs to.
|
||||
*/
|
||||
browserContext(): BrowserContext {
|
||||
if (!this.#browserContext) {
|
||||
throw new Error('browserContext is not initialised');
|
||||
}
|
||||
return this.#browserContext;
|
||||
}
|
||||
|
||||
|
@ -171,7 +171,7 @@ const serviceWorkerExtensionPath = path.join(
|
||||
browserWSEndpoint,
|
||||
_isPageTarget(target) {
|
||||
return (
|
||||
target.type === 'other' && target.url.startsWith('devtools://')
|
||||
target.type() === 'other' && target.url().startsWith('devtools://')
|
||||
);
|
||||
},
|
||||
});
|
||||
|
@ -19,7 +19,6 @@ import os from 'os';
|
||||
import path from 'path';
|
||||
import {TLSSocket} from 'tls';
|
||||
|
||||
import {Protocol} from 'devtools-protocol';
|
||||
import expect from 'expect';
|
||||
import {TimeoutError} from 'puppeteer';
|
||||
import {Page} from 'puppeteer-core/internal/api/Page.js';
|
||||
@ -709,7 +708,7 @@ describe('Launcher specs', function () {
|
||||
const {browser, close} = await launch(
|
||||
{
|
||||
targetFilter: target => {
|
||||
return target.type !== 'page';
|
||||
return target.type() !== 'page';
|
||||
},
|
||||
waitForInitialPage: false,
|
||||
},
|
||||
@ -746,8 +745,8 @@ describe('Launcher specs', function () {
|
||||
|
||||
const remoteBrowser = await puppeteer.connect({
|
||||
browserWSEndpoint,
|
||||
targetFilter: (targetInfo: Protocol.Target.TargetInfo) => {
|
||||
return !targetInfo.url?.includes('should-be-ignored');
|
||||
targetFilter: target => {
|
||||
return !target.url().includes('should-be-ignored');
|
||||
},
|
||||
});
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user