8000 feat: config download behavior (#13309) · puppeteer/puppeteer@c3ca96c · GitHub
[go: up one dir, main page]

Skip to content

Commit c3ca96c

Browse files
authored
feat: config download behavior (#13309)
1 parent e5be9de commit c3ca96c

17 files changed

+338
-4
lines changed

docs/api/index.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -929,6 +929,13 @@ Set of configurable options for CSS coverage.
929929
</td></tr>
930930
<tr><td>
931931

932+
<span id="downloadbehavior">[DownloadBehavior](./puppeteer.downloadbehavior.md)</span>
933+
934+
</td><td>
935+
936+
</td></tr>
937+
<tr><td>
938+
932939
<span id="elementscreenshotoptions">[ElementScreenshotOptions](./puppeteer.elementscreenshotoptions.md)</span>
933940

934941
</td><td>
@@ -1472,6 +1479,13 @@ Represents the source scheme of the origin that originally set the cookie. A val
14721479
</td></tr>
14731480
<tr><td>
14741481

1482+
<span id="downloadpolicy">[DownloadPolicy](./puppeteer.downloadpolicy.md)</span>
1483+
1484+
</td><td>
1485+
1486+
</td></tr>
1487+
<tr><td>
1488+
14751489
<span id="elementfor">[ElementFor](./puppeteer.elementfor.md)</span>
14761490

14771491
</td><td>

docs/api/puppeteer.browserconnectoptions.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,25 @@ Sets the viewport for each page.
7979
</td></tr>
8080
<tr><td>
8181

82+
<span id="downloadbehavior">downloadBehavior</span>
83+
84+
</td><td>
85+
86+
`optional`
87+
88+
</td><td>
89+
90+
[DownloadBehavior](./puppeteer.downloadbehavior.md)
91+
92+
</td><td>
93+
94+
Sets the download behavior for the context.
95+
96+
</td><td>
97+
98+
</td></tr>
99+
<tr><td>
100+
82101
<span id="protocol">protocol</span>
83102

84103
</td><td>

docs/api/puppeteer.browsercontextoptions.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,29 @@ Default
3535
</th></tr></thead>
3636
<tbody><tr><td>
3737

38+
<span id="downloadbehavior">downloadBehavior</span>
39+
40+
</td><td>
41+
42+
`optional`
43+
44+
</td><td>
45+
46+
[DownloadBehavior](./puppeteer.downloadbehavior.md)
47+
48+
</td><td>
49+
50+
Behavior definition for when downloading a file.
51+
52+
**Remarks:**
53+
54+
If not set, the default behavior will be used.
55+
56+
</td><td>
57+
58+
</td></tr>
59+
<tr><td>
60+
3861
<span id="proxybypasslist">proxyBypassList</span>
3962

4063
</td><td>
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
---
2+
sidebar_label: DownloadBehavior
3+
---
4+
5+
# DownloadBehavior interface
6+
7+
### Signature
8+
9+
```typescript
10+
export interface DownloadBehavior
11+
```
12+
13+
## Properties
14+
15+
<table><thead><tr><th>
16+
17+
Property
18+
19+
</th><th>
20+
21+
Modifiers
22+
23+
</th><th>
24+
25+
Type
26+
27+
</th><th>
28+
29+
Description
30+
31+
</th><th>
32+
33+
Default
34+
35+
</th></tr></thead>
36+
<tbody><tr><td>
37+
38+
<span id="downloadpath">downloadPath</span>
39+
40+
</td><td>
41+
42+
`optional`
43+
44+
</td><td>
45+
46+
string
47+
48+
</td><td>
49+
50+
The default path to save downloaded files to.
51+
52+
**Remarks:**
53+
54+
Setting this is required if behavior is set to `allow` or `allowAndName`.
55+
56+
</td><td>
57+
58+
</td></tr>
59+
<tr><td>
60+
61+
<span id="policy">policy</span>
62+
63+
</td><td>
64+
65+
</td><td>
66+
67+
[DownloadPolicy](./puppeteer.downloadpolicy.md)
68+
69+
</td><td>
70+
71+
Whether to allow all or deny all download requests, or use default behavior if available.
72+
73+
**Remarks:**
74+
75+
Setting this to `allowAndName` will name all files according to their download guids.
76+
77+
</td><td>
78+
79+
</td></tr>
80+
</tbody></table>

docs/api/puppeteer.downloadpolicy.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
sidebar_label: DownloadPolicy
3+
---
4+
5+
# DownloadPolicy type
6+
7+
### Signature
8+
9+
```typescript
10+
export type DownloadPolicy = 'deny' | 'allow' | 'allowAndName' | 'default';
11+
```

packages/puppeteer-core/src/api/Browser.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
raceWith,
1616
} from '../../third_party/rxjs/rxjs.js';
1717
import type {ProtocolType} from '../common/ConnectOptions.js';
18+
import type {DownloadBehavior} from '../common/DownloadBehavior.js';
1819
import {EventEmitter, type EventType} from '../common/EventEmitter.js';
1920
import {
2021
debugError,
@@ -41,6 +42,13 @@ export interface BrowserContextOptions {
4142
* Bypass the proxy for the given list of hosts.
4243
*/
4344
proxyBypassList?: string[];
45+
/**
46+
* Behavior definition for when downloading a file.
47+
*
48+
* @remarks
49+
* If not set, the default behavior will be used.
50+
*/
51+
downloadBehavior?: DownloadBehavior;
4452
}
4553

4654
/**

packages/puppeteer-core/src/cdp/Browser.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {BrowserContextEvent} from '../api/BrowserContext.js';
2121
import {CDPSessionEvent, type CDPSession} from '../api/CDPSession.js';
2222
import type {Page} from '../api/Page.js';
2323
import type {Target} from '../api/Target.js';
24+
import type {DownloadBehavior} from '../common/DownloadBehavior.js';
2425
import type {Viewport} from '../common/Viewport.js';
2526

2627
import {CdpBrowserContext} from './BrowserContext.js';
@@ -49,6 +50,7 @@ export class CdpBrowser extends BrowserBase {
4950
contextIds: string[],
5051
acceptInsecureCerts: boolean,
5152
defaultViewport?: Viewport | null,
53+
downloadBehavior?: DownloadBehavior,
5254
process?: ChildProcess,
5355
closeCallback?: BrowserCloseCallback,
5456
targetFilterCallback?: TargetFilterCallback,
@@ -71,7 +73,7 @@ export class CdpBrowser extends BrowserBase {
7173
ignore: true,
7274
});
7375
}
74-
await browser._attach();
76+
await browser._attach(downloadBehavior);
7577
return browser;
7678
}
7779
#defaultViewport?: Viewport | null;
@@ -134,8 +136,11 @@ export class CdpBrowser extends BrowserBase {
134136
this.emit(BrowserEvent.Disconnected, undefined);
135137
};
136138

137-
async _attach(): Promise<void> {
139+
async _attach(downloadBehavior: DownloadBehavior | undefined): Promise<void> {
138140
this.#connection.on(CDPSessionEvent.Disconnected, this.#emitDisconnected);
141+
if (downloadBehavior) {
142+
await this.#defaultContext.setDownloadBehavior(downloadBehavior);
143+
}
139144
this.#targetManager.on(
140145
TargetManagerEvent.TargetAvailable,
141146
this.#onAttachedToTarget,
@@ -202,7 +207,7 @@ export class CdpBrowser extends BrowserBase {
202207
override async createBrowserContext(
203208
options: BrowserContextOptions = {},
204209
): Promise<CdpBrowserContext> {
205-
const {proxyServer, proxyBypassList} = options;
210+
const {proxyServer, proxyBypassList, downloadBehavior} = options;
206211

207212
const {browserContextId} = await this.#connection.send(
208213
'Target.createBrowserContext',
@@ -216,6 +221,9 @@ export class CdpBrowser extends BrowserBase {
216221
this,
217222
browserContextId,
218223
);
224+
if (downloadBehavior) {
225+
await context.setDownloadBehavior(downloadBehavior);
226+
}
219227
this.#contexts.set(browserContextId, context);
220228
return context;
221229
}

packages/puppeteer-core/src/cdp/BrowserConnector.ts

Expand all lines: packages/puppeteer-core/src/cdp/BrowserConnector.ts
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export async function _connectToCdpBrowser(
2828
const {
2929
acceptInsecureCerts = false,
3030
defaultViewport = DEFAULT_VIEWPORT,
31+
downloadBehavior,
3132
targetFilter,
3233
_isPageTarget: isPageTarget,
3334
slowMo = 0,
@@ -55,6 +56,7 @@ export async function _connectToCdpBrowser(
5556
browserContextIds,
5657
acceptInsecureCerts,
5758
defaultViewport,
59+
downloadBehavior,
5860
undefined,
5961
() => {
6062
return connection.send('Browser.close').catch(debugError);

packages/puppeteer-core/src/cdp/BrowserContext.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
} from '../api/Browser.js';
1111
import {BrowserContext} from '../api/BrowserContext.js';
1212
import type {Page} from '../api/Page.js';
13+
import type {DownloadBehavior} from '../common/DownloadBehavior.js';
1314
import {assert} from '../util/assert.js';
1415

1516
import type {CdpBrowser} from './Browser.js';
@@ -98,4 +99,14 @@ export class CdpBrowserContext extends BrowserContext {
9899
assert(this.#id, 'Default BrowserContext cannot be closed!');
99100
await this.#browser._disposeContext(this.#id);
100101
}
102+
103+
public async setDownloadBehavior(
104+
downloadBehavior: DownloadBehavior,
105+
): Promise<void> {
106+
await this.#connection.send('Browser.setDownloadBehavior', {
107+
behavior: downloadBehavior.policy,
108+
downloadPath: downloadBehavior.downloadPath,
109+
browserContextId: this.#id,
110+
});
111+
}
101112
}

packages/puppeteer-core/src/common/ConnectOptions.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import type {
1212
} from '../api/Browser.js';
1313

1414
import type {ConnectionTransport} from './ConnectionTransport.js';
15+
import type {DownloadBehavior} from './DownloadBehavior.js';
1516
import type {Viewport} from './Viewport.js';
1617

1718
/**
@@ -54,6 +55,10 @@ export interface BrowserConnectOptions {
5455
* @defaultValue '\{width: 800, height: 600\}'
5556
*/
5657
defaultViewport?: Viewport | null;
58+
/**
59+
* Sets the download behavior for the context.
60+
*/
61+
downloadBehavior?: DownloadBehavior;
5762
/**
5863
* Slows down Puppeteer operations by the specified amount of milliseconds to
5964
* aid debugging.

0 commit comments

Comments
 (0)
0