Skip to content

Commit f764ff3

Browse files
kblokMeir017
authored andcommitted
Introducing Burst mode (#705)
* Burst mode * CodeFactor * Ops * cr * cr
1 parent 7a5b155 commit f764ff3

File tree

4 files changed

+130
-57
lines changed

4 files changed

+130
-57
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using Newtonsoft.Json;
2+
3+
namespace PuppeteerSharp.Messaging
4+
{
5+
internal class PageCaptureScreenshotResponse
6+
{
7+
[JsonProperty("data")]
8+
public string Data { get; set; }
9+
}
10+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using Newtonsoft.Json;
2+
3+
namespace PuppeteerSharp.Messaging
4+
{
5+
internal class PageGetLayoutMetricsResponse
6+
{
7+
[JsonProperty("contentSize")]
8+
public LayourContentSize ContentSize { get; set; }
9+
10+
public class LayourContentSize
11+
{
12+
[JsonProperty("width")]
13+
public decimal Width { get; set; }
14+
[JsonProperty("height")]
15+
public decimal Height { get; set; }
16+
}
17+
}
18+
}

lib/PuppeteerSharp/Page.cs

Lines changed: 95 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ public class Page : IDisposable
4343
private readonly Dictionary<string, Worker> _workers;
4444
private readonly ILogger _logger;
4545
private bool _ensureNewDocumentNavigation;
46+
private PageGetLayoutMetricsResponse _burstModeMetrics;
47+
private bool _screenshotBurstModeOn;
48+
private ScreenshotOptions _screenshotBurstModeOptions;
4649

4750
private static readonly Dictionary<string, decimal> _unitToPixels = new Dictionary<string, decimal> {
4851
{"px", 1},
@@ -1565,6 +1568,20 @@ await Task.WhenAny(new[]
15651568
/// <param name="options">Navigation parameters.</param>
15661569
public Task<Response> GoForwardAsync(NavigationOptions options = null) => GoAsync(1, options);
15671570

1571+
/// <summary>
1572+
/// Resets the background color and Viewport after taking Screenshots using BurstMode.
1573+
/// </summary>
1574+
/// <returns>The burst mode off.</returns>
1575+
public Task SetBurstModeOff()
1576+
{
1577+
_screenshotBurstModeOn = false;
1578+
if (_screenshotBurstModeOptions != null)
1579+
{
1580+
ResetBackgroundColorAndViewport(_screenshotBurstModeOptions);
1581+
}
1582+
1583+
return Task.CompletedTask;
1584+
}
15681585
#endregion
15691586

15701587
#region Private Method
@@ -1645,72 +1662,86 @@ private Dictionary<string, decimal> BuildMetricsObject(List<Metric> metrics)
16451662

16461663
private async Task<string> PerformScreenshot(ScreenshotType type, ScreenshotOptions options)
16471664
{
1648-
await Client.SendAsync("Target.activateTarget", new
1665+
if (!_screenshotBurstModeOn)
16491666
{
1650-
targetId = Target.TargetId
1651-
}).ConfigureAwait(false);
1667+
await Client.SendAsync("Target.activateTarget", new
1668+
{
1669+
targetId = Target.TargetId
1670+
}).ConfigureAwait(false);
1671+
}
16521672

16531673
var clip = options.Clip?.Clone();
16541674
if (clip != null)
16551675
{
16561676
clip.Scale = 1;
16571677
}
16581678

1659-
if (options != null && options.FullPage)
1679+
if (!_screenshotBurstModeOn)
16601680
{
1661-
var metrics = await Client.SendAsync("Page.getLayoutMetrics").ConfigureAwait(false);
1662-
var contentSize = metrics[MessageKeys.ContentSize];
1663-
1664-
var width = Convert.ToInt32(Math.Ceiling(contentSize[MessageKeys.Width].Value<decimal>()));
1665-
var height = Convert.ToInt32(Math.Ceiling(contentSize[MessageKeys.Height].Value<decimal>()));
1666-
1667-
// Overwrite clip for full page at all times.
1668-
clip = new Clip
1681+
if (options != null && options.FullPage)
16691682
{
1670-
X = 0,
1671-
Y = 0,
1672-
Width = width,
1673-
Height = height,
1674-
Scale = 1
1675-
};
1676-
1677-
var isMobile = Viewport?.IsMobile ?? false;
1678-
var deviceScaleFactor = Viewport?.DeviceScaleFactor ?? 1;
1679-
var isLandscape = Viewport?.IsLandscape ?? false;
1680-
var screenOrientation = isLandscape ?
1681-
new ScreenOrientation
1683+
var metrics = _screenshotBurstModeOn
1684+
? _burstModeMetrics :
1685+
await Client.SendAsync<PageGetLayoutMetricsResponse>("Page.getLayoutMetrics").ConfigureAwait(false);
1686+
1687+
if (options.BurstMode)
16821688
{
1683-
Angle = 90,
1684-
Type = ScreenOrientationType.LandscapePrimary
1685-
} :
1686-
new ScreenOrientation
1689+
_burstModeMetrics = metrics;
1690+
}
1691+
1692+
var contentSize = metrics.ContentSize;
1693+
1694+
var width = Convert.ToInt32(Math.Ceiling(contentSize.Width));
1695+
var height = Convert.ToInt32(Math.Ceiling(contentSize.Height));
1696+
1697+
// Overwrite clip for full page at all times.
1698+
clip = new Clip
16871699
{
1688-
Angle = 0,
1689-
Type = ScreenOrientationType.PortraitPrimary
1700+
X = 0,
1701+
Y = 0,
1702+
Width = width,
1703+
Height = height,
1704+
Scale = 1
16901705
};
16911706

1692-
await Client.SendAsync("Emulation.setDeviceMetricsOverride", new
1693-
{
1694-
mobile = isMobile,
1695-
width,
1696-
height,
1697-
deviceScaleFactor,
1698-
screenOrientation
1699-
}).ConfigureAwait(false);
1700-
}
1707+
var isMobile = Viewport?.IsMobile ?? false;
1708+
var deviceScaleFactor = Viewport?.DeviceScaleFactor ?? 1;
1709+
var isLandscape = Viewport?.IsLandscape ?? false;
1710+
var screenOrientation = isLandscape ?
1711+
new ScreenOrientation
1712+
{
1713+
Angle = 90,
1714+
Type = ScreenOrientationType.LandscapePrimary
1715+
} :
1716+
new ScreenOrientation
1717+
{
1718+
Angle = 0,
1719+
Type = ScreenOrientationType.PortraitPrimary
1720+
};
1721+
1722+
await Client.SendAsync("Emulation.setDeviceMetricsOverride", new
1723+
{
1724+
mobile = isMobile,
1725+
width,
1726+
height,
1727+
deviceScaleFactor,
1728+
screenOrientation
1729+
}).ConfigureAwait(false);
1730+
}
17011731

1702-
if (options != null && options.OmitBackground)
1703-
{
1704-
await Client.SendAsync("Emulation.setDefaultBackgroundColorOverride", new
1732+
if (options != null && options.OmitBackground)
17051733
{
1706-
color = new
1734+
await Client.SendAsync("Emulation.setDefaultBackgroundColorOverride", new
17071735
{
1708-
r = 0,
1709-
g = 0,
1710-
b = 0,
1711-
a = 0
1712-
}
1713-
}).ConfigureAwait(false);
1736+
color = new
1737+
{
1738+
r = 0,
1739+
g = 0,
1740+
b = 0,
1741+
a = 0
1742+
}
1743+
}).ConfigureAwait(false);
1744+
}
17141745
}
17151746

17161747
dynamic screenMessage = new ExpandoObject();
@@ -1727,19 +1758,27 @@ private async Task<string> PerformScreenshot(ScreenshotType type, ScreenshotOpti
17271758
screenMessage.clip = clip;
17281759
}
17291760

1730-
JObject result = await Client.SendAsync("Page.captureScreenshot", screenMessage).ConfigureAwait(false);
1761+
var result = await Client.SendAsync<PageCaptureScreenshotResponse>("Page.captureScreenshot", screenMessage).ConfigureAwait(false);
17311762

1732-
if (options != null && options.OmitBackground)
1763+
if (options.BurstMode)
17331764
{
1734-
await Client.SendAsync("Emulation.setDefaultBackgroundColorOverride").ConfigureAwait(false);
1765+
_screenshotBurstModeOptions = options;
1766+
_screenshotBurstModeOn = true;
17351767
}
1736-
1737-
if (options?.FullPage == true && Viewport != null)
1768+
else
17381769
{
1739-
await SetViewportAsync(Viewport).ConfigureAwait(false);
1770+
await ResetBackgroundColorAndViewport(options);
17401771
}
1772+
return result.Data;
1773+
}
17411774

1742-
return result.GetValue(MessageKeys.Data).AsString();
1775+
private Task ResetBackgroundColorAndViewport(ScreenshotOptions options)
1776+
{
1777+
var omitBackgroundTask = options?.OmitBackground == true ?
1778+
Client.SendAsync("Emulation.setDefaultBackgroundColorOverride") : Task.CompletedTask;
1779+
var setViewPortTask = (options?.FullPage == true && Viewport != null) ?
1780+
SetViewportAsync(Viewport) : Task.CompletedTask;
1781+
return Task.WhenAll(omitBackgroundTask, setViewPortTask);
17431782
}
17441783

17451784
private decimal ConvertPrintParameterToInches(object parameter)

lib/PuppeteerSharp/ScreenshotOptions.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,13 @@ public class ScreenshotOptions
4949
/// <value>The quality.</value>
5050
[JsonProperty("quality")]
5151
public int? Quality { get; set; }
52-
52+
/// <summary>
53+
/// When BurstMode is <c>true</c> the screenshot process will only execute all the screenshot setup actions (background and metrics overrides)
54+
/// before the first screenshot call and it will ignore the reset actions after the screenshoot is taken.
55+
/// <see cref="Page.SetBurstModeOff"/> needs to be called after the last screenshot is taken.
56+
/// </summary>
57+
[JsonIgnore]
58+
public bool BurstMode { get; set; } = false;
5359
internal static ScreenshotType? GetScreenshotTypeFromFile(string file)
5460
{
5561
var extension = new FileInfo(file).Extension.Replace(".", string.Empty);

0 commit comments

Comments
 (0)