Output URL of open firefox tabs in terminal

9,445

Solution 1

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.

Solution 2

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";
    }
}'
Share:
9,445

Related videos on Youtube

aldorado
Author by

aldorado

Updated on September 18, 2022

Comments

  • aldorado
    aldorado over 1 year

    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.

  • moon.musick
    moon.musick almost 11 years
    Double quotes needed for the egrep pattern in zsh. Other than that, nice tip, thanks :)
  • aldorado
    aldorado almost 11 years
    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 \"?
  • Radu Rădeanu
    Radu Rădeanu almost 11 years
    @moon.musick Or you can use bash -c 'cat $HOME/.mozilla/firefox/*default/sessionstore.js | sed "s/\"/\n/g" | egrep http[s]*:' and you will have no problem in any *sh. :)
  • Nicolas Barbulesco
    Nicolas Barbulesco over 10 years
    In the file sessionstore.js there are not only the addresses of the open tabs. There are many other addresses.
  • Nicolas Barbulesco
    Nicolas Barbulesco over 10 years
    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.
  • Nicolas Barbulesco
    Nicolas Barbulesco over 10 years
    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.
  • Radu Rădeanu
    Radu Rădeanu over 10 years
    @NicolasBarbulesco Then try this answer.
  • Hugo Vieira
    Hugo Vieira over 10 years
    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.
  • Hugo Vieira
    Hugo Vieira over 10 years
    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...
  • Nicolas Barbulesco
    Nicolas Barbulesco over 10 years
    The bugs with the file sessionstore.js — there are several of them — show themselves when one relaunches Firefox. But they occur sooner : when Firefox writes this file. I have seen such bug occur during normal use of Firefox.
  • damien
    damien about 9 years
    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.
  • DJCrashdummy
    DJCrashdummy over 8 years
    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...!