Skip to content

Instantly share code, notes, and snippets.

@stefanschmidt
Created April 23, 2022 15:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stefanschmidt/72c307df74226280254dcaf0e69f9560 to your computer and use it in GitHub Desktop.
Save stefanschmidt/72c307df74226280254dcaf0e69f9560 to your computer and use it in GitHub Desktop.
Delete Firefox Addon preferences
import sys
import glob
import os
import configparser
import json
import sqlite3
# Functions for finding Firefox default profile folder adapted from
# https://github.com/1dot75cm/browsercookie
def parse_profile(profile):
cp = configparser.ConfigParser()
cp.read(profile)
path = None
for section in cp.sections():
try:
if cp.getboolean(section, 'IsRelative'):
path = os.path.dirname(profile) + '/' + cp.get(section, 'Path')
else:
path = cp.get(section, 'Path')
if cp.has_option(section, 'Default'):
return os.path.abspath(os.path.expanduser(path))
except configparser.NoOptionError:
pass
if path:
return os.path.abspath(os.path.expanduser(path))
raise Exception('No default Firefox profile found')
def find_default_profile():
if sys.platform == 'darwin':
return glob.glob(os.path.expanduser('~/Library/Application Support/Firefox/profiles.ini'))
elif sys.platform.startswith('linux'):
return glob.glob(os.path.expanduser('~/.mozilla/firefox/profiles.ini'))
elif sys.platform == 'win32':
return glob.glob(os.path.join(os.getenv('APPDATA', ''), 'Mozilla/Firefox/profiles.ini'))
else:
raise Exception('Unsupported operating system: ' + sys.platform)
def find_profile_path():
profile = find_default_profile()
if not profile:
raise FindProfileError('Could not find default Firefox profile')
path = parse_profile(profile[0])
if not path:
raise Exception('Could not find path to default Firefox profile')
return path
def find_storage_db():
path = find_profile_path()
storage_db_path = os.path.expanduser(path + '/storage-sync-v2.sqlite')
if storage_db_path:
return storage_db_path
else:
raise Exception('Failed to find Firefox storage database')
def find_addons_json():
path = find_profile_path()
storage_db_path = os.path.expanduser(path + '/addons.json')
if storage_db_path:
return storage_db_path
else:
raise Exception('Failed to find Firefox storage database')
def find_extension_id(adddon_name):
addons_json_path = find_addons_json()
with open(addons_json_path) as f:
d = json.load(f)
for a in d['addons']:
if a['name'] == adddon_name:
ext_id = a['id']
return ext_id
adddon_name = 'Foobar'
addons_json_path = find_addons_json()
storage_db_path = find_storage_db()
ext_id = find_extension_id(adddon_name)
conn = sqlite3.connect(storage_db_path)
curs = conn.cursor()
curs.execute("select data from storage_sync_data where ext_id==(?)", (ext_id,))
result = curs.fetchone()
if result:
print('Found preferences of ' + adddon_name + "\n")
print(str(result) + "\n")
curs.execute("delete from storage_sync_data where ext_id==(?)", (ext_id,))
if curs.rowcount:
print('Deleted preferences of ' + adddon_name)
else:
print('No preferences found of ' + adddon_name)
conn.commit()
conn.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment