Skip to content

Instantly share code, notes, and snippets.

@mirogula
Forked from warner/make-html.py
Last active May 9, 2022 09:34
Show Gist options
  • Save mirogula/de2e5cf5303b815b272c to your computer and use it in GitHub Desktop.
Save mirogula/de2e5cf5303b815b272c to your computer and use it in GitHub Desktop.
Convert Firefox sessionstore.js file to html.
#!/usr/bin/python
################################################################################
# usage: session2html.py [-h] -s SESSION_FILE [-o OUTPUT_FILE]
#
# Convert Firefox sessionstore.js file to html.
#
# optional arguments:
# -h, --help show this help message and exit
# -s SESSION_FILE session file to be converted
# -o OUTPUT_FILE write html output to OUTPUT_FILE; if not defined write html
# output to stdout
################################################################################
import os, sys, json, time, argparse
from xml.sax.saxutils import escape
html_escape_table = {
'"': """,
"'": "'"
}
# escapes this characters: '&', '<', '>', '"', and '''
def html_escape(text):
return escape(text, html_escape_table)
# parse script arguments
desc="Convert Firefox sessionstore.js file to html."
parser = argparse.ArgumentParser(description=desc)
parser.add_argument("-s", dest="sessionfile", type=argparse.FileType('r'),
metavar="SESSION_FILE", required=True, help="session file to be converted")
parser.add_argument("-o", dest="outputfile", type=argparse.FileType('w'),
default=sys.stdout, metavar="OUTPUT_FILE", help="write html output \
to OUTPUT_FILE; if not defined write html output to stdout")
args = parser.parse_args()
# read session file and parse it to json structure
ss = json.loads(args.sessionfile.read().decode("utf-8"))
mtimeInfo = "<p>File mtime {0}</p>".format(
time.ctime(os.stat(args.sessionfile.name).st_mtime))
lastUpdateInfo = ""
if "session" in ss:
if "lastUpdate" in ss["session"]:
lastUpdateInfo = "<p>Recorded session.lastUpdate {0}</p>".format(
time.ctime(ss["session"]["lastUpdate"]/1000.0))
args.outputfile.write("""
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<title>Open Tabs from session file</title>
</head>
<body>
<h1>Open Tabs from Session File</h1>
{0}
{1}
<ul style="list-style-type:none">
""".format(mtimeInfo, lastUpdateInfo))
counter = 1
for window in ss["windows"]:
for tab in window["tabs"]:
entries = tab["entries"]
args.outputfile.write('<li>#{0}<ul style="list-style-type:none">'.format(
counter))
for entry in entries:
url = entry["url"].encode("utf-8")
title = html_escape(entry.get("title", url)).encode("utf-8")
line = '<li><a href="{0}">{1}</a> : <tt>{2}</tt></li>\n'.format(
url, title, url)
args.outputfile.write(line)
args.outputfile.write("</ul>")
print "tabs processed: {0}".format(counter)
counter += 1
args.outputfile.write("""
</ul>
<p></p>
</body>
</html>
"""
)
args.outputfile.close()
@mirogula
Copy link
Author

This script can also be used to convert session files from session manager add-on (https://addons.mozilla.org/en-us/firefox/addon/session-manager/). All you have to do is delete first 4 lines. For example, this is normal session file from session manager:

[SessionManager v2]
name=Google-2014-09-22
timestamp=1411403027705
autosave=false  count=1/2   screensize=1024x768
{"windows":[{"tabs":[{"entries":[{"url":"https://www.google.com/?.........

, and this is file usable by this script:

{"windows":[{"tabs":[{"entries":[{"url":"https://www.google.com/?.........

@Xananax
Copy link

Xananax commented Mar 21, 2016

Lifesaver, thanks a bunch (note for fellow users, this uses python2)

@Hubcapp
Copy link

Hubcapp commented Jun 29, 2018

I forked it to put headers for separate Windows and for the (now obsoleted but still used by me) tab groups. Feel free to update your gist with it if you want to.

edit: also favicons

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment