Skip to content

Instantly share code, notes, and snippets.

@dsmortensen
Created August 8, 2022 14:25
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/88fd6a58263077b889b63778a46f7a5b to your computer and use it in GitHub Desktop.
Save dsmortensen/88fd6a58263077b889b63778a46f7a5b to your computer and use it in GitHub Desktop.
Chrome extensions EA (managed_python3)
#!/usr/local/bin/managed_python3
'''
Name: chrome_extensions.py
Description: This extension attribute returns a list of the names and
versions of all Chrome extensions 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 <dmortensen@simonsfoundation.org>
Created: 2022-04-26
Last Modified: 2022-04-26
Version: 1.0
'''
import getpass
from pkg_resources import parse_version
from SystemConfiguration import SCDynamicStoreCopyConsoleUser
from pprint import pprint
import json
import os
import sys
# The locales of the languages you prefer, in order of preference.
locales = ["en_US", "en_GB", "en"]
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/"
"Google/Chrome" % username)
for dirpath, dirnames, filenames in os.walk(ext_folder):
for filename in filenames:
if filename == "manifest.json":
manifest_list.append(os.path.join(dirpath, filename))
return manifest_list
def get_localized_name(ext_root, msg):
localized_name = None
for locale in locales:
messages = os.path.join(ext_root, "_locales", locale, "messages.json")
if os.path.exists(messages):
with open(messages) as messages_file:
messages_data = json.load(messages_file)
try:
localized_name = messages_data[msg]["message"]
except KeyError:
localized_name = messages_data[msg.lower()]["message"]
break
return localized_name
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 manifest_data["name"].startswith("__MSG_"):
msg = manifest_data["name"].lstrip("__MSG_").rstrip("__")
name = get_localized_name(os.path.split(manifest)[0], msg)
else:
name = manifest_data["name"]
# Only store the "newest" version of each extension.
if name not in result_dict or parse_version(
manifest_data["version"]) > parse_version(result_dict[name]):
result_dict[name] = manifest_data["version"]
return result_dict
def print_results(results):
list = ""
count = 0
for (name, version) in sorted(results.items()):
result = "%s (version %s)" % (name, version)
# 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