In Firefox - across all tabs: How do I list all open URL's of all open browser tabs across all open browsers, using code external to the browser?

---

If I open multiple tabs, or additional browsers with tabs and then I shutdown my machine, or it crashes or I kill all processes (.exe's), the next time I launch the browser, all tabs will be re-opened in one browser window, or multiple browser windows, as it existed before shutting down.

Where is this information stored? specifically, where are the URL's stored so that this is possible?

1) I need to programatically get access to all URL's currently open in all tabs in all running browsers for a given profile when the browsers/tabs are up and running

2) or not: if they are all shut down (the data is stored somewhere in the profile directory in some file or DB, so that the next launch will open all URL's)

---

I would like to get access to all open or stored URL's through bash (cygwin), Python, Java or Rust, some language running on the machine accessing files in the profile directory (code running external to the browser).

2

There are 2 answers

0
Dexter On

If I open multiple tabs, or additional browsers with tabs and then I shutdown my machine, or it crashes or I kill all processes (.exe's), the next time I launch the browser, all tabs will be re-opened in one browser window, or multiple browser windows, as it existed before shutting down.

Where is this information stored? specifically, where are the URL's stored so that this is possible?

The session restoration is performed by the sessionstore component. An high level overview of the startup restoration process is described here:

/**
 * Session Storage and Restoration
 *
 * Overview
 * This service reads user's session file at startup, and makes a determination
 * as to whether the session should be restored. It will restore the session
 * under the circumstances described below.  If the auto-start Private Browsing
 * mode is active, however, the session is never restored.
 *
 * Crash Detection
 * The CrashMonitor is used to check if the final session state was successfully
 * written at shutdown of the last session. If we did not reach
 * 'sessionstore-final-state-write-complete', then it's assumed that the browser
 * has previously crashed and we should restore the session.
 *
 * Forced Restarts
 * In the event that a restart is required due to application update or extension
 * installation, set the browser.sessionstore.resume_session_once pref to true,
 * and the session will be restored the next time the browser starts.
 *
 * Always Resume
 * This service will always resume the session if the integer pref
 * browser.startup.page is set to 3.
 */

As we can see, it mentions the SessionFile. Looking for that file, takes us at this definition, which suggests session information is stored in the sessionstore.jsonlz4 file, which is a utf-8 encoded JSON file, compressed with lz4.

Unfortunately, the lz4 compression used does not work with standard tools, but there's some good discussion on how to work around that on Superuser (e.g. skip the first 8 bytes of the file).

Another possibility is to create a WebExtension and perform Native messaging with your executable. WebExtensions can enumerate open windows and tabs and get their URLs, if given the appropriate permissions.

0
mivk On

If Firefox is open with the tabs you want to save, they are stored in the file
sessionstore-backups/recovery.jsonlz4 (in your profile).

(.jsonlz4 is a Mozilla-specific format for storing compressed JSON data. There are various ways and tools to decompress it to get plain JSON. See for example the answers to this question.)

If Firefox has been closed, the previous session is stored in the file
sessionstore.json. (this one is not compressed)

If you have none of these 2 files (after a crash or some other reason?), you might have
sessionstore-backups/previous.jsonlz4 or
sessionstore-backups/recovery.baklz4.

The example below, in Ubuntu Linux, uses lz4jsoncat to decompress the file into normal JSON, and jq to parse it and list the title and URL of each tab currently open in any of the open Firefox windows.

profile=$HOME/.mozilla/firefox/Your_Profile   # <-- adapt to the path to your profile

lz4jsoncat $profile/sessionstore-backups/recovery.jsonlz4 \
| jq -r '.windows[] | .tabs[] | (.index - 1) as $i | .entries[$i] | .title, .url, ""'

In Debian or Ubuntu, the required packages can be installed with :

sudo apt install lz4json jq