Skip to content

Instantly share code, notes, and snippets.

@alphapapa
Last active September 17, 2015 06:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alphapapa/bc7f3b25025fb99dad56 to your computer and use it in GitHub Desktop.
Save alphapapa/bc7f3b25025fb99dad56 to your computer and use it in GitHub Desktop.
Pretty-print the last Firefox session's windows, tabs, and URLs. Useful for when Firefox fails to correctly reload a tab on start, and you have no idea what was loaded in that tab, but you know it was important.
#!/usr/bin/env python
import datetime, json, sys
def formatEntry(entry):
return "%s (%s) (%s)" % (entry['title'].encode('utf8')
if entry.has_key('title') else "<untitled>",
entry['url'].encode('utf8'),
datetime.datetime.fromtimestamp(
int(tab['lastAccessed']) / 1000).strftime('%Y-%m-%d %H:%M:%S'))
j = json.load(sys.stdin)
for i, window in enumerate(j['windows']):
print "Window", i
for tab in window['tabs']:
print formatEntry(tab['entries'][-1])
for e in tab['entries']:
print " %s" % formatEntry(e)
print
#!/bin/bash
# Sometimes when Firefox starts up, it displays empty tabs instead of
# the tab that was loaded in that slot. And then when you try to
# reload it, it loads "New Tab", and the tab's history is gone.
# This is extremely annoying. If you're lucky, you can use the page
# title to find the page in your browsing history. If you're not, you
# may have no idea what page was loaded in that tab, and it may be
# effectively lost for good.
# However, Firefox does store the previous session's list of windows
# and tabs in a JSON file. However, this is a giant string with no
# line breaks and lots of extraneous data. Even pretty-printing it
# doesn't help a human find what he actually needs: the list of tabs
# and URLs loaded in the previous session.
# These scripts solve that problem.
# print-last-firefox-session.sh finds the appropriate files in the
# Firefox profile directories and sends the most recent one to
# print-firefox-session.py.
# print-firefox-session.py prints a list of windows, tabs, and the
# history of each tab, along with tab titles and last-accessed
# date/times.
# Last tested with Firefox 40.0.3. Should work with most previous
# versions, since it will still find "sessionstore.js" files. I don't
# think the format has changed much.
file=$(find $HOME/.mozilla/firefox -type f -iname sessionstore.js -o -iname previous.js -o -iname recovery.js -exec ls -t '{}' \; | head -n1)
if ! [[ -f $file ]]
then
echo "$file not found"
exit 1
fi
pp-firefox-session-urls.py <$file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment