In Mozilla Firefox, how do I extract the number of currently opened tabs to save elsewhere?

Open the about:telemetry link in Firefox and click scalars tab from the sidebar menu. Alternatively, opening about:telemetry#scalars-tab will take you directly to the scalars tab.

The browser.engagement.max_concurrent_tab_count key will show the number of tabs active for the session, but does not update when a tab is closed. Instead, if you want to update this value you will need to restart your browser.

The browser.engagement.tab_open_event_count key shows the current number of open tabs at a given time and is updated dynamically.


Online

Within a running Firefox session, it's easy to extract the data using the Mozilla Add-on API. I wrote a simple Tab Count Logger extension that does this, and saves the count to an SQLite database.

The relevant part of the code is:

const tabs = require("sdk/tabs");
const windows = require("sdk/windows").browserWindows;

console.log("Windows: " + windows.length + "; tabs: " + tabs.length);

Offline

Opened tabs are stored in sessionstore.js in the profile directory, not in SQLite. This file is JSON. A script to count tabs:

#!/usr/bin/env python3
# Count open tabs from a firefox profile
# Working directory is the root of a Firefox profile.
import json
j = json.loads(open("sessionstore.js", 'rb').read().decode('utf-8'))
def info_for_tab(tab):
    try:
        return (tab['entries'][0]['url'], tab['entries'][0]['title'])
    except IndexError:
        return None
    except KeyError:
        return None
def tabs_from_windows(window):
    return list(map(info_for_tab, window['tabs']))
all_tabs = list(map(tabs_from_windows, j['windows']))
print('Statistics: {wins} windows, {tabs} total tabs'.format(wins=len(all_tabs), tabs=sum(map(len, all_tabs))))

After having saved this to ~/bin/firefox_count_tabs, you can get the information for all your profiles as in:

for i in ~/.mozilla/firefox/*.*; do test -d $i && (echo "== $i =="; cd $i; ~/bin/firefox_count_tabs ); done

The count will be shown on the exit confirm dialog - if you hadn't disable that ️

Tags:

Sqlite

Firefox