feat: firefox-prefetch-addons.sh script
This commit is contained in:
117
firefox-prefetch-addons.sh
Normal file
117
firefox-prefetch-addons.sh
Normal file
@@ -0,0 +1,117 @@
|
||||
if [[ "$*" == *"--help"* ]]; then
|
||||
echo 'Usage: firefox-prefetch-addons [FLAGS...] [ADDONS...]'
|
||||
echo 'A simple bash script to fetch Firefox addons IDs, to be used in ExtensionSettings policy.'
|
||||
echo ''
|
||||
echo 'ADDONS can be both just the name of the extension or the URL of the extensions page, so'
|
||||
echo 'for example: "ublock-origin" and "https://addons.mozilla.org/en-US/firefox/addon/ublock-origin/"'
|
||||
echo 'are treated as the same.'
|
||||
echo ''
|
||||
echo 'Options:'
|
||||
echo ''
|
||||
echo ' --format-json: format resulting addons object as JSON.'
|
||||
echo ' --format-nix: format resulting addons object as a Nix expression (requires the "nix" command to be installed).'
|
||||
echo ' --pretty: prettify resulting object output (requires "alejandra" to be installed for Nix output).'
|
||||
echo ' --no-pretty: do not prettify resulting object output.'
|
||||
echo ''
|
||||
echo 'Dependencies:'
|
||||
echo ''
|
||||
echo ' alejandra: OPTIONAL, used for formatting the output Nix expression.'
|
||||
echo ' jq: REQUIRED, used to properly work with extensions JSON values.'
|
||||
echo ' nix: OPTIONAL, used to transform the JSON output to a Nix expression.'
|
||||
echo ' unzip: REQUIRED, used to unzip downloaded extension.'
|
||||
echo ' wget: REQUIRED, used to download manifest of extensions.'
|
||||
echo ''
|
||||
echo '2025 (c) Gustavo "Guz" L. de Mello <contact@guz.one>'
|
||||
echo 'Licensed under WTFPL license. PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND.'
|
||||
exit 0
|
||||
fi
|
||||
|
||||
addons_object="{}"
|
||||
|
||||
format_as_nix=false
|
||||
exit_1=false
|
||||
|
||||
# Check if command is being piped to another
|
||||
if [[ -t 1 ]]; then
|
||||
pretty=true
|
||||
fi
|
||||
|
||||
for a in "$@"; do
|
||||
if [[ "$a" == "--format-json" ]]; then
|
||||
format_as_nix=false
|
||||
continue
|
||||
fi
|
||||
if [[ "$a" == "--format-nix" ]]; then
|
||||
format_as_nix=true
|
||||
continue
|
||||
fi
|
||||
if [[ "$a" == "--pretty" ]]; then
|
||||
pretty=true
|
||||
continue
|
||||
fi
|
||||
if [[ "$a" == "--no-pretty" ]]; then
|
||||
pretty=false
|
||||
continue
|
||||
fi
|
||||
|
||||
addon_name="$a"
|
||||
if [[ "$addon_name" == "http"* ]]; then
|
||||
addon_name="''${addon_name%/}"
|
||||
addon_name="''${addon_name##*/}"
|
||||
fi
|
||||
|
||||
install_url="https://addons.mozilla.org/firefox/downloads/latest/$addon_name/latest.xpi"
|
||||
temp_extension_xpi="/tmp/firefox-prefetch-addons-$addon_name.xpi"
|
||||
temp_extension_directory="/tmp/firefox-prefetch-addons-$addon_name-dir"
|
||||
|
||||
error="$(wget -O "$temp_extension_xpi" "$install_url" 2>&1 1>/dev/null)"
|
||||
if [[ "$?" -ne 0 ]]; then
|
||||
echo "Failed to download manifest for addon \"$a\""
|
||||
echo "$error"
|
||||
exit_1=true
|
||||
fi
|
||||
|
||||
error="$(unzip "$temp_extension_xpi" -d "$temp_extension_directory" 2>&1 1>/dev/null)"
|
||||
if [[ "$?" -ne 0 ]]; then
|
||||
echo "Failed to unzip manifest for addon \"$a\""
|
||||
echo "$error"
|
||||
exit_1=true
|
||||
fi
|
||||
|
||||
addon_id="$(jq -r '.browser_specific_settings.gecko.id' "$temp_extension_directory/manifest.json")"
|
||||
addons_object="$(echo "$addons_object" | jq -r ". += {\"$addon_id\": {install_url: \"$install_url\", installation_mode: \"force_installed\"}}")"
|
||||
|
||||
rm "$temp_extension_xpi"
|
||||
rm -r "$temp_extension_directory"
|
||||
done
|
||||
|
||||
if [[ "$format_as_nix" == true ]]; then
|
||||
echo "$addons_object" >"/tmp/firefox-prefetch-addons-json-object.json"
|
||||
|
||||
nix eval --impure --expr 'builtins.fromJSON (builtins.readFile /tmp/firefox-prefetch-addons-json-object.json)' >"/tmp/firefox-prefetch-addons-json-object.nix"
|
||||
if [[ "$pretty" == true ]]; then
|
||||
error="$(alejandra "/tmp/firefox-prefetch-addons-json-object.nix" 2>&1 1>/dev/null)"
|
||||
if [[ "$?" -ne 0 ]]; then
|
||||
echo "Failed to format Nix file of addons object"
|
||||
echo "$error"
|
||||
exit_1=true
|
||||
fi
|
||||
fi
|
||||
|
||||
cat "/tmp/firefox-prefetch-addons-json-object.nix"
|
||||
|
||||
rm "/tmp/firefox-prefetch-addons-json-object.json"
|
||||
rm "/tmp/firefox-prefetch-addons-json-object.nix"
|
||||
else
|
||||
if [[ "$pretty" == true ]]; then
|
||||
echo "$addons_object" | jq . | cat
|
||||
else
|
||||
echo "$addons_object" | jq -c . | cat
|
||||
fi
|
||||
fi
|
||||
|
||||
if [[ "$exit_1" == true ]]; then
|
||||
exit 1
|
||||
else
|
||||
exit 0
|
||||
fi
|
||||
Reference in New Issue
Block a user