Files
gists/tools/system/copy_firefox_extension.sh
2026-05-22 13:01:54 +02:00

169 lines
4.0 KiB
Bash
Executable File

#!/bin/bash
set -euo pipefail
EXTENSION_NAME="${1:-bitwarden}"
DIRECTION="${2:-inet-to-intra}"
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)"
exit 1
fi
MOZILLA_BASE="${HOME}/.mozilla/firefox"
SNAP_BASE="${HOME}/snap/firefox/common/.mozilla/firefox"
find_base_path() {
if [[ -d "$MOZILLA_BASE" ]] && [[ -f "$MOZILLA_BASE/profiles.ini" ]]; then
echo "$MOZILLA_BASE"
return 0
elif [[ -d "$SNAP_BASE" ]] && [[ -f "$SNAP_BASE/profiles.ini" ]]; then
echo "$SNAP_BASE"
return 0
fi
return 1
}
find_profile() {
local base="$1"
local name="$2"
local pattern
case "$name" in
internet)
pattern="*internet*" ;;
intranet)
pattern="*intranet*" ;;
noproxy)
pattern="*noProxy*" ;;
*)
pattern="*${name}*" ;;
esac
for prof in "$base"/$pattern; do
if [[ -d "$prof" ]]; then
echo "$prof"
return 0
fi
done
return 1
}
list_profiles() {
local base="$1"
if [[ ! -d "$base" ]]; then
return 1
fi
for prof in "$base"/*.default*; do
if [[ -d "$prof" ]]; then
echo "$prof"
fi
done
for prof in "$base"/*.*; do
if [[ -d "$prof" ]]; then
echo "$prof"
fi
done
}
find_extension() {
local ext_dir="$1"
local name="$2"
if [[ ! -d "$ext_dir" ]]; then
return 1
fi
for ext in "$ext_dir"/*; do
if [[ -d "$ext" ]]; then
if [[ -f "$ext/manifest.json" ]]; then
if grep -qi "$name" "$ext/manifest.json" 2>/dev/null; then
echo "$ext"
return 0
fi
fi
elif [[ -f "$ext" ]] && [[ "$ext" == *.xpi ]]; then
if unzip -p "$ext" manifest.json 2>/dev/null | grep -qi "$name"; then
echo "$ext"
return 0
fi
fi
done
return 1
}
main() {
echo "=== Firefox Extension Sync ($DIRECTION) ==="
echo ""
local base_path
base_path=$(find_base_path) || {
echo "Error: Could not find Firefox profile directory"
exit 1
}
echo "Profile base path: $base_path"
echo "Available profiles:"
list_profiles "$base_path" | while read -r p; do
echo " - $(basename "$p")"
done
echo ""
local source_profile target_profile
case "$DIRECTION" in
inet-to-intra)
source_profile=$(find_profile "$base_path" "internet")
target_profile=$(find_profile "$base_path" "intranet")
;;
intra-to-inet)
source_profile=$(find_profile "$base_path" "intranet")
target_profile=$(find_profile "$base_path" "internet")
;;
esac
if [[ -z "$source_profile" ]]; then
echo "Error: Could not find source profile"
exit 1
fi
if [[ -z "$target_profile" ]]; then
echo "Error: Could not find target profile"
exit 1
fi
echo "Source: $(basename "$source_profile")"
echo "Target: $(basename "$target_profile")"
echo ""
local source_ext
source_ext=$(find_extension "$source_profile/extensions" "$EXTENSION_NAME") || {
echo "Error: $EXTENSION_NAME extension not found in source profile"
exit 1
}
local ext_name
ext_name=$(basename "$source_ext")
echo "Found extension: $ext_name"
echo ""
local target_ext="$target_profile/extensions/$ext_name"
if [[ -d "$target_ext" ]]; then
echo "Removing existing extension from target..."
rm -rf "$target_ext"
fi
cp -r "$source_ext" "$target_ext"
echo "Copied successfully!"
echo ""
echo "Done! Restart Firefox to load the extension."
}
main "$@"