Il peut parfois être utile de récupérer l'extension ID d'une extension de Firefox, de Thunderbird, etc. en n'ayant sous la main que son fichier .xpi, notamment pour une installation automatisée.
Il faut donc un moyen automatique de connaître cet ID.
La MozillaZine Knowledge Base propose une commande UNIX qui fait le job en extrayant le fichier de métadonnées install.rdf
et en cherchant l'ID dans un nœud XML <em:id>
(unzip
et xmlstarlet
sont requis) :
# Retrieve the extension id for an addon from its install.rdf get_extension_id() { unzip -qc $1 install.rdf | xmlstarlet sel \ -N rdf=http://www.w3.org/1999/02/22-rdf-syntax-ns# \ -N em=http://www.mozilla.org/2004/em-rdf# \ -t -v \ "//rdf:Description[@about='urn:mozilla:install-manifest']/em:id" }mais il s'avère qu'elle ne gère pas certains XPI où l'architecture de
install.rdf
est différente : dans mon cas c'était Tab Mix Plus où l'ID se trouve dans l'attribut NS1:id
du nœud XML <RDF:Description>
.
Je vous propose une nouvelle version de cette commande qui gère les deux cas :
get_extension_id() { unzip -qc "$1" install.rdf | xmlstarlet sel \ -N rdf=http://www.w3.org/1999/02/22-rdf-syntax-ns# \ -N em=http://www.mozilla.org/2004/em-rdf# \ -t \ -m "//rdf:Description" \ --if "@about='urn:mozilla:install-manifest'" -v "./em:id" \ --elif "@rdf:about='urn:mozilla:install-manifest'" -v "./@em:id" }
Ainsi qu'un script Bash pour une utilisation plus aisée :
#/bin/bash if [ "$#" -lt 1 ] ; then echo >&2 "Usage: $0 <xpi_file>" exit 1 fi command -v unzip >/dev/null 2>&1 || { echo >&2 "It seems 'unzip' is not installed. As it is needed, program will abort." exit 1 } command -v xmlstarlet >/dev/null 2>&1 || { echo >&2 "It seems 'xmlstarlet' is not installed. As it is needed, program will abort." exit 1 } XPI_FILEPATH="$1" if [ ! -e "$XPI_FILEPATH" ] ; then echo >&2 'File: "'$XPI_FILEPATH'" does not exists, program will abort' exit 2 fi # Following code extracts the "install.rdf" file and reads the extension ID: # from node "<em:id>" ("old" way) # or # from attribute "em:id" ("new" way) EXT_ID=$( unzip -qc "$XPI_FILEPATH" install.rdf | xmlstarlet sel \ -N rdf=http://www.w3.org/1999/02/22-rdf-syntax-ns# \ -N em=http://www.mozilla.org/2004/em-rdf# \ -t \ -m "//rdf:Description" \ --if "@about='urn:mozilla:install-manifest'" -v "./em:id" \ --elif "@rdf:about='urn:mozilla:install-manifest'" -v "./@em:id" ) if [ -n "$EXT_ID" ] ; then echo $EXT_ID exit 0 else echo >&2 'Failed to read extension ID from "'$XPI_FILEPATH'".' exit 3 fi