Skip to content

Instantly share code, notes, and snippets.

@mariogt
Forked from lazymutt/firefox_tab_extractor.py
Created June 26, 2020 22:28
Show Gist options
  • Select an option

  • Save mariogt/367ac11d21675a79ee6f28f1e60e5144 to your computer and use it in GitHub Desktop.

Select an option

Save mariogt/367ac11d21675a79ee6f28f1e60e5144 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
# Copyright (c) 2017 University of Utah Student Computing Labs. ################
# All Rights Reserved.
#
# Permission to use, copy, modify, and distribute this software and
# its documentation for any purpose and without fee is hereby granted,
# provided that the above copyright notice appears in all copies and
# that both that copyright notice and this permission notice appear
# in supporting documentation, and that the name of The University
# of Utah not be used in advertising or publicity pertaining to
# distribution of the software without specific, written prior
# permission. This software is supplied as is without expressed or
# implied warranties of any kind.
################################################################################
# firefox_tab_extractor.py #####################################################
# 04/17/17, v1.0, todd.mcdaniel@utah.edu
#
# I open way too many tabs. There, I said it. Admitting the problem is the
# first step, programming your way out of it is the second. :) When I realize
# that Firefox is slowing my machine to a crawl, it's then difficult and
# time-consuming to clean up several hundred tabs. I wanted a solution to
# backup everything and start fresh.
#
# This script will read Firefox's sessionstore.js and save each open window,
# each tab from that window, and the history of URLs visited in that tab.
#
# Usage:
#
# To process a specific file:
#
# firefox_tab_extractor.py ~/someplace/something.js
#
# or use the script without a path to have it decide which sessionstore to use:
#
# The script will use [profile]/sessionstore.js is Firefox is not running,
# or [profile]/sessionstore-backups/recovery.js, if it is.
#
# In either case, the script will output the results to the running users Desktop folder.
# ie ~/Desktop/firefox_tabs_04172017_yourhostname.txt
#
# Sample output:
#
# Window #0000 Tab #0000 Entry #0000 Title: Mozilla Firefox Start Page
# URL: about:home
# Entry #0001 Title: sessionstore.js - Google Search
# URL: https://www.google.com/search?q=google&ie=utf-8&oe=utf-8
# Entry #0002 Title: sessionstore.js - Google Search
# URL: https://www.google.com/search?q=google&ie=utf-8&oe=utf-8#q=sessionstore.js
# Tab #0001 Entry #0000 Title: Apple
# URL: https://www.apple.com/
# Tab #0002 Entry #0000 URL: https://github.com/
#
#
################################################################################
from __future__ import print_function
import json
import sys
import os
import pwd
import socket
import time
def main():
hostname = (socket.gethostname()).split(".")[0]
user_dir = pwd.getpwuid(os.getuid())[5]
if len(sys.argv) > 1:
sessionstore_path = sys.argv[1]
if not os.path.exists(sessionstore_path):
print("File doesn't exist. Exiting.")
sys.exit()
else:
try:
working_dir = user_dir + '/Library/Application Support/Firefox/'
with open(working_dir + '/profiles.ini', 'r') as ff_pref_file:
ff_prefs = ff_pref_file.read()
ff_prefs = [x for x in ff_prefs.split('\n') if x]
for item in ff_prefs:
if 'Path' in item:
working_dir = working_dir + item.split('=')[1]
if os.path.exists(working_dir + '/sessionstore.js'):
sessionstore_path = working_dir + '/sessionstore.js'
else:
if os.path.exists(working_dir + '/sessionstore-backups/recovery.js'):
sessionstore_path = working_dir + '/sessionstore-backups/recovery.js'
else:
print("Unable to find sessionstore.js or recovery.js. Exiting.")
quit()
except Exception as this_exception:
print(this_exception)
quit()
output_path = user_dir + '/Desktop/firefox_tabs_' + time.strftime("%m%d%Y") + '_' + hostname + '.txt'
print("Using sessionstore: \033[4m%s\033[0m\n" % sessionstore_path)
print("Output to: \033[4m%s\033[0m\n" % output_path)
if os.path.exists(output_path):
overwrite_check = raw_input("File already exists, overwrite [Yy to continue]: ")
if overwrite_check.lower() != "y":
print("Exiting.")
quit()
with open(sessionstore_path) as data_file:
consumed_sessionstore = json.load(data_file)
with open(output_path, 'w') as output_file:
for window_index, window in enumerate(consumed_sessionstore['windows']):
window_string = "Window #{:04d} ".format(window_index)
blank_window_string = " " * len(window_string)
for tab_index, tab in enumerate(window['tabs']):
tab_string = "Tab #{:04d} ".format(tab_index)
blank_tab_string = " " * len(tab_string)
for entry_index, entries in enumerate(tab['entries']):
entry_string = "Entry #{:04d} ".format(entry_index)
blank_entry_string = " " * len(entry_string)
blank_overlap = blank_window_string + blank_tab_string + blank_entry_string
if entry_index == 0:
if tab_index == 0:
try:
print("{}{}{}Title: {}\n{} URL: {}".format(window_string, tab_string, entry_string, entries['title'], blank_overlap, entries['url']), file=output_file)
except:
print("{}{}{}URL: {}".format(window_string, tab_string, entry_string, entries['url']), file=output_file)
else:
try:
print("{}{}{}Title: {}\n{} URL: {}".format(blank_window_string, tab_string, entry_string, entries['title'], blank_overlap, entries['url']), file=output_file)
except:
print("{}{}{}URL: {}".format(blank_window_string, tab_string, entry_string, entries['url']), file=output_file)
else:
try:
print("{}{}{}Title: {}\n{} URL: {}".format(blank_window_string, blank_tab_string, entry_string, entries['title'], blank_overlap, entries['url']), file=output_file)
except:
print("{}{}{}URL: {}".format(blank_window_string, blank_tab_string, entry_string, entries['url']), file=output_file)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment