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

后端 未结 4 524
悲&欢浪女
悲&欢浪女 2021-02-05 11:48

I want to automatically count the number of tabs that are open in Firefox so that I can track this over time.

It is therefore not enough that I can get an add-on that di

4条回答
  •  盖世英雄少女心
    2021-02-05 12:34

    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
    

提交回复
热议问题