Get the currently open tabs in Google Chrome via the command line

Solution 1:

This question was not answered, solution above is for Chrome Dev Tools where the OP asked in CLI, so this is the solution I've found :

strings ~/.config/google-chrome/Default/Current\ Session | grep -E '^https?://'

Solution 2:

For a macOS only solution, you could also use the open-source utility chrome-cli. You can install with Homebrew:

brew install chrome-cli

Also, take note of one thing from the README:

To execute javascript you must first allow it under View > Developer > Allow JavaScript from Apple Events.

Here's what it looks like:

$ chrome-cli list tabs
[1889] Edit - Super User
[1918] prasmussen/chrome-cli: Control Google Chrome from the command line
[1905] Yotto Winter Mix 19 by Yotto
$ chrome-cli list links
[1889] https://superuser.com/posts/1407956/edit
[1918] https://github.com/prasmussen/chrome-cli
[1905] https://soundcloud.com/yotto/yotto-winter-mix-2019

Solution 3:

I wish I knew about chrome-cli...

This is also a OSX only solution, i use applescript:

function chrome_json_summary() {
local wincount=$(osascript -e 'tell application "Google Chrome" to get number of windows')
[ "$wincount" -eq 0 ] && echo "zero windows!" && return

local json="["
for (( i=1; i<=$wincount; i++)); do
    json="$json""["
    local cmd="osascript -e 'tell application \"Google Chrome\" to get number of tabs in window $i'"
    local tabcount=$(eval $cmd)
    for (( j=1; j<=$tabcount; j++)); do
        local cmd="osascript -e 'tell application \"Google Chrome\" to get URL of tab $j of window $i'"
        local url=$(eval $cmd)
        [ $j -eq $tabcount ] && json="$json\"$url\"" || json="$json\"$url\","
   done
   [ $i -eq $wincount ] && json="$json]" || json="$json],"
done
echo "$json]"
}

This bash function uses applescript to iterate over all chrome windows and each tab in each window to build a array of arrays json.

I store the json in a local file. i use this to backup my chrome state. this script would be faster if i did the json generation all in applescript.

I have a separate function that loads a json file into chrome.

the advantage of this over chrome-cli would be that applescript (plus basic bash scripting) will pretty much not break with osx upgrades.

Solution 4:

I have written a tool to extract data from chrome session files for precisely this purpose. https://github.com/lemnos/chrome-session-dump. Running it like so chrome-session-dump will produce a list of tabs (in order) which can subsequently be passed to firefox. E.G chrome-session-dump|xargs firefox. You can also obtain the currently open tab via -active for processing by external scripts.