Skip to content

Instantly share code, notes, and snippets.

@dsmortensen
Created August 8, 2022 13:12
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 dsmortensen/0db283aa32d0775007cc589865ca88d7 to your computer and use it in GitHub Desktop.
Save dsmortensen/0db283aa32d0775007cc589865ca88d7 to your computer and use it in GitHub Desktop.
Firefox add-ons EA(python3)
#!/usr/local/bin/managed_python3
'''
Name: firefox_add_ons.py
Description: This extension attribute returns a list of the names and versions of all Firefox add-ons installed for the current user.
Based on a python3 version of the Chrome Extensions EA by Elliot Jordan <elliot@lindegroup.com>. (https://gist.github.com/astral303/e414400ec7a69c34480e1705d371e15b)
Uses Greg Neagle's managed_python3 (https://github.com/macadmins/python).
Author: Daniel Mortensen <dsmtheperson@gmail.com>
Created: 2022-04-26
Last Modified: 2022-04-26
Version: 1.0
'''
import json
import os
import sys
from SystemConfiguration import SCDynamicStoreCopyConsoleUser
from pkg_resources import parse_version
from pprint import pprint
def get_current_user():
username = (SCDynamicStoreCopyConsoleUser(None, None, None) or [None])[0]
return username
def get_manifests(username):
manifest_list = []
ext_folder = ("/Users/%s/Library/Application Support/"
"Firefox/Profiles" % username)
for dirpath, dirnames, filenames in os.walk(ext_folder):
for filename in filenames:
if filename == "addons.json":
manifest_list.append(os.path.join(dirpath, filename))
return manifest_list
def parse_results(manifest_list):
result_dict = {}
for manifest in manifest_list:
with open(manifest) as manifest_file:
manifest_data = json.load(manifest_file)
if "addons" in manifest_data:
add_ons = manifest_data["addons"]
for add_on in add_ons:
info = {}
if "name" in add_on:
add_on_name = add_on.get("name")
version = add_on.get("version")
url = add_on["creator"]["url"]
if add_on_name not in result_dict or parse_version(
version) > parse_version(result_dict[add_on_name]["version"]):
info["version"] = version
info["url"] = url
result_dict[add_on_name] = info
return result_dict
def print_results(results):
data_dump = json.dumps(results)
data_json = json.loads(data_dump)
list = ""
for name in data_json:
info = data_json.get(name)
version = info.get("version")
url = info.get("url")
result = "%s (version %s, %s)" % (name, version, url)
# Straighten curly quotes to avoid UnicodeEncodeError.
result = result.replace(u"\u2018", "'").replace(
u"\u2019", "'").replace(
u"\u201c", "\"").replace(
u"\u201d", "\"")
list = list + result + "\n"
return list.strip()
def main():
username = get_current_user()
manifest_list = get_manifests(username)
results = parse_results(manifest_list)
to_echo = print_results(results)
print("<result>%s</result>" % to_echo)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment