31

I'm trying to set up a custom toolchain where the browser (Firefox or, preferrably, Chrome) is often/frequently/constantly forced to refresh from the commandline.

(The idea is to instantly see the visual changes in the html/webapp I'm editing in an adjacent Emacs frame - without having to constantly tab to the browser to do a manual refresh.)

The closest I've come so far is to run google-chrome FILE_PATH. However this opens a new tab every time.

Are there other approaches?

3
17

Something to play with

It uses xdotool, which lets you script windows/desktop actions. If you supply the name of the browser as an argument, it'll find and reload the current page. You can set a default browser, so you don't need to supply one each time, and you can change whether you send a CTRL-R to reload, or SHIFT-CTRL-R to reload without cache.

It should flip to your browser, reload the page, then flip back to whatever window you called this from. I use this often by putting browser in background, with editor window set to 'ON-TOP' so it's always visible, hot-key this script, or call it from your editor, and it'll return your focus when it's done.

I'm a vim user, and I could see making an autocommand to automatically trigger this script whenever a given file was written, so the browser would refresh when appropriate, I know you can do the same.

#!/bin/bash
#
# L Nix <lornix@lornix.com>
# reload browser window
#
# whether to use SHIFT+CTRL+R to force reload without cache
RELOAD_KEYS="CTRL+R"
#RELOAD_KEYS="SHIFT+CTRL+R"
#
# set to whatever's given as argument
BROWSER=$1
#
# if was empty, default set to name of browser, firefox/chrome/opera/etc..
if [ -z "${BROWSER}" ]; then
    BROWSER=firefox
fi
#
# get which window is active right now
MYWINDOW=$(xdotool getactivewindow)
#
# bring up the browser
xdotool search --name ${BROWSER} windowactivate --sync
# send the page-reload keys (C-R) or (S-C-R)
xdotool search --name ${BROWSER} key --clearmodifiers ${RELOAD_KEYS}
#
# sometimes the focus doesn't work, so follow up with activate
xdotool windowfocus --sync ${MYWINDOW}
xdotool windowactivate --sync ${MYWINDOW}
#
3
  • +1 Cool stuff! Note that --name parameter matches against the string that is displayed in the window titlebar so it might not always contain the name of the browser as in this here example. – rebus Dec 27 '12 at 1:31
  • @rebus what about --class or --classname? – Aquarius Power Jun 20 '14 at 19:09
  • I am having following error: Warning: XTEST extension unavailable on '(null)'. Some functionality may be disabled; See 'man xdotool' for more info. – alper Jun 26 '20 at 17:53
8

@lornix's answer didn't quite work for me (I'm not sure why, possibly my window manager doesn't let xdotool switch windows). I wrote a modified version which should be more robust, it:

  1. Doesn't do the unnecessary switching between windows
  2. Limits itself to visible windows
  3. Refreshes all of the visible windows

(Also it uses F5 instead of Ctrl-R, but that's just because I have Ctrl-R bound to something else.)

#!/bin/bash

set -o errexit
set -o nounset

keystroke="CTRL+F5"

# set to whatever's given as argument, defaults to firefox
BROWSER="${1:-firefox}"

# find all visible browser windows
browser_windows="$(xdotool search --sync --all --onlyvisible --name ${BROWSER})"

# Send keystroke
for bw in $browser_windows; do
    xdotool key --window "$bw" "$keystroke"
done
2
  • I should add this doesn't seem to work with chromium, and I have absolutely no idea why. – dshepherd Jan 12 '16 at 10:21
  • 1
    I love seeing a well written bash script! By the way, chromium works if you use windowfocus before key. Of course, you then have to restore the previous focus. – hackerb9 Nov 11 '19 at 2:00
6

If you are using Firefox, Auto Reload is an extension where it reloads whenever file changes. You can add files, directories or localhost and edit regex based filters so that you will achieve what you want. I don't know any complementary to this in Chrome. link: https://addons.mozilla.org/en-US/firefox/addon/auto-reload/

1
  • 1
    Doesn't work any more, sadly. – Gringo Suave Aug 9 '18 at 6:52
2

For OS X you can use this bit of applescript:

activate application "Firefox"
tell application "System Events" to keystroke "r" using command down
1

None of these quite worked for me, so here's my own version:

SHOULD_SWITCH_BACK=$1 # y/Y means it will switch back to current window  
CURRENT_WINDOW=$(xdotool getactivewindow)  
KEY="CTRL+F5"  
BROWSER="Firefox"  
  
xdotool search --desktop 0 ${BROWSER} windowactivate  
xdotool key ${KEY}  
if [[ $SHOULD_SWITCH_BACK == 'y' || $SHOULD_SWITCH_BACK == 'Y' ]]; then  
    xdotool windowfocus --sync ${CURRENT_WINDOW}  
    xdotool windowactivate --sync ${CURRENT_WINDOW}  
fi  

I don't know what --desktop 0 means. I'm not familiar with xdotool, but I saw the --desktop line on https://askubuntu.com/questions/21262/shell-command-to-bring-a-program-window-in-front-of-another and it worked for me.

I originally posted this on my website at https://www.taeluf.com/blog/bash/reload-browser-tab/

0

The add-on Live Reload by Blaise Kal polls source files for changes in the background and reloads the whole browser tab when changes have been detected. It does not need to be triggered externally and is available for Chrome and Firefox. With this approach it is compatible with many web frameworks and requires few configuration. It may spam your server log, though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.