Skip to content

Commit

Permalink
Fill cropped area with body's background color to have equally sized …
Browse files Browse the repository at this point in the history
…screenshots
  • Loading branch information
MediaMarco committed May 23, 2024
1 parent 45cd195 commit fb2baeb
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion core/src/main/java/de/otto/jlineup/browser/Browser.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ public static Type forValue(String value) {
static final String JS_CHECK_FOR_ELEMENT_CALL = "return document.querySelector('%s') !== null;";

static final String JS_GET_DEVICE_PIXEL_RATIO_CALL = "return window.devicePixelRatio;";
static final String JS_GET_BODY_COLOR_CALL = "return window.getComputedStyle(document.body).getPropertyValue('background-color').match(/\\d+/g);";

private final JobConfig jobConfig;
private final FileService fileService;
Expand Down Expand Up @@ -453,7 +454,14 @@ private void takeScreenshotsForContext(final ScreenshotContext screenshotContext

if (yPosition + viewportHeight > pageHeight && GlobalOptions.getOption(GlobalOption.JLINEUP_CROP_LAST_SCREENSHOT).equalsIgnoreCase("true")) {
LOG.debug("Last screenshot. Will crop image. Page height: {}, yPosition: {}, viewportHeight: {}", pageHeight, yPosition, viewportHeight);
currentScreenshot = currentScreenshot.getSubimage(0, (int)((yPosition + viewportHeight - pageHeight) * getDevicePixelRatio()), currentScreenshot.getWidth(), (int)((pageHeight.intValue() - yPosition) * getDevicePixelRatio()));

BufferedImage croppedAndFilledScreenshot = new BufferedImage(currentScreenshot.getWidth(), currentScreenshot.getHeight(), BufferedImage.TYPE_4BYTE_ABGR);
Graphics2D g = croppedAndFilledScreenshot.createGraphics();
g.setColor(getBodyColor());
g.fillRect(0, 0, currentScreenshot.getWidth(), currentScreenshot.getHeight());
// Draw shifted image
g.drawImage(currentScreenshot, 0, -(int)((yPosition + viewportHeight - pageHeight) * getDevicePixelRatio()), null);
currentScreenshot = croppedAndFilledScreenshot;
}

fileService.writeScreenshot(screenshotContext,
Expand Down Expand Up @@ -687,6 +695,13 @@ private String getUserAgent() {
return (String) jse.executeScript(JS_GET_USER_AGENT_CALL);
}

private Color getBodyColor() {
LOG.debug("Getting body color.");
JavascriptExecutor jse = (JavascriptExecutor) getWebDriver();
List<String> bodyColor = (List<String>) jse.executeScript(JS_GET_BODY_COLOR_CALL);
return new Color(Integer.parseInt(bodyColor.get(0)), Integer.parseInt(bodyColor.get(1)), Integer.parseInt(bodyColor.get(2)));
}

void scrollTo(int yPosition) throws InterruptedException {
LOG.debug("Scroll to {}", yPosition);
JavascriptExecutor jse = (JavascriptExecutor) getWebDriver();
Expand Down

0 comments on commit fb2baeb

Please sign in to comment.