14

Is there a way to get such URL using some sort of commands?

Amit Rege
  • 143

4 Answers4

13

This answer is outdated, and although IMO it is still valuable, please pay attention to the 2022 Answer provided below by @dotancohen.


There are few files who contain information about your sessions:

  • ~/.mozilla/firefox/*.default/sessionstore-backups/recovery.js which contains information about the current session, also contains information for the closed tabs and the previous session. Every 15 seconds Firefox creates a backup in this file. This file is not available when Firefox is closed.

  • ~/.mozilla/firefox/*.default/sessionstore.js which contains information about the last session, when the Firefox browser is closed. This file is not available when Firefox is open.

  • ~/.mozilla/firefox/*.default/sessionstore-backups/previous.js which contains information about the previous session.

The analysis of the content of recovery.js shows that, for each tab, only the entries of the current URL contain string attributes.


I. When Firefox is open:

1.A. If you want to get all URLs of the open tabs from the current session you can use this command:

cat $HOME/.mozilla/firefox/*.default/sessionstore-backups/recovery.js | \
sed "s/\\_closedTabs.*//" | \
sed "s/{/\n{/g" | \
egrep -o 'url.*attributes' | \
cut -d\" -f3

* Please note that, you must copy/paste all lines together into a terminal window and press Enter.

Where:

  • cat $HOME/.mozilla/firefox/*.default/sessionstore-backups/recovery.js will print the content of this file;
  • sed "s/\\_closedTabs.*//" will remove everything after the string _closedTabs;
  • sed "s/{/\n{/g" | \ will put a newline before each {;
  • egrep -o 'url.*attributes' will filter only those parts of the lines who begin with url and end with attributes. Without -o option, the entire lines who contain the string will be filtered;
  • cut -d\" -f3 will use " as delimiter and will filter only the 3th column.

In my case the output of the command is:

https://askubuntu.com/
https://www.mozilla.org/en-US/

1.B. If you want to get the data for the current and for the previous session at once you could use this:

printf "\n# CurrentSession:\n"; \
cat $HOME/.mozilla/firefox/*.default/sessionstore-backups/recovery.js | \
sed "s/\\_closedTabs/\{\"url\":\"# ClosedTabs:\"attributes/g" | \
sed "s/\\lastSessionState/\{\"url\":\"# LastSession:\"attributes/" | \
sed "s/{/\n{/g" | \
egrep -o 'url":"*.*attributes*' | \
cut -d\" -f3 | \
sed "s/#/\n#/" \
; echo

Where:

  • printf "\n# CurrentSession:\n"; will print # CurrentSession: between two newlines;
  • sed "s/\\_closedTabs/\{\"url\":\"# ClosedTabs:\"attributes/g" will replace the string _closedTabs with "url":"# ClosedTabs:"attributes in the entire "file" (option g);
  • sed "s/\\lastSessionState/\{\"url\":\"# LastSession:\"attributes/" will replace lastSessionState with "url":"# LastSession:"attributes;
  • sed "s/#/\n#/" will put a newline before each #.
  • ; echo will add a blank line at the bottom.

In my case the output of the command is:

# CurrentSession:
https://askubuntu.com/
https://www.mozilla.org/en-US/

ClosedTabs:

https://www.yahoo.com/

LastSession:

https://askubuntu.com/ https://www.abv.bg/

ClosedTabs:

https://www.google.com/gmail/about/ https://www.yahoo.com/


2.A. If you want to get and the history, you could use:

cat $HOME/.mozilla/firefox/*.default/sessionstore-backups/recovery.js | sed "s/\\_closedTabs.*//" | sed "s/{/\n{/g" | egrep 'url":"http*' | cut -d\" -f4

In my case the output of the command is:

https://askubuntu.com/
https://www.google.bg/search?client=ubuntu&channel=fs&q=firefox&ie=utf-8&oe=utf-8&gfe_rd=cr&ei=pTKyWIitGqTs8wewj4KgDQ
https://www.mozilla.org/bg/firefox/new/
https://www.mozilla.org/en-US/

2.B. You can put a separator between the data for each tab:

cat $HOME/.mozilla/firefox/*.default/sessionstore-backups/recovery.js | \
sed "s/\\_closedTabs.*//" | \
sed "s/{/\n{/g" | \
sed "s/entries/url\":\"# TAB:/g" | \
egrep 'url":"*' | \
cut -d\" -f4 | \
sed "s/#/\n#/"

In my case the output of the command is:

# TAB:
about:startpage
https://askubuntu.com/

TAB:

https://www.google.bg/search?client=ubuntu&channel=fs&q=firefox&ie=utf-8&oe=utf-8&gfe_rd=cr&ei=pTKyWIitGqTs8wewj4KgDQ https://www.mozilla.org/bg/firefox/new/ https://www.mozilla.org/en-US/


3. 1.B. and 2.B. together:

printf "\n# CurrentSession:\n"; \
cat $HOME/.mozilla/firefox/*.default/sessionstore-backups/recovery.js | \
sed "s/\\_closedTabs/\{\"url\":\"# ClosedTabs:/g" | \
sed "s/\\lastSessionState/\{\"url\":\"# LastSession:/" | \
sed "s/entries/url\":\"# TAB:/g" | \
sed "s/{/\n{/g" | \
egrep 'url":"*' | \
cut -d\" -f4 | \
sed "s/#/\n#/" \
; echo

In my case the output of the command is:

# CurrentSession:

TAB:

https://host.bg/ https://admin.host.bg/

TAB:

https://www.mediawiki.org/wiki/MediaWiki

TAB:

https://en.wikipedia.org/wiki/Main_Page

ClosedTabs:

TAB:

about:startpage https://www.yahoo.com/

LastSession:

TAB:

about:startpage https://askubuntu.com/

ClosedTabs:

TAB:

https://www.mozilla.org/en-US/ https://www.google.com/gmail/about/


II. When Firefox is closed:

When Firefox is closed, you can get the data for the last session. The approach is the same as explained above but instead of recovery.js you have to use sessionstore.js (or previous.js):

cat $HOME/.mozilla/firefox/*.default/sessionstore.js \
...

References:

pa4080
  • 30,621
6

2022 Answer

As Bao mentions, the top-voted answer no longer works in modern Firefox because the file it depends on has two format changes:

  1. It is a compressed file now, so we use lz4jsoncat to read it.
  2. The method to identify the current tab and history item in that file is different. Now we must sort the tabs on lastAccessed, and then find the correct history item from the index field. I prefer jq for that.

This one-liner should do it:

lz4jsoncat ~/.mozilla/firefox/*.default-release/sessionstore-backups/recovery.jsonlz4 | jq -r ".windows[0].tabs | sort_by(.lastAccessed)[-1] | .entries[.index-1] | .url"

To install the prerequisite tools on Ubuntu, run this:

$ sudo aptitude install jq lz4json
dotancohen
  • 2,864
2

The accepted answer does not work now, firefox has removed that file.

So here's my solution(s) of it:

Method-1: use keyboard automation tools, such as xdotool, to post keys to copy the url into the clipboard.

This method has some drawbacks, that is, it will change the current focus of the cursor (you need to move to the address bar using Ctrl-L first)

Method-2: use Grease Monkey, bind key F12 to GM_setClipboard(document.location); use keyboard automation tools, send key F12 to firefox; watch the clipboard, save it (maybe even backup and restore the clipboard).

2

Drag/Drop

Drag it and drop it. Drag it from the icon to the left of the address field. In most browsers you may see an i for the icon.

Copy/Paste

You can also use copy and paste. Copy the text from the address field. Paste it into the terminal.

L. D. James
  • 25,444