Skip to content

Instantly share code, notes, and snippets.

@prurigro
Last active August 29, 2015 14:16
Show Gist options
  • Save prurigro/417db9e0487259507335 to your computer and use it in GitHub Desktop.
Save prurigro/417db9e0487259507335 to your computer and use it in GitHub Desktop.
Reload the webpage in Firefox when the source file changes
#!/usr/bin/env bash
#
# browser-remote-watch: reload the page when the source changes
#
# Version 1.1
#
# Written by Kevin MacMartin ([email protected])
# Released under the MIT license
#
# Uses the Firefox REMOTE CONTROL extension
# https://addons.mozilla.org/en-US/firefox/addon/remote-control/
#
# Instructions: run with one or more file or directory to refresh on change
#
# Set REMOTE_PORT to the default unless already configured
REMOTE_PORT=${REMOTE_PORT:='32000'}
# Colours
[[ -t 1 ]] && {
c_d=$'\e[1;30m' # GREY
c_r=$'\e[1;31m' # RED
c_g=$'\e[1;32m' # GREEN
c_y=$'\e[1;33m' # YELLOW
c_b=$'\e[1;34m' # BLUE
c_m=$'\e[1;35m' # MAGENTA
c_t=$'\e[1;36m' # TEAL
c_w=$'\e[1;37m' # WHITE
c_c=$'\e[0m' # DISABLES COLOUR
}
function error {
printf '%s %s\n' "${c_r}ERROR:$c_c" "$c_w$1$c_c" >&2
exit 1
}
[[ -n "$1" ]] \
|| error 'you must supply at least one file or directory to watch'
for each in "$@"; do
[[ -f "$each" || -d "$each" ]] \
|| error "$1 is not a file or directory"
done
printf '%s %s\n' "$c_b==>$c_w" "press ctrl-c to quit$c_c"
action='Starting'
while (( 1 )); do
printf '%s\n' "$c_w$action @ $(date)$c_c"
[[ "$action" = 'Starting' ]] \
&& action='Refreshed'
inotifywait -e create -e modify "$@"
printf '%s\n' 'reload' | nc localhost "$REMOTE_PORT"
printf '\n'
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment