Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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.

share|improve this question
add comment

2 Answers 2

up vote 5 down vote accepted

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.

share|improve this answer
    
Double quotes needed for the egrep pattern in zsh. Other than that, nice tip, thanks :) –  moon.musick Aug 28 '13 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 '13 at 13:31
    
@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. :) –  Radu Rădeanu Aug 28 '13 at 13:32
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 :) –  moon.musick Aug 28 '13 at 13:35
    
In the file sessionstore.js there are not only the addresses of the open tabs. There are many other addresses. –  Nicolas Barbulesco Oct 5 '13 at 9:04
show 2 more comments

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).

You can copy/paste this code to a terminal:

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";'

EDIT: 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|improve this answer
    
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 Oct 5 '13 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. –  Hugo Vieira Oct 7 '13 at 13:37
    
I mean this. –  Nicolas Barbulesco Oct 8 '13 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... –  Hugo Vieira Oct 8 '13 at 13:08
    
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. –  Nicolas Barbulesco Oct 8 '13 at 13:24
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.