fix: set defaults in screenshot (#11021)

This commit is contained in:
jrandolf 2023-09-25 15:05:20 +02:00 committed by GitHub
parent 52468adcff
commit ace1230e41
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 46 additions and 49 deletions

View File

@ -17,7 +17,7 @@ export interface ScreenshotOptions
| captureBeyondViewport | <code>optional</code> | boolean | Capture the screenshot beyond the viewport. | <code>true</code> | | captureBeyondViewport | <code>optional</code> | boolean | Capture the screenshot beyond the viewport. | <code>true</code> |
| clip | <code>optional</code> | [ScreenshotClip](./puppeteer.screenshotclip.md) | Specifies the region of the page to clip. | | | clip | <code>optional</code> | [ScreenshotClip](./puppeteer.screenshotclip.md) | Specifies the region of the page to clip. | |
| encoding | <code>optional</code> | 'base64' \| 'binary' | Encoding of the image. | <code>'binary'</code> | | encoding | <code>optional</code> | 'base64' \| 'binary' | Encoding of the image. | <code>'binary'</code> |
| fromSurface | <code>optional</code> | boolean | Capture the screenshot from the surface, rather than the view. | <code>false</code> | | fromSurface | <code>optional</code> | boolean | Capture the screenshot from the surface, rather than the view. | <code>true</code> |
| fullPage | <code>optional</code> | boolean | When <code>true</code>, takes a screenshot of the full page. | <code>false</code> | | fullPage | <code>optional</code> | boolean | When <code>true</code>, takes a screenshot of the full page. | <code>false</code> |
| omitBackground | <code>optional</code> | boolean | Hides default white background and allows capturing screenshots with transparency. | <code>false</code> | | omitBackground | <code>optional</code> | boolean | Hides default white background and allows capturing screenshots with transparency. | <code>false</code> |
| optimizeForSpeed | <code>optional</code> | boolean | | <code>false</code> | | optimizeForSpeed | <code>optional</code> | boolean | | <code>false</code> |

View File

@ -242,7 +242,7 @@ export interface ScreenshotOptions {
/** /**
* Capture the screenshot from the surface, rather than the view. * Capture the screenshot from the surface, rather than the view.
* *
* @defaultValue `false` * @defaultValue `true`
*/ */
fromSurface?: boolean; fromSurface?: boolean;
/** /**
@ -495,6 +495,20 @@ export interface NewDocumentScriptEvaluation {
identifier: string; identifier: string;
} }
/**
* @internal
*/
export function setDefaultScreenshotOptions(options: ScreenshotOptions): void {
options.optimizeForSpeed ??= false;
options.type ??= 'png';
options.fromSurface ??= true;
options.fullPage ??= false;
options.omitBackground ??= false;
options.encoding ??= 'binary';
options.captureBeyondViewport ??= true;
options.allowViewportExpansion ??= options.captureBeyondViewport;
}
/** /**
* Page provides methods to interact with a single tab or * Page provides methods to interact with a single tab or
* {@link https://developer.chrome.com/extensions/background_pages | extension background page} * {@link https://developer.chrome.com/extensions/background_pages | extension background page}
@ -2285,7 +2299,7 @@ export abstract class Page extends EventEmitter<PageEvents> {
...userOptions.clip, ...userOptions.clip,
} }
: undefined, : undefined,
} as ScreenshotOptions; };
if (options.type === undefined && options.path !== undefined) { if (options.type === undefined && options.path !== undefined) {
const filePath = options.path; const filePath = options.path;
// Note we cannot use Node.js here due to browser compatability. // Note we cannot use Node.js here due to browser compatability.
@ -2358,8 +2372,8 @@ export abstract class Page extends EventEmitter<PageEvents> {
); );
} }
options.captureBeyondViewport ??= true; setDefaultScreenshotOptions(options);
options.allowViewportExpansion ??= options.captureBeyondViewport;
options.clip = options.clip && roundClip(normalizeClip(options.clip)); options.clip = options.clip && roundClip(normalizeClip(options.clip));
await using stack = new AsyncDisposableStack(); await using stack = new AsyncDisposableStack();

View File

@ -647,52 +647,33 @@ export class BidiPage extends Page {
} }
} }
override async screenshot(
options: Readonly<ScreenshotOptions> & {encoding: 'base64'}
): Promise<string>;
override async screenshot(
options?: Readonly<ScreenshotOptions>
): Promise<Buffer>;
override async screenshot(
options: Readonly<ScreenshotOptions> = {}
): Promise<Buffer | string> {
const {
clip,
type,
captureBeyondViewport,
allowViewportExpansion = true,
} = options;
if (captureBeyondViewport) {
throw new Error(`BiDi does not support 'captureBeyondViewport'.`);
}
const invalidOption = Object.keys(options).find(option => {
return [
'fromSurface',
'omitBackground',
'optimizeForSpeed',
'quality',
].includes(option);
});
if (invalidOption !== undefined) {
throw new Error(`BiDi does not support ${invalidOption}.`);
}
if ((type ?? 'png') !== 'png') {
throw new Error(`BiDi only supports 'png' type.`);
}
if (clip?.scale !== undefined) {
throw new Error(`BiDi does not support 'scale' in 'clip'.`);
}
return await super.screenshot({
...options,
captureBeyondViewport,
allowViewportExpansion: captureBeyondViewport ?? allowViewportExpansion,
});
}
override async _screenshot( override async _screenshot(
options: Readonly<ScreenshotOptions> options: Readonly<ScreenshotOptions>
): Promise<string> { ): Promise<string> {
const {clip} = options; const {clip, type, captureBeyondViewport, allowViewportExpansion} = options;
if (captureBeyondViewport && !allowViewportExpansion) {
throw new Error(
`BiDi does not support 'captureBeyondViewport'. Use 'allowViewportExpansion'.`
);
}
if (options.omitBackground !== undefined && options.omitBackground) {
throw new Error(`BiDi does not support 'omitBackground'.`);
}
if (options.optimizeForSpeed !== undefined && options.optimizeForSpeed) {
throw new Error(`BiDi does not support 'optimizeForSpeed'.`);
}
if (options.fromSurface !== undefined && !options.fromSurface) {
throw new Error(`BiDi does not support 'fromSurface'.`);
}
if (options.quality !== undefined) {
throw new Error(`BiDi does not support 'quality'.`);
}
if (type === 'webp' || type === 'jpeg') {
throw new Error(`BiDi only supports 'png' type.`);
}
if (clip !== undefined && clip.scale !== undefined && clip.scale !== 1) {
throw new Error(`BiDi does not support 'scale' in 'clip'.`);
}
const { const {
result: {data}, result: {data},
} = await this.#connection.send('browsingContext.captureScreenshot', { } = await this.#connection.send('browsingContext.captureScreenshot', {

View File

@ -1039,7 +1039,9 @@ export class CdpPage extends Page {
await this.#frameManager.networkManager.setCacheEnabled(enabled); await this.#frameManager.networkManager.setCacheEnabled(enabled);
} }
async _screenshot(options: Readonly<ScreenshotOptions>): Promise<string> { override async _screenshot(
options: Readonly<ScreenshotOptions>
): Promise<string> {
const { const {
fromSurface, fromSurface,
omitBackground, omitBackground,