Skip to content

Instantly share code, notes, and snippets.

@gkatev
Last active October 10, 2022 20:01
Show Gist options
  • Save gkatev/d66664872a68fab7255da33ea6fce838 to your computer and use it in GitHub Desktop.
Save gkatev/d66664872a68fab7255da33ea6fce838 to your computer and use it in GitHub Desktop.
Linux/X11 script to refresh a browser window that is rendering a local HTML file. You could append it to a build command or pair it with inotifywait.
#!/bin/bash
if [ ! "$1" ]; then
echo "Usage: html-refresh <html file>"
exit 1
fi
title="$(xmllint --html "$1" --xpath "//html/head/title/text()" \
| perl -MHTML::Entities -pe 'decode_entities($_);')"
# Switch workspace, if necessary
html_workspace=$(wmctrl -l | grep -F "$title" | head -n 1 | awk '{print $2}')
if [ ! -z "$html_workspace" ] && [ "$html_workspace" != "$(wmctrl -d | awk '{if($2=="*") print $1}')" ]; then
wmctrl -s "$html_workspace"
# Wait for the desktop to actually switch. Better way?
sleep 0.1
fi
# Send F5
# Disabled because xdotool's search interprets `name` as a regex, leading to
# trouble with special characters in the HTML's title (see xdotool issue #183)
# xdotool search --onlyvisible --name "$title" windowactivate --sync key "F5" \
# windowactivate $(xdotool getactivewindow)
# Alternative hybrid wmctrl-xdotool approach
ID=$(wmctrl -l | grep -F "$title" | head -n 1 | cut -d ' ' -f 1)
xdotool windowactivate --sync "$ID" key "F5" \
windowactivate $(xdotool getactivewindow)
([ $? -eq 0 ] && echo "Refresh: success") || echo "Refresh: fail"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment