Skip to content

Instantly share code, notes, and snippets.

@superkeyor
Last active January 1, 2024 18:10
Show Gist options
  • Save superkeyor/897ec8170b3a62d9fad48309efbd5f4a to your computer and use it in GitHub Desktop.
Save superkeyor/897ec8170b3a62d9fad48309efbd5f4a to your computer and use it in GitHub Desktop.
bash script to export an addon/extension's url uuid given its (partial) name
function firefox_extension() {
# Usage: firefox_extension "name" ["url|uuid"]
# Parameters:
# extension_name (string): The name of the addon (case insensitive, could be partial, via grep).
# parameter_type (string): The type of information to retrieve (url, uuid).
# Examples:
# firefox_extension "Bitwarden - Free Password Manager" "uuid"
# firefox_extension (Lists all available extension names)
# Notes:
# extensions.json has name, url, id
# prefs.js has id:uuid at extensions.webextensions.uuids
# hard-coded for Mac, but could be modified for Linux
# will create output files in extensions subfolder of working directory
# inspired by https://stackoverflow.com/questions/2054364
# reference: about:debugging#/runtime/this-firefox
function findpath() {
local pattern=$1
local base_dir=$(dirname "${pattern}")
local filename=$(basename "${pattern}")
find "${base_dir}" -name "${filename}" -maxdepth 1
}
local backup_folder="extensions"
mkdir -p "$backup_folder"
local profile_dir=$(findpath "$HOME/Library/Application Support/Firefox/Profiles/*-*")
local extensions_file="$profile_dir/extensions.json"
local prefs_file="$profile_dir/prefs.js"
local lang=$(grep -Eo '^[^.]*' <<<"$LANG")
# Process extensions.json
# enabled
jq --arg lang "$lang" -j '.addons |
map(select(.type == "extension" and .location == "app-profile" and (.userDisabled | not))) |
sort_by(.defaultLocale.name) | .[] |
"- [",.defaultLocale.name,"]",
"(https://addons.mozilla.org/\($lang)/firefox/addon/",(.id|@uri),")\t", .id, "\n"' "$extensions_file" | tr -d '{}' | sort > "$backup_folder/url.md"
# disabled
jq --arg lang "$lang" -j '.addons |
map(select(.type == "extension" and .location == "app-profile" and (.userDisabled))) |
sort_by(.defaultLocale.name) | .[] |
"- [",.defaultLocale.name,"]",
"(https://addons.mozilla.org/\($lang)/firefox/addon/",(.id|@uri),")\t", .id, "\n"' "$extensions_file" | tr -d '{}' | sort > "$backup_folder/disabled.md"
# Process prefs.js for UUIDs
local uuids_line=$(grep "extensions.webextensions.uuids" "$prefs_file" | sed 's/user_pref("extensions.webextensions.uuids", "//' | tr -d ');' | tr -d '\\')
local key=""
local value=""
while read -r pair; do
key=$(echo $pair | cut -d':' -f1 | tr -d '"{} ,')
value=$(echo $pair | cut -d':' -f2 | tr -d '"{} ,')
echo -e "$key\t$value"
done < <(echo $uuids_line | tr ',' '\n') > "$backup_folder/uuid.txt"
local name=$1
local param_type=$2
if [ -z "$name" ]; then
echo "Available addon names:"
cat "$backup_folder/url.md" | cut -d'(' -f1
local total=$(wc -l < "$backup_folder/url.md")
echo "Total Enabled: $total"
echo 'General Usage: firefox_extension "name" ["url|uuid"]'
return
fi
local result=""
case "$param_type" in
url)
result=$(grep -i "$name" "$backup_folder/url.md" | cut -d'(' -f2 | cut -d')' -f1)
;;
uuid)
# cut by tab $'\t'
local id=$(grep -i "$name" "$backup_folder/url.md" | cut -d$'\t' -f2)
result=$(grep "$id" "$backup_folder/uuid.txt" | cut -d$'\t' -f2)
;;
*)
echo "Invalid parameter type. Choose 'url', or 'uuid'."
return 1
;;
esac
echo $result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment