Is there a way to activate a particular tab of chrome via bash?

7,887

Solution 1

Yes. I currently do it like this:

1) you'll need to install jq to mess around with tab data you'll get fed when invoking the script of currently open tabs in chromium. also you'll need to install chrome-remote-interface to do the work of activating the tabs from background -> foreground of chromium.


2) chromium needs to be running with remote debugging turned on. close all instances of chromium, and run this command instead of just chromium in terminal otherwise this process will not work:

/usr/bin/chromium --remote-debugging-port=9222 &


3) use this base script as an example and save it as act, chmod +x it and place it in your user's bin directory. I use bash script for example, it can be ported over if you're a zsh user:

#!/usr/bin/env bash

TABS_JSON=$(chrome-remote-interface list | sed -e "s/^'//" -e "s/'$//" | jq -r 'map(select(.type == "page") | {id: .id, title: .title})')

if [[ -z $@ ]]; then
    TAB_NAMES=$(echo "$TABS_JSON" | jq -r 'map(.title) | .[]')

    echo "$TAB_NAMES"
else
    TAB=$*

    TAB_ID=$(echo "$TABS_JSON" | jq -r "map(select(.title | match(\"${TAB}\";\"i\")) | .id) | .[]")

        chrome-remote-interface activate "$TAB_ID" >/dev/null

    #you might need wmctrl if window does not activate. 
    #wmctrl -a chromium
fi

navigate to cnn.com and a couple of other tabs and keep cnn.com in the background, switch to terminal and try out the command like so:

act cnn

It should activate chromium window and switch to cnn tab. You are free to further integrate this with your rofi menu or fzf variant in your workflow, and even throw in the list of open tabs into the global pool of active windows.

Solution 2

The following script works for me, but it requires that you only have one Chrome window, and that window to be active. It should be easy to tweak it or improve it.

WINID=$(xdotool search --name 'Google Chrome' | head -n 1)
WINID_HEX=$(printf "0x%x" $WINID)

while true
do
        xwininfo -id $WINID_HEX | grep Gmail
        if [ "$?" -ne 0 ]
        then
                xdotool key --window $WINID ctrl+Tab
        else
                break
        fi
        sleep 2
done

There are probably many other ways to do this:

  • Using Chromium and modify its source code
  • Writing an extension for Chrome that would switch to the right tab when a certain condition is met
  • Using tools like xmacro
  • Etc.
Share:
7,887

Related videos on Youtube

Gilles 'SO- stop being evil'
Author by

Gilles 'SO- stop being evil'

Updated on September 18, 2022

Comments

  • Gilles 'SO- stop being evil'
    Gilles 'SO- stop being evil' almost 2 years

    Suppose, for example, I have a chrome window active with gmail opened somewhere among its tabs (but not necessarily the active tab).

    Question: Is there a way to activate the gmail tab from the command line?

  • meuh
    meuh over 8 years
    Note: xwininfo will accept a decimal $WINID.
  • Gonki
    Gonki almost 5 years
    The solution needs code to prevent infinite loop when Gmail tab is not open.