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