tilde-site/content/blog/2025-02-26-bash-count-firefox-tabs.md

44 lines
1.9 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

+++
title = "Count your opened Firefox tabs with bash"
date = "2025-02-26"
updated = "2025-02-26"
[extra]
authors = ["Ydreniv"]
+++
At any point in time, I have many Firefox tabs opened.
I might even have reached a thousand.
This feels like an issue I should address at some point, and I have already
tried.
But today, my goal is easier : count the number of currently opened tabs.
In truth, its fairly easy : many extensions provide this information, in more
or less granular details (number of windows, number of tabs in a specific
window…).
However neat this is, I want to get my total number of opened tabs printed in
a Unix shell.
Indeed, I ultimately would like to automatically graph this number over time.
Before I started, I figured Id need to find a session file, list the tabs,
and get the count.
Sounds fairly easy right ?
Well it was not too hard in the end, but there were a couple bumps.
The first is the format used to store the sessions.
Its compressed using LZ4, but with headers set by Mozilla, before LZ4 was
fully standardized.
[This StackOverflow](https://superuser.com/a/1363751) answer gives a bunch of
tools to decompress the file.
Ive chosen to use [andikleens tool](https://github.com/andikleen/lz4json.git)
as it worked out of the box easily.
Once in possession of the extracted JSON, Ive used `jq` to get the tabs IDs, and
`wc` to count them.
So with `./lz4jsoncat /home/<user>/snap/firefox/common/.mozilla/firefox/<profile-id>.default-release/sessionstore-backups/recovery.jsonlz4 | jq '.windows[].tabs[].index' | wc -l`,
Ive gotten my answer in a shell : 138 (yikes !).
Im using only one Firefox profile, but I suppose you could adapt this shell
pipe to support more.
Anyway, Ill need to work on a cronjob to do it regularly, and an agent to
collect these data (probably Prometheus).
This would be one of the first metrics Id collect for my "local computer
monitoring center".