From 0e092d2ea0ec18ad7f07ad3507deb80f96086e7a Mon Sep 17 00:00:00 2001 From: galr52 Date: Tue, 6 Apr 2021 16:14:31 +0300 Subject: [PATCH] feat: accept captureBeyondViewport as optional screenshot param (#7063) Issue: #7063 --- src/common/Page.ts | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/src/common/Page.ts b/src/common/Page.ts index c80cd78a..fd27620b 100644 --- a/src/common/Page.ts +++ b/src/common/Page.ts @@ -183,6 +183,11 @@ export interface ScreenshotOptions { * @defaultValue 'binary' */ encoding?: 'base64' | 'binary'; + /** + * If you need a screenshot bigger than the Viewport + * @defaultValue true + */ + captureBeyondViewport?: boolean; } /** @@ -1796,6 +1801,9 @@ export class Page extends EventEmitter { targetId: this._target._targetId, }); let clip = options.clip ? processClip(options.clip) : undefined; + let { captureBeyondViewport = true } = options; + captureBeyondViewport = + typeof captureBeyondViewport === 'boolean' ? captureBeyondViewport : true; if (options.fullPage) { const metrics = await this._client.send('Page.getLayoutMetrics'); @@ -1804,17 +1812,33 @@ export class Page extends EventEmitter { // Overwrite clip for full page. clip = { x: 0, y: 0, width, height, scale: 1 }; + + if (!captureBeyondViewport) { + const { isMobile = false, deviceScaleFactor = 1, isLandscape = false } = + this._viewport || {}; + const screenOrientation: Protocol.Emulation.ScreenOrientation = isLandscape + ? { angle: 90, type: 'landscapePrimary' } + : { angle: 0, type: 'portraitPrimary' }; + await this._client.send('Emulation.setDeviceMetricsOverride', { + mobile: isMobile, + width, + height, + deviceScaleFactor, + screenOrientation, + }); + } } const shouldSetDefaultBackground = options.omitBackground && format === 'png'; if (shouldSetDefaultBackground) { await this._setTransparentBackgroundColor(); } + const result = await this._client.send('Page.captureScreenshot', { format, quality: options.quality, clip, - captureBeyondViewport: true, + captureBeyondViewport, }); if (shouldSetDefaultBackground) { await this._resetDefaultBackgroundColor();