Firefox – reading out urls of opened tabs from the command-line

I sometimes have quite a big range of tabs open in Firefox and I prefer it to save them to a file, rather then using the build-in bookmarks.

Therefore I (manually) copy the urls from the about:preferences page, save them to a file and process the file with: tr '|' 'n' in a little bash script.

Later when I want to reopen the tabs from the textfile I run this little loop:

#!/bin/bash

# usage: $bash Open-tabs.sh file-with-bookmarks.txt

 while read -r line; do
     firefox -new-tab "$line" 2>/dev/null &
     sleep 2
 done < "$1"

and it opens all tabs with a delay of 2 seconds.

I would like to know if there is a way, I can read-out the urls of the opened tabs from the command line, so I could include it to my script?

Asked By: nath

||

Source(Changed file path) : Get all the open tabs

This snippet gets the current firefox tab url’s. It uses the
recovery.js[onlz4] file in your profile folder. That file is updated
almost instantly, however it will not always be the correct url.

Get all the open tabs:

python -c '
import io, json, pathlib as p
fpath = next(iter(p.Path("~/.mozilla/firefox").expanduser().glob("*.default/sessionstore-backups/recovery.js*")))
with io.open(fpath, "rb") as fd:
    if fpath.suffix == ".jsonlz4":
        import lz4.block as lz4
        fd.read(8)  # b"mozLz40"
        jdata = json.loads(lz4.decompress(fd.read()).decode("utf-8"))
    else:
        jdata = json.load(fd)
    for win in jdata.get("windows"):
        for tab in win.get("tabs"):
            i = tab["index"] - 1
            print(tab["entries"][i]["url"])
'
Answered By: Hunter.S.Thompson

this works for Firefox 57+. You’ll need lz4 (via pip). The file header is gathered from the length of b'mozLz40'. Use an environment variable for the filepath if you want to use it in a oneliner, replace with n and t accordingly and merge lines.

export opentabs=$(find ~/.mozilla/firefox*/*.default/sessionstore-backups/recovery.jsonlz4);

python3 <<< $'import os, json, lz4.block
f = open(os.environ["opentabs"], "rb")
magic = f.read(8)
jdata = json.loads(lz4.block.decompress(f.read()).decode("utf-8"))
f.close()
for win in jdata["windows"]:
    for tab in win["tabs"]:
        i = int(tab["index"]) - 1
        urls = tab["entries"][i]["url"]
        print(urls)'
Answered By: wbob

I recommend using https://github.com/balta2ar/brotab for this purpose:

pip install brotab
brotab install

Install the web extension as well: https://addons.mozilla.org/en-US/firefox/addon/brotab/

Restart Firefox, and you can use brotab list and parse it as so:

bt list | awk -F't' '{
    print "Downloading "$2
    system("curl --silent --output ""$2"" ""$3""")
}'
Answered By: Doron Behar

Some of these answers reference the “[random chars].default” directory. Starting with version 67, users can have profiles for different update channels (e.g., release, beta, nightly, etc.).

On my Ubuntu 18 system, this directory was “[random chars].default-release”. I still had a “[…].default” directory but it was mostly empty. Keep that in mind if you get an error that “sessionstore-backups” can’t be found.

Answered By: LousyG
Categories: Answers Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.