Update Firefox extension sync modes

This commit is contained in:
tke
2026-08-21 19:07:59 +02:00
parent 348a275480
commit f434d58ef9
+122 -5
View File
@@ -2,13 +2,43 @@
set -euo pipefail
EXTENSION_NAME="${1:-bitwarden}"
DIRECTION="${2:-inet-to-intra}"
usage() {
echo "Usage: $0 [extension_name|all] [inet-to-intra|intra-to-inet]"
echo " no arguments: list extension names from the source profile"
echo " extension_name: copy the first matching extension by name"
echo " all: copy every extension from source to target"
echo " direction: which way to copy (default: inet-to-intra)"
}
EXTENSION_NAME=""
DIRECTION="inet-to-intra"
case $# in
0)
;;
1)
case "$1" in
inet-to-intra|intra-to-inet)
usage
exit 1
;;
*)
EXTENSION_NAME="$1"
;;
esac
;;
2)
EXTENSION_NAME="$1"
DIRECTION="$2"
;;
*)
usage
exit 1
;;
esac
if [[ "$DIRECTION" != "inet-to-intra" && "$DIRECTION" != "intra-to-inet" ]]; then
echo "Usage: $0 [extension_name] [inet-to-intra|intra-to-inet]"
echo " extension_name: Name to search for (default: bitwarden)"
echo " direction: Which way to copy (default: inet-to-intra)"
usage
exit 1
fi
@@ -97,6 +127,72 @@ find_extension() {
return 1
}
list_extensions() {
local ext_dir="$1"
if [[ ! -d "$ext_dir" ]]; then
return 1
fi
shopt -s nullglob
for ext in "$ext_dir"/*; do
if [[ -d "$ext" ]]; then
if [[ -f "$ext/manifest.json" ]]; then
local manifest_name
manifest_name=$(grep -oE '"name"[[:space:]]*:[[:space:]]*"[^"]+"' "$ext/manifest.json" 2>/dev/null | head -n1 | sed -E 's/.*"([^"]+)".*/\1/' || true)
if [[ -n "$manifest_name" ]]; then
echo "$manifest_name"
else
echo "$(basename "$ext")"
fi
fi
elif [[ -f "$ext" ]] && [[ "$ext" == *.xpi ]]; then
local manifest_name
manifest_name=$(unzip -p "$ext" manifest.json 2>/dev/null | grep -oE '"name"[[:space:]]*:[[:space:]]*"[^"]+"' | head -n1 | sed -E 's/.*"([^"]+)".*/\1/' || true)
if [[ -n "$manifest_name" ]]; then
echo "$manifest_name"
else
echo "$(basename "$ext")"
fi
fi
done
shopt -u nullglob
}
copy_all_extensions() {
local source_ext_dir="$1"
local target_ext_dir="$2"
if [[ ! -d "$source_ext_dir" ]]; then
echo "Error: Source extensions directory not found"
exit 1
fi
mkdir -p "$target_ext_dir"
shopt -s nullglob
local copied=0
for ext in "$source_ext_dir"/*; do
local ext_name
ext_name=$(basename "$ext")
local target_ext="$target_ext_dir/$ext_name"
if [[ -e "$target_ext" ]]; then
echo "Removing existing extension from target: $ext_name"
rm -rf "$target_ext"
fi
cp -r "$ext" "$target_ext"
echo "Copied: $ext_name"
copied=1
done
shopt -u nullglob
if [[ $copied -eq 0 ]]; then
echo "No extensions found to copy."
fi
}
main() {
echo "=== Firefox Extension Sync ($DIRECTION) ==="
echo ""
@@ -140,6 +236,27 @@ main() {
echo "Target: $(basename "$target_profile")"
echo ""
if [[ -z "$EXTENSION_NAME" ]]; then
echo "Extensions in source profile:"
local found=0
while IFS= read -r ext; do
found=1
[[ -n "$ext" ]] && echo " - $ext"
done < <(list_extensions "$source_profile/extensions" || true)
if [[ $found -eq 0 ]]; then
echo " - <none>"
fi
exit 0
fi
if [[ "$EXTENSION_NAME" == "all" ]]; then
echo "Copying all extensions..."
copy_all_extensions "$source_profile/extensions" "$target_profile/extensions"
echo ""
echo "Done! Restart Firefox to load the extensions."
exit 0
fi
local source_ext
source_ext=$(find_extension "$source_profile/extensions" "$EXTENSION_NAME") || {
echo "Error: $EXTENSION_NAME extension not found in source profile"