17

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.

4 Answers 4

8

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.

7
  • Double quotes needed for the egrep pattern in zsh. Other than that, nice tip, thanks :) Aug 28, 2013 at 13:21
  • 1
    Would you like to describe in a few words how this command works? As I get it it is like: get content of the sessionstore.js, pipe to an operation to replace \" with \n and pipe to an operation to get all lines that begin with http? Is that right? What is the use of replacing \"?
    – aldorado
    Aug 28, 2013 at 13:31
  • 1
    @aldorado it displays the contents of sessionstore.js file, then replaces occurences of " with \n (a newline) and then filters out only lines containing http or https. Try displaying the file and you'll know why it needs newlines :) Aug 28, 2013 at 13:35
  • 2
    In the file sessionstore.js there are not only the addresses of the open tabs. There are many other addresses. Oct 5, 2013 at 9:04
  • 1
    This answer uses the file sessionstore.js of Firefox. The problem is that is that this file notoriously loses tabs. What I want here is a parallel solution, asking Firefox what his tabs are. Like in AppleScript. Oct 5, 2013 at 9:09
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";
    }
}'
6
  • This answer uses the file sessionstore.js of Firefox. The problem is that is that this file notoriously loses tabs. What I want here is a parallel solution, asking Firefox what his tabs are. Like in AppleScript. Oct 5, 2013 at 9:11
  • What do you mean by "loses tabs"? I'm currently running Firefox with 5 tabs and I see these 5 tabs when running these scripts. This solution walks all windows and all tabs of each window. Oct 7, 2013 at 13:37
  • I mean this. Oct 8, 2013 at 12:39
  • The question posted by aldorado: (...) find out the URLs of the currently opened firefox tabs (...). You are talking about listing tabs that were saved after shutting down firefox. Also, that bug does not seem that notorious... Oct 8, 2013 at 13:08
  • 1
    i've tested both methods for the recovery.js-file with 9 windows and 840 tabs and they worked like a charm! -- just after piping the output into a text file i had to set the character map to ISO-8859-15 for the file of the perl-method...! Nov 17, 2015 at 21:43
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.

2
  • 1
    php5-cli needs to be installed for this to work could you add that to your answer to improve. Is there a way to grab all of the tabs urls not just the front most? like the OP asks.
    – damien
    Mar 22, 2015 at 9:05
  • I'm aware of the mismatch of the question and my answer, however Google will direct you here if you seek the information in my answer.
    – Lenar Hoyt
    Mar 22, 2015 at 10:43
0

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.

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.