30

I'd like to write a shell script that automatically transfers all tabs from Chrome to Firefox (by getting the list of tabs from Chrome, and then opening them in Firefox with a script.) In Google Chrome, is it possible to obtain a list of open tabs via the command line?

1
12

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?://'
1
  • 5
    This answer seems not work after Chrome v87. There is no Current\ Session anymore.
    – roachsinai
    Nov 20 '20 at 4:08
8

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
2

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.

2

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.

1

I finally found a duplicate question (it took a lot of searching to find it, though): https://stackoverflow.com/questions/4968271/chrome-on-linux-query-the-browser-to-see-what-tabs-are-open

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.