How to reload Google Chrome tab from terminal?

2020-05-20 09:38发布

问题:

Is there a way to reload a Google Chrome tab in Ubuntu using just the terminal. I don't want to just open a new window, but to actually refresh a tab!

Extra question: Is this possible in other browsers as well such as Opera, Safari, Firefox.

回答1:

sudo apt-get update

sudo apt-get install xdotool

DISPLAY=:0 xdotool key F5

That's all you need.



回答2:

Looks like user2974830's answer is using some incorrect (perhaps old) syntax for xdotool. --search is supposed to be just search (without the dashes) and --windowid is supposed to be --window

That being said here's a more comprehensive solution that I found here. The link uses inotifywait, but I prefer entr which wraps inotifywait.

echo "$(date --rfc-3339=seconds) Refresh: $FILE"
CUR_WID=$(xdotool getwindowfocus)

#gets the first $BROWSER window, if you have more than one
#$BROWSER window open, it might not refresh the right one,
#as an alternative you can search by the window/html title
WID=$(xdotool search --onlyvisible --class $BROWSER|head -1)
#TITLE="window/html file title"
#WID=$(xdotool search --title "$TITLE"|head -1)
xdotool windowactivate $WID
xdotool key 'ctrl+r'
xdotool windowactivate $CUR_WID

I saved it to a file called reload-chrome-nix and I run find . -type f | entr ./reload-chrome-nix



回答3:

One liner which will focus the Chrome window and then reload the browser by sending Control+r:

xdotool search --onlyvisible --class Chrome windowfocus key ctrl+r


回答4:

Probably this could be a (the easiest) solution for your problem

xdotool key --windowid "$(xdotool --search --class Chrome | head -n 1)" F5

https://unix.stackexchange.com/questions/87831/how-to-send-keystrokes-f5-from-terminal-to-a-process

Edit: This should work for other browsers too without problems



回答5:

If some of these aren't working out for you, I was looking for a similar thing for vivaldi, and managed to do it combining some things here and others in another post

xdotool windowactivate $(xdotool search --class vivaldi | tail -1) key ctrl+r windowactivate $(xdotool getactivewindow)

you can add other key - keyvalue combinations after the first one



回答6:

Solution: chromix-too

Chromix-Too is a tool to send many types of commands to an existing Chrome session via command line, include tab reloading, given a tab identifier. As described here it is composed of three components: an extension for Google Chrome, a server and the chromix-too utility.

How to install chromix-too

  1. Install the extension on your Chrome browser.

  2. If not already installed, install the npm package manager. A general way to install it on Unix systems is by executing (with root permissions)

    curl -L https://www.npmjs.com/install.sh | sh
    

    The npm tool can usually be installed using other well-known package managers, for example in Ubuntu you should be able to do

    sudo apt-get install npm
    
  3. Install the chromix-toopackage via npm

    sudo npm install -g chromix-too
    

How to reload a tab from terminal

  1. First of all, start the server:

    chromix-too-server
    
  2. Now, using the chromix-too tool you can list all tabs with the corresponding identifiers in order to be able select the right tab.

    chromix-too ls
    

    The result will be something like

    35 The foo bar website
    136 https://another.website.foo/
    142 https://another.one.bar/
    

    As you can see, the first column shows a unique numeric identifier and the second column shows the tab title.

  3. Now suppose you want to reload the second tab in the list, that is the one with identifier 136. You can do this by typing

    chromix-too reload 136
    

Alternative tab selection

If you cannot use the numeric identifier to select a tab, there are several other ways. For example you can select tabs based on their titles or you can select pinned or unpinned tabs. See this link for help about tab selection and about the usage of Chromix-Too in general. As you can see in the tutorial, it is even possible to force a total reload without using the browser cache.

Benefits of this approach with respect to other approaches

The main benefit of using this approach with respect to other solutions shown in the other answers (including my previous answer( is that you can select and reload tabs in a very precise way (for example by using unique IDs) and without having to send keystrokes to the GUI and to focus windows, which may be uncomfortable. For example, if you are using your computer to type some text, the other scripts shown in the previous answers would change the focus and you may end up typing on browser windows accidentally. This can be avoided using this solution.



回答7:

Important note: a probably better answer has been posted after this one, please also see that answer, based on the Chromix-Too tool, and use the one you prefer for your use case.

This answer starts from the solution kindly provided by blackloop, but it can work with multiple windows and let you select the right tab in the right window, by using string comparison for tab titles and by using the capability of some browsers to select a specific tab by sending keystrokes.

The code has two parameters: the tab position in the window (a number from 1 to 8) and a substring of the tab title, to identify the correct tab.

The code below is based on Google Chrome, see comment on some code lines to learn how to change it for the Firefox case (or you can modify it to take the browser name in input).

Save this code in a file, say for example tab_refresh.sh
(Note: this code extends this code as in the answer by blockloop)

#!/bin/bash

BROWSER=google-chrome # Or BROWSER=firefox for the Firefox case
TABNUM=$1
TABTITLE=$2

CUR_WID=$(xdotool getwindowfocus)

for WID in $(xdotool search --onlyvisible --class $BROWSER)
do
  xdotool windowactivate $WID
  xdotool key "ctrl+$TABNUM"
  # Or "alt+$TABNUM" for the Firefox case
  WIN_TITLE=$(xdotool getwindowname $WID)
  if [[ $WIN_TITLE = *"$TABTITLE"* ]]
  then
    xdotool key 'F5'
  fi
done   
xdotool windowactivate $CUR_WID

After creating the file, make it executable, for example by typing:

chmod +x tab_refresh.sh

Finally, to use this script, you have to type something like:

./tab_refresh.sh TABNUM TABTITLE

For example, say you want to update the 4th tab in a Google Chrome window where its 4th tab title contains the string foo.

./tab_refresh.sh 4 foo


回答8:

chrome-remote-reload requires remote debugging enabled, but works both on Linux and Windows.

  1. Download executable: https://github.com/Benoth/chrome-remote-reload/releases or build it from sources.
  2. Run Chrome(/Chromium/Iron... or possibly even Opera or Edge, but the last one uses --devtools-server-port instead) with --remote-debugging-port=9222 parameter to enable remote debugging (just remember to close all instances of browser beforehand).
  3. Run downloaded executable.