Skip to content

Instantly share code, notes, and snippets.

@homebysix
Last active December 8, 2023 23:59
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save homebysix/da935f8f0ef88d7d4b6300ff14066ca6 to your computer and use it in GitHub Desktop.
Save homebysix/da935f8f0ef88d7d4b6300ff14066ca6 to your computer and use it in GitHub Desktop.
Chrome extensions EA
#!/usr/bin/env python
# encoding: utf-8
'''
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.
Author: Elliot Jordan <elliot@lindegroup.com>
Created: 2017-01-24
Last Modified: 2017-01-25
Version: 1.0.3
'''
from distutils.version import LooseVersion
from pprint import pprint
from SystemConfiguration import SCDynamicStoreCopyConsoleUser
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]
username = [username, ""][username in [u"loginwindow", None, u""]]
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 LooseVersion(
manifest_data["version"]) > LooseVersion(result_dict[name]):
result_dict[name] = manifest_data["version"]
return result_dict
def print_results(results):
print "<result>"
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", "\"")
print result
print "</result>"
def main():
username = get_current_user()
manifest_list = get_manifests(username)
results = parse_results(manifest_list)
print_results(results)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment