Skip to content

Instantly share code, notes, and snippets.

@martinvirtel
Last active November 23, 2016 07:01
Show Gist options
  • Save martinvirtel/ebc47db5fc5e7fee04777e7f20a9077b to your computer and use it in GitHub Desktop.
Save martinvirtel/ebc47db5fc5e7fee04777e7f20a9077b to your computer and use it in GitHub Desktop.
Chrome Refresh on File Change
#!/bin/bash
#
# Watches the folder or files passed as arguments to the script and when it
# detects a change it automatically refreshes Chrome tab or
# window.
#
# Inspired by: http://razius.com/articles/auto-refreshing-google-chrome-on-file-changes/
#
# Usage:
# ./chrome-refresh.sh /folder/to/watch /some/folder/file_to_watch.html
TIME_FORMAT='%F %H:%M'
OUTPUT_FORMAT='%T Event(s): %e fired for file: %w. Refreshing.'
echo Please click on window to refresh!
CHROME_WINDOW_ID=$(xdotool selectwindow)
echo Refreshed window is "$(xdotool getwindowname $CHROME_WINDOW_ID)" ID $CHROME_WINDOW_ID
if [ "$@" == "" ] ; then
watch=.
else
watch="$@"
fi
echo Watching files: $watch for MODIFY. Hit CTRL+C to exit.
while inotifywait -e MODIFY -q -r --timefmt "${TIME_FORMAT}" --format "${OUTPUT_FORMAT}" "$watch"; do
CURRENT_WINDOW_ID=$(xdotool getactivewindow)
xdotool windowactivate $CHROME_WINDOW_ID
xdotool key --window $CHROME_WINDOW_ID F5
xdotool windowactivate $CURRENT_WINDOW_ID
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment