First let me confess, I am a tab-aholic…
I tend to have several Firefox windows open at once, with several tabs in each. And I leave them open for… well… weeks. And usually that’s fine – it restores everything if I have to reboot or restart Firefox, and all is well. Except when it doesn’t.
Sometimes, for whatever reason, the sessions aren’t restored, and all of those tabs, as in “hey, I wasn’t quite done with that!”, are lost.
When this happened to me last night, I took a look in my Firefox profile directory and discovered something about the way Firefox stores it’s sessions. I’m sure it’s well-known to many, but it was news to me.
When I looked at sessionstore.bak (the automatically created backup of sessionstore.js) I saw something like:
({"windows":[{"tabs":[{"entries":[{"url":"http://stoa.canterburyschool.org:9080
/stoa/courses/Faculty/TechnologyGuides/VoIPandSkypeinForeignLanguage
/document_edit_form","title":": VoIP and Skype in Foreign Language","ID":2528274837}...
Hmmmm... I know that's Javascript, but it also looks a lot like a Python dictionary. Why not see if Python could eval() it to a dictionary? My first try failed on the unknown identifiers "true" and "false", but after defining true and false as equal to Python's True and False, it worked. Now I had a nested set of dictionaries containing all of my previous session's info. Not bad.
[Edit: Not being a regular JSON user, and since the file extension was .js, not .json, I never considered that this might be JSON. It turns that it's not QUITE JSON and the Python JSON library won't decode it (without at least stripping off the leading and trailing parentheses). Check out this discussion for more info.]
From there it was easy to create some code that would grab the URL's from an unrecoverable session and put them into an HTML page for easy access later.
Warning: this code uses eval() on arbitrary strings loaded from an arbitrary file, so it is not secure - use with great caution. If it wipes your hard drive, sells your house and absconds with the money, and kicks your dog on the way out, you have been warned.
In any case, below is my session recovery code, which I've already used a couple of times.
import sys
""" this code is released to the public domain and meant only for illustration. It is not
warranted as being safe or suitable for any particular purpose at all.
in particular, this program evaluates (and potentially executes) arbitrary code,
so use with caution.
"""
def get_session(infilename, outfilename):
true = True
false = False
sessionstring = open(infilename).read()
session = eval(sessionstring)
outfile = open(outfilename, "w")
for window in session['windows']:
outfile.write("---------------Window-----------------<p>\n")
outfile.write("<ul>\n")
for tab in window['tabs']:
outfile.write( '<li><a href="%s" \="">%s</a> - %s</li> \n' %
(tab['entries'][0]['url'],tab['entries'][0]['url'],
tab['entries'][0]['title']))
outfile.write("</ul>\n")
outfile.close()
if __name__ == '__main__':
infile = sys.argv[1]
if len(sys.argv) == 3:
outfile = sys.argv[2]
else:
outfile = "sessionsave.html"
get_session(infile, outfile)
Posted by Naomi Ceder 
