From a34a84a4879f9c1e38a980e4a7c1a0da83750217 Mon Sep 17 00:00:00 2001 From: mhalle Date: Mon, 5 Feb 2024 17:35:08 -0500 Subject: [PATCH] set initlal viewport for interactive shots (#128) * set initlal viewport for interactive shots --- shot_scraper/cli.py | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/shot_scraper/cli.py b/shot_scraper/cli.py index 23603cb..936b505 100644 --- a/shot_scraper/cli.py +++ b/shot_scraper/cli.py @@ -349,6 +349,8 @@ def shot( if interactive or devtools: use_existing_page = True page = context.new_page() + if width or height: + page.set_viewport_size(_get_viewport(width, height)) page.goto(url) context = page click.echo( @@ -1051,6 +1053,14 @@ def _check_and_absolutize(filepath): # On Windows, instantiating a Path object on `http://` or `https://` will raise an exception return False +def _get_viewport(width, height): + if width or height: + return { + "width": width or 1280, + "height": height or 720, + } + else: + return {} def take_shot( context_or_page, @@ -1125,16 +1135,11 @@ def on_response(response): if log_console: page.on("console", console_log) - viewport = {} - full_page = True - if shot.get("width") or shot.get("height"): - viewport = { - "width": shot.get("width") or 1280, - "height": shot.get("height") or 720, - } + viewport = _get_viewport(shot.get("width"), shot.get("height")) + if viewport: page.set_viewport_size(viewport) - if shot.get("height"): - full_page = False + + full_page = not shot.get("height") if not use_existing_page: # Load page and check for errors