#!/usr/bin/env python3 """Install the browser tab host and extension for the current user. Copies moonmix-tabs-host and the extension, and registers the host with every Chromium-family browser that has a profile. Loading the extension is up to the user. --uninstall removes everything again. """ import base64 import hashlib import json import os from pathlib import Path import shutil import sys NAME = "org.latha.moonmix.tabs" BROWSERS = ( "google-chrome", "google-chrome-beta", "google-chrome-unstable", "chromium", "BraveSoftware/Brave-Browser", "microsoft-edge", "vivaldi", ) def extension_id(manifest): digest = hashlib.sha256(base64.b64decode(manifest["key"])).hexdigest()[:32] return "".join(chr(ord("a") + int(c, 16)) for c in digest) def main(): root = Path(__file__).resolve().parent.parent config = Path(os.environ.get("XDG_CONFIG_HOME") or Path.home() / ".config") data = Path(os.environ.get("XDG_DATA_HOME") or Path.home() / ".local/share") host = Path.home() / ".local/bin/moonmix-tabs-host" extension = data / "moonmix/extension" registered = [config / b / "NativeMessagingHosts" / f"{NAME}.json" for b in BROWSERS] if sys.argv[1:] == ["--uninstall"]: for path in [*registered, host]: path.unlink(missing_ok=True) shutil.rmtree(extension, ignore_errors=True) print("Removed. Also remove the extension in the browser.") return built = root / "_build/native/release/build/cmd/moonmix-tabs-host/moonmix-tabs-host.exe" if not built.exists(): sys.exit("Build first: moon build --target native --release") host.parent.mkdir(parents=True, exist_ok=True) # Replace, not overwrite: the old one may be running. temporary = host.with_name(host.name + ".new") shutil.copyfile(built, temporary) temporary.chmod(0o755) temporary.replace(host) shutil.copytree(root / "extension", extension, dirs_exist_ok=True) origin = extension_id(json.loads((extension / "manifest.json").read_text())) manifest = json.dumps({ "name": NAME, "description": "moonmix browser tab names", "path": str(host), "type": "stdio", "allowed_origins": [f"chrome-extension://{origin}/"], }, indent=2) found = False for path in registered: # Only browsers that are in use here. if path.parent.parent.is_dir(): path.parent.mkdir(exist_ok=True) path.write_text(manifest + "\n") print(f"Registered: {path}") found = True if not found: print("No supported browser profile found; nothing registered.") print(f"Installed host: {host}") print("Now open the browser's extensions page (chrome://extensions), turn on") print(f"Developer mode, choose 'Load unpacked' and select:\n {extension}") if __name__ == "__main__": main()