Skip to content

Instantly share code, notes, and snippets.

@korc
Last active October 22, 2022 08:05
Show Gist options
  • Save korc/c2312eeffba10f28fff57429339e66c7 to your computer and use it in GitHub Desktop.
Save korc/c2312eeffba10f28fff57429339e66c7 to your computer and use it in GitHub Desktop.
Set up Chrome master_preferences file for Pre-installed Extensions. Cf. https://www.chromium.org/administrators/pre-installed-extensions/
#!/usr/bin/env python3
import sys
import os
import json
args = sys.argv[1:]
if not args:
print(
f"Usage: {sys.argv[0]} [-o <initial_preferences_file>] <Preferences_file> [<extension_id..>]")
exit(1)
output_file = None
if args[0] == "-o":
output_file = args[1]
args = args[2:]
exts = json.load(open(args[0]))['extensions']["settings"]
out_settings = {}
if len(args) == 1:
print("Please choose extension id(s):\n")
for ext in exts:
print(f"{ext}\t{exts[ext]['manifest']['name']}")
exit(0)
for ext_id in args[1:]:
ext = exts[ext_id]
out_cfg = dict(
location=1,
path=os.path.join(os.path.split(ext["path"])[0], "0.0"),
manifest=dict(
version="0.0",
name=ext["manifest"]["name"]+" (installing)",
manifest_version=2
)
)
for k in ("key", "update_url", "permissions", "content_scripts"):
if k in ext["manifest"]:
out_cfg["manifest"][k] = ext["manifest"][k]
if "granted_permissions" in ext:
out_cfg["granted_permissions"] = ext["granted_permissions"]
out_settings[ext_id] = out_cfg
if not output_file:
for ext_id in out_settings:
p_id = "" if len(out_settings) == 1 else f"{ext_id}: "
print(f"{p_id}{json.dumps(out_settings[ext_id])}")
exit(0)
try:
output_preferences = json.load(open(output_file))
except IOError:
output_preferences = {}
for ext_id in out_settings:
output_preferences.setdefault("extensions", {}).setdefault(
"settings", {})[ext_id] = out_settings[ext_id]
json.dump(output_preferences, open(output_file, "w"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment