From a19996ff74ae0ea7bda79667c53f76ffaf7f02ea Mon Sep 17 00:00:00 2001 From: "suzanne.soy" Date: Sat, 10 Oct 2020 01:38:18 +0100 Subject: [PATCH] take full-length screenshots of pages using firefox --- .gitignore | 1 + screenshot-full-firefox.sh | 29 +++++++++++++++++++++++++++ tests.sh | 11 ++++++++++ tests/test-screenshot-full-firefox.sh | 7 +++++++ 4 files changed, 48 insertions(+) create mode 100644 .gitignore create mode 100755 screenshot-full-firefox.sh create mode 100755 tests.sh create mode 100755 tests/test-screenshot-full-firefox.sh diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..317fef4 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/tests-results/ diff --git a/screenshot-full-firefox.sh b/screenshot-full-firefox.sh new file mode 100755 index 0000000..1f66db3 --- /dev/null +++ b/screenshot-full-firefox.sh @@ -0,0 +1,29 @@ +#!/usr/bin/env nix-shell +#!nix-shell --pure -i bash -p firefox-esr bash -I nixpkgs=https://github.com/NixOS/nixpkgs/archive/fea633695211167753ee732c9087808d0c57461c.tar.gz + +set -euET -o pipefail + +usage() { + printf "Usage: ./screenshot-full-firefox.sh url filename"\\n +} + +if test $# -ne 2; then + usage + exit 1 +elif test $# -eq 1 && (test "x$1" = "x-h" || test "x$1" = "x--help"); then + usage + exit 0 +fi + +url="$1" +image="$2" + +temp_profile_home="$(mktemp -d)" + +function cleanup() { + rm -fr --one-file-system "$temp_profile_home" +} + +trap cleanup EXIT + +HOME="$temp_profile_home" firefox --headless --window-size 1920 --screenshot "$image" "$url" diff --git a/tests.sh b/tests.sh new file mode 100755 index 0000000..64cc1ab --- /dev/null +++ b/tests.sh @@ -0,0 +1,11 @@ +#!/usr/bin/env nix-shell +#!nix-shell -i bash -p bash nix -I nixpkgs=https://github.com/NixOS/nixpkgs/archive/fea633695211167753ee732c9087808d0c57461c.tar.gz + +# Older nix-shell --pure versions prevent nested nix-shells to properly download dependencies, so the above doesn't use --pure. +# If this script increases in complexity and becomes more sensitive to env vars, use --pure and wrap it with an impure launcher. + +set -euET -o pipefail + +for i in tests/*.sh; do + "$i" +done diff --git a/tests/test-screenshot-full-firefox.sh b/tests/test-screenshot-full-firefox.sh new file mode 100755 index 0000000..5b497c5 --- /dev/null +++ b/tests/test-screenshot-full-firefox.sh @@ -0,0 +1,7 @@ +#!/usr/bin/env nix-shell +#!nix-shell --pure -i bash -p bash nix -I nixpkgs=https://github.com/NixOS/nixpkgs/archive/fea633695211167753ee732c9087808d0c57461c.tar.gz + +set -euET -o pipefail + +cd "$(dirname "$0")" +../screenshot-full-firefox.sh https://example.com/ ../tests-results/screenshots-full-firefox.png