For users of Firefox and SessionSaver who lost sessions upgrading to 0.9.x

by Uche Ogbuji


SessionSaver created by "pike" based on work by "rue" is an "old" extension that does not work on Firefox 0.9.x. Some, including me, upgraded for the security fixes and now find we can't use SS, now can we resume old sessions. One hope is that the authors can update SessionSaver to work with recent versions of Firefox, but "rue" himself, who seems to have re-taken development over from Pike, seems to indicate it could be a matter of months before that happens. Some people claim they use SS fine in Firefox 0.9.2, but I haven't been able to get any version to work. If you've been successful, please be kind enough to post details as a comment here or in the SS mozdev forum.



This is all extremely annoying for me because SS was the only reason I was finally able to switch from Galeon to Firefox a couple of months ago. Why on earth is this basic, commonsense capability not built into Firefox, anyway?



As a desperate measure I wrote a a quick, brutal hack in Python to retrieve a given session from the prefs file and "replay" it by launching the stored Firefox windows and tabs. The code is pasted below. It's not elegant because I needed the-minimum-that-would-work. I found one or two quirks in its reproduction of the saved session while using it, but it got me about 95% of the way. It does discard history information and the position of each window.



To use it, first update the paths at the top to reflect your own Firefox install. Then invoke the script with your Firefox prefs.js file as the first argument. This will process the prefs file and give you a list of session names you can replay. Then run the script the same way, but with the chosen session name as the second argument. Good luck.



And yes, I know about the webbrowser module. It doesn't work with Firefox except in most recent CVS versions of Python. At least I couldn't get it to work with stock Python 2.3.




import os
import time
import sys
import re

NEW_FIREFOX_COMMAND = '/usr/local/bin/firefox &'
#PROFILE =
FIREFOX_NEW_WINDOW_COMMAND = '/usr/local/lib/firefox/mozilla-xremote-client -a firefox "openURL(%s, new-window)"'
FIREFOX_NEW_TAB_COMMAND = '/usr/local/lib/firefox/mozilla-xremote-client -a firefox "openURL(%s, new-tab)"'

SESSION_PAT = re.compile('user_pref\("sessionsaver(\.static)?\.(.*?)\.session[0-9]+", "(.*)"\);')

def parse_sessions(f):
sessions = {}
for line in f:
match = SESSION_PAT.match(line)
if match:
name = match.group(2)
data = match.group(3)
#The line seems to end with " 0"
if data.strip():
data = data.split()[-2]
tabs_data = data.split('|||')
tabs = []
for tab_data in tabs_data:
current = tab_data.split('||')[-1]
link = current.split('|')[-1]
tabs.append(link)
sessions.setdefault(name, []).append(tabs)
return sessions

def launch_firefox(url, new_window=0):
if new_window:
cmd = FIREFOX_NEW_WINDOW_COMMAND%url
else:
cmd = FIREFOX_NEW_TAB_COMMAND%url
os.system(cmd)
return

def launch(session):
os.system(NEW_FIREFOX_COMMAND)
first = 1
for window in session:
if window:
url = window[0]
print 'Launching', url, 'in new window'
if not first:
#The first time, we've already launched a new window
launch_firefox(url, 1)
first = 0
time.sleep(1)
for url in window[1:]:
time.sleep(1)
print 'Launching', url, 'in new tab'
launch_firefox(url)
return

try:
fname = sys.argv[1]
except IndexError:
print 'Usage:\n\t%s prefs-file [session-name]'%sys.argv[0]
print
sys.exit(1)
try:
selected_session = sys.argv[2]
except IndexError:
sessions = parse_sessions(open(fname, 'r'))
print 'Usage:\n\t%s prefs-file [session-name]\n'%sys.argv[0]
print 'Available session names:', ', '.join(sessions.keys())
print
sys.exit(1)
sessions = parse_sessions(open(fname, 'r'))
launch(sessions[selected_session])

#Handy for debugging
#import pprint; pprint.pprint(sessions)




Do you have any other tricks for rescuing stranded sessions?