18

I would like to find out the URLs of the currently opened firefox tabs with a terminal command. Is there any possibility?

This way I would be able to write them into a text file and look at them lateron; and safe resources (I often have many open tabs). I know that there is an add-on for firefox, but I would be more confortable writing my own script.

Zanna
  • 72,312
aldorado
  • 545

4 Answers4

9

The currently open URLs of the opened Firefox tabs are stored in sessionstore.js file which is located somewhere in $HOME/.mozilla/firefox/XXXXXXXX.default directory.

So, you can start from something like this:

cat $HOME/.mozilla/firefox/*default/sessionstore.js | sed "s/{/\n/g" | egrep -o '"url".*"scroll"' | cut -d\" -f4

Using cat we can display that file, and with the help of sed, egrep and cut we select only the URLs of the opened Firefox tabs from that file.

Radu Rădeanu
  • 174,089
  • 51
  • 332
  • 407
5

That information is stored in $HOME/.mozilla/firefox/*default/sessionstore.js and its format is json.

The following example was made to work with PHP. It walks all firefox windows, all tabs and gets the relevant information which is the last entry inside of "entries".

If we could use xpath to parse it, it would be something like: /windows/*/tabs/last()/url (my xpath knowledge is rusty).

cat $HOME/.mozilla/firefox/*default/sessionstore.js | php -r '
$json=json_decode(fgets(STDIN), TRUE);
foreach($json["windows"] as $w)
foreach($w["tabs"] as $t)
echo end($t["entries"])["url"]."\n";'

Or, with perl (but first, sudo apt-get install libjson-pp-perl):

cat $HOME/.mozilla/firefox/*default/sessionstore.js | perl -e '
use JSON qw( decode_json );
my $json = decode_json(<STDIN>);
foreach my $w ( @{$json->{"windows"}} ) {
    foreach my $t ( @{$w->{"tabs"}} ) {
        print $t->{"entries"}[-1]->{"url"}."\n";
    }
}'
Zanna
  • 72,312
Hugo Vieira
  • 1,272
1

This requires the jq package, which can be installed with sudo apt-get install jq:

jq '.windows[].tabs[].entries[].url' ~/.mozilla/firefox/xkxwaf4z.default/sessionstore-backups/recovery.js

Check the path in ~/.mozilla/firefox and replace xkxwaf4z with the correct name.

Zanna
  • 72,312
0

This is how you can extract the current URL of the front-most/active tab in Firefox:

cat recovery.js | php -r '$json=json_decode(fgets(STDIN), TRUE); $tindex = $json["windows"][0]["selected"]-1; $eindex = $json["windows"][0]["tabs"][$tindex]["index"]-1; echo $json["windows"][0]["tabs"][$tindex]["entries"][$eindex]["url"];'

Explanation: First, it finds the index of the active tab $tindex and then the index of the active history entry in this tab $eindex.

Every 15 seconds Firefox creates a backup in *default/sessionstore-backups/recovery.js.