Skip to content

Instantly share code, notes, and snippets.

@felixgolcher
Created June 24, 2020 13:12
Show Gist options
  • Save felixgolcher/7127c798671315f5dfd3f1b7f3f8b61d to your computer and use it in GitHub Desktop.
Save felixgolcher/7127c798671315f5dfd3f1b7f3f8b61d to your computer and use it in GitHub Desktop.
reload.sh file.html titlebit; reloads file.html in a browser window when changed. Tested only on xfce/manjaro.
#!/bin/bash -x
# Wed 24 Jun 2020 02:39:13 PM CEST
# felix.golcher@hu-berlin.de
# heavily based on
# https://unix.stackexchange.com/questions/37258/refresh-reload-active-browser-tab-from-command-line
# L Nix <lornix@lornix.com>
# reload browser window
# useage: reload.sh file titlebit
# file: the file loaded in the browser
# titlebit: Some unique part of the browser window title
FILETOWATCH=$1
# whether to use SHIFT+CTRL+R to force reload without cache
RELOAD_KEYS="F5"
# RELOAD_KEYS="CTRL+R"
# RELOAD_KEYS="SHIFT+CTRL+R"
#
# set to whatever's given as argument
NAMEBIT=$2
#
# if was empty, default set to name of browser, firefox/chrome/opera/etc..
if [ -z "${NAMEBIT}" ]; then
NAMEBIT=Mozilla
fi
#
echo $NAMEBIT
while inotifywait -e close_write $FILETOWATCH;
do sleep 1
# get which window is active right now
MYWINDOW=$(xdotool getactivewindow)
#
# bring up the browser
xdotool search --name ${NAMEBIT} windowactivate --sync
# send the page-reload keys (C-R) or (S-C-R)
xdotool search --name ${NAMEBIT} key --clearmodifiers ${RELOAD_KEYS}
#
# sometimes the focus doesn't work, so follow up with activate
xdotool windowfocus --sync ${MYWINDOW}
xdotool windowactivate --sync ${MYWINDOW}
date +%Y-%m-%d_%H%M
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment