Firefox tabs git repo: Difference between revisions

From WTFwiki
Jump to navigation Jump to search
Stian (talk | contribs)
Created page with "How to commit your Firefox tabs to a git repo automatically. == Scripts == Cron job: <syntaxhighlight lang="cron"> # Firefox tabs backup 50 2 * * * /home/stian/bin/cro..."
 
Stian (talk | contribs)
No edit summary
 
Line 25: Line 25:


<syntaxhighlight lang="python">
<syntaxhighlight lang="python">
#!/usr/bin/python
#!/usr/bin/env python3
# -*- coding: utf-8 -*-


import os, glob, sys, json, subprocess
import os, glob, sys, json, subprocess
Line 33: Line 32:
     (profile,) = glob.glob(os.path.expanduser("~/.mozilla/firefox/*.default"))
     (profile,) = glob.glob(os.path.expanduser("~/.mozilla/firefox/*.default"))
except:
except:
     print >>sys.stderr, "No profile or multiple default profiles"
     print("No profile or multiple default profiles", file=sys.stderr)
     sys.exit(1)
     sys.exit(1)


Line 42: Line 41:
         includetitle = True
         includetitle = True
     elif sys.argv[1] == '-h':
     elif sys.argv[1] == '-h':
         print >>sys.stderr, "Usage: %s [-h] [-t]" % sys.argv[0]
         print(f"Usage: {sys.argv[0]} [-h] [-t]\n"
        print >>sys.stderr, "  -h  Help."
              "  -h  Help.\n"
        print >>sys.stderr, "  -t  Include titles."
              "  -t  Include titles.", file=sys.stderr)
         sys.exit(1)
         sys.exit(1)


Line 52: Line 51:
wcount = 0
wcount = 0
for window in js['windows']:
for window in js['windows']:
     print "Window", wcount
     print("Window", wcount)
     wcount += 1
     wcount += 1
     for tab in window['tabs']:
     for tab in window['tabs']:
Line 64: Line 63:
         else:
         else:
             out = "\t" + url
             out = "\t" + url
         print out.encode('utf-8')
         print(out)
</syntaxhighlight>
</syntaxhighlight>


You need to install the dejsonlz4 command from [https://github.com/avih/dejsonlz4] because when mozilla added compression to it they decided in their infinite wisdome to use a unique file format that no standard tool can read.
You need to install the dejsonlz4 command from [https://github.com/avih/dejsonlz4] because when mozilla added compression to it they decided in their infinite wisdome to use a unique file format that no standard tool can read.

Latest revision as of 13:21, 21 February 2025

How to commit your Firefox tabs to a git repo automatically.

Scripts

Cron job:

# Firefox tabs backup
50 2 * * *      /home/stian/bin/cron-tabdump.sh

Looks like:

#!/bin/sh

cd ~/config/tabdump
~/bin/tabdump -t >tabdump
git diff --quiet && exit 0
git add tabdump
git commit -q -m "cron autocommit $(date -I)"

And tabdump:

#!/usr/bin/env python3

import os, glob, sys, json, subprocess

try:
    (profile,) = glob.glob(os.path.expanduser("~/.mozilla/firefox/*.default"))
except:
    print("No profile or multiple default profiles", file=sys.stderr)
    sys.exit(1)

includetitle = False

if len(sys.argv) > 1:
    if sys.argv[1] == '-t':
        includetitle = True
    elif sys.argv[1] == '-h':
        print(f"Usage: {sys.argv[0]} [-h] [-t]\n"
               "   -h   Help.\n"
               "   -t   Include titles.", file=sys.stderr)
        sys.exit(1)

fn = os.path.join(profile, "sessionstore-backups", "recovery.jsonlz4")
js = json.loads(subprocess.check_output(['/home/stian/down/dejsonlz4/dejsonlz4', fn]))

wcount = 0
for window in js['windows']:
    print("Window", wcount)
    wcount += 1
    for tab in window['tabs']:
        if not tab['entries']:
            continue
        cur = tab['entries'][-1]
        url = cur['url']
        title = cur['title'] if 'title' in cur else ''
        if includetitle:
            out = "\t" + url + "\t" + title
        else:
            out = "\t" + url
        print(out)

You need to install the dejsonlz4 command from [1] because when mozilla added compression to it they decided in their infinite wisdome to use a unique file format that no standard tool can read.