-
Notifications
You must be signed in to change notification settings - Fork 73
/
dump_firefox_session.py
executable file
·72 lines (57 loc) · 1.95 KB
/
dump_firefox_session.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env python
# LZ4 logic taken from: https://gist.github.com/Tblue/62ff47bef7f894e92ed5
# Copyright (c) 2015, Tilman Blumenbach
import json
import sys
try:
import lz4.block
except ImportError:
sys.stderr.write('Please "pip install lz4"\n')
raise SystemExit(1)
class MozLz4aError(Exception):
pass
class InvalidHeader(MozLz4aError):
def __init__(self, msg):
self.msg = msg
def __str__(self):
return self.msg
def decompress(file_obj):
if file_obj.read(8) != b"mozLz40\0":
raise InvalidHeader("Invalid magic number")
return lz4.block.decompress(file_obj.read())
def dump_session_js(fpath):
with open(fpath, 'r') as fh:
sess = json.loads(fh.read())
for window in sess['windows']:
print('=== WINDOW: %s (%s)===' % (window.get('title', 'NO TITLE'), window['selected']))
if 'closedAt' in window:
continue
for tab in window['tabs']:
if 'closedAt' in tab:
continue
print(str(tab['index']) + ' ' + tab['entries'][-1]['url'])
def dump_session_jsonlz4(fpath):
with open(fpath, "rb") as in_file:
data = decompress(in_file)
sess = json.loads(data)
for window in sess['windows']:
print('=== WINDOW: %s (%s)===' % (window.get('title', 'NO TITLE'), window['selected']))
if 'closedAt' in window:
continue
for tab in window['tabs']:
if 'closedAt' in tab:
continue
print(str(tab['index']) + ' ' + tab['entries'][-1]['url'])
if __name__ == "__main__":
if len(sys.argv) < 1:
sys.stderr.write(
"USAGE: dump_firefox_session.py /path/to/sessionstore.js\n"
)
raise SystemExit(1)
fpath = sys.argv[1]
if fpath.endswith('.js'):
dump_session_js(sys.argv[1])
elif fpath.endswith('.jsonlz4'):
dump_session_jsonlz4(fpath)
else:
raise SystemExit('Unknown file extension.')