Take website screenshots with headless Firefox
This script takes a screenshot of a website. It does so headless with Firefox. A backup is taken before doing anything. It deletes traces left during the process.
Backup
exec 2>&1
orig=/home/ra/.mozilla/firefox
backup=/home/ra/tmp/firefox_backup
rm -rf $backup
mkdir -p $backup
cp -r $orig $backup
:
Create the test profile
which firefox >/dev/null 2>&1 && firefox --version
which firefox >/dev/null 2>&1 && firefox -CreateProfile headlessTest
Take the screenshot
exec 2>&1
start_time="$(date -u +%s)"
dt=$(date '+%Y-%m-%d-%H%M%S')
filename=/tmp/$dt-headlessTest.jpg
url=https://www.gnu.org/software/emacs/
which firefox >/dev/null 2>&1 && firefox \
-P headlessTest \
--screenshot $filename $url \
--window-size=1024,768
end_time="$(date -u +%s)"
elapsed="$(($end_time-$start_time))"
echo "Total of $elapsed seconds elapsed for screenshot: $filename"
echo "Size: $(du --block-size K $filename)"
:
Cleanup and restore
exec 2>&1
orig=/home/ra/.mozilla/firefox
backup=/home/ra/tmp/firefox
rm -rf $orig/*
cp -r $backup/* $orig
rm -rf $backup #Uncomment to delete the backup
echo "Done"
:
Fullscript
exec 2>&1
orig=/home/ra/.mozilla/firefox
backup=/home/ra/tmp/firefox_backup
rm -rf $backup
mkdir -p $backup
cp -r $orig $backup
:
which firefox >/dev/null 2>&1 && firefox --version
which firefox >/dev/null 2>&1 && firefox -CreateProfile headlessTest
exec 2>&1
start_time="$(date -u +%s)"
dt=$(date '+%Y-%m-%d-%H%M%S')
filename=/tmp/$dt-headlessTest.jpg
url=https://www.gnu.org/software/emacs/
which firefox >/dev/null 2>&1 && firefox \
-P headlessTest \
--screenshot $filename $url \
--window-size=1024,768
end_time="$(date -u +%s)"
elapsed="$(($end_time-$start_time))"
echo "Total of $elapsed seconds elapsed for screenshot: $filename"
echo "Size: $(du --block-size K $filename)"
:
exec 2>&1
orig=/home/ra/.mozilla/firefox
backup=/home/ra/tmp/firefox
rm -rf $orig/*
cp -r $backup/* $orig
rm -rf $backup #Uncomment to delete the backup
echo "Done"
:
4.0K /home/ra/.mozilla/firefox
4.0K /home/ra/tmp/firefox
*** You are running in headless mode.
Total of 7 seconds elapsed for screenshot: /tmp/2020-03-30-154448-headlessTest.jpg
Size: 104K /tmp/2020-03-30-154448-headlessTest.jpg
4.0K /home/ra/.mozilla/firefox
4.0K /home/ra/tmp/firefox