How to install Firefox addon from command line in scripts?

I want to improve my unattended script adding some Firefox addon, however I cant find the way,

Can someone help to find out how to?

Example: (Want to install adblockPlus plugin and set a new default webpage)

wget https://addons.mozilla.org/firefox/downloads/latest/1865/addon-1865-latest.xpi
firefox -silent -install-global-extension addon-1865-latest.xpi -setDefaultBrowser www.google.es

Thanks in advance.


Solution 1:

Using your method

gksudo firefox -install-global-extension addon-1865-latest.xpi seems to do the trick for you. That will install the extension to all users on your system.

To install the extension only for your user use the extension path as an argument

firefox addon-1865-latest.xpi

You still need to click the Install button though!

Automating the installation

Firefox does not need the addon file name but the identifier from the addon as a package name. That means that if you are planning on installing an addon without user intervention you need to extract it to a folder with the name of the addon identifier string, not the name of the addon.

The identifier string can be found on the first lines of the addon install manifest file install.rdf and it looks like this: <em:id>{d10d0bf8-f5b5-c8b4-a8b2-2b9879e08c5d}</em:id>. Everything within the {} (including the curly braces) is the identifier.

To get an addon to work you need to extract the package, rename the folder that contains the files to the addon identifier string and place it either on the global addon folder or within the user addon folder.

Global addon install

If you want to install an extension automatically to all users in your system you need to extract it, rename the folder that contains the addon to the addon's id string and copy it to the firefox global extensions folder /usr/share/mozilla/extensions/{ec8030f7-c20a-464f-9b0e-13a3a9e97384}/, anything that you use there will be called up automatic when a user opens firefox.

User specific install

If you want to install an extension automatically to just one user in your system you need to extract it, rename the folder that contains the addon to the addon's id string and copy it to the firefox user extensions folder /home/user_name/.mozilla/extensions/{ec8030f7-c20a-464f-9b0e-13a3a9e97384}/ (create it if it does not exist), anything that you use there will be called up automatic when a user opens firefox.

How-to prepare an addon for automatic install - Example

Make an extensions folder in your home and download the addon in to it

mkdir ~/extensions
cd ~/extensions
wget https://addons.mozilla.org/firefox/downloads/latest/1865/addon-1865-latest.xpi

Extract it and delete the original

unzip ~/extensions/addon-1865-latest.xpi
rm ~/extensions/addon-1865-latest.xpi

Read the first line in the install.rdf file to get the addon's id (in this case it will be {d10d0bf8-f5b5-c8b4-a8b2-2b9879e08c5d}). and create a folder with that name

mkdir ~/extensions/{d10d0bf8-f5b5-c8b4-a8b2-2b9879e08c5d}

Move all the files in your extensions folder into the newly created ~/extensions/{d10d0bf8-f5b5-c8b4-a8b2-2b9879e08c5d} and you are ready to install by moving the {d10d0bf8-f5b5-c8b4-a8b2-2b9879e08c5d} folder, as described, for a local install or for a global install.

How-to set the default home page

To change your homepage without using the preferences inside firefox you have to edit ~/.mozilla/firefox/*.default/prefs.js (where *.default is a folder inside ~/.mozilla/firefox created for your user) and add this line to the end of it

user_pref("browser.startup.homepage", "http://uptechtalk.com");

or using this command

echo "user_pref("browser.startup.homepage", "http://uptechtalk.com");" >> ~/.mozilla/firefox/*.default/prefs.js

You need to do it after closing firefox or the program will overwrite the setting on exit.

If your user has not used firefox yet and you want to set the homepage for all new users (set homepage globally) use this command

echo "user_pref("browser.startup.homepage", "http://uptechtalk.com");" >> /etc/xul-ext/ubufox.js

Comments about your question

-silent does not exist, you will be prompted to install that xpi extension anyways and you have to click the button to install it;

-setDefaultBrowser will not set your homepage, it will make firefox your default browser

Solution 2:

This is problematic since in different versions of Firefox, different things work and at some nothing work. For the newer versions you just have to rename the .xpi to <addon id>.xpi and place it in an extensions folder. When you start firefox afterwards, you will be asked to accept the installation of all addons you added there.

Here is some BASH functions that make your life easier..

EXTENSIONS_SYSTEM='/usr/share/mozilla/extensions/{ec8030f7-c20a-464f-9b0e-13a3a9e97384}/'
EXTENSIONS_USER=`echo ~/.mozilla/firefox/*.default/extensions/`

# -------------------------- xpi tools ---------------------------------

get_addon_id_from_xpi () { #path to .xpi file
    addon_id_line=`unzip -p $1 install.rdf | egrep '<em:id>' -m 1`
    addon_id=`echo $addon_id_line | sed "s/.*>\(.*\)<.*/\1/"`
    echo "$addon_id"
}

get_addon_name_from_xpi () { #path to .xpi file
    addon_name_line=`unzip -p $1 install.rdf | egrep '<em:name>' -m 1`
    addon_name=`echo $addon_name_line | sed "s/.*>\(.*\)<.*/\1/"`
    echo "$addon_name"
}

# Installs .xpi given by relative path
# to the extensions path given
install_addon () {
    xpi="${PWD}/${1}"
    extensions_path=$2
    new_filename=`get_addon_id_from_xpi $xpi`.xpi
    new_filepath="${extensions_path}${new_filename}"
    addon_name=`get_addon_name_from_xpi $xpi`
    if [ -f "$new_filepath" ]; then
        echo "File already exists: $new_filepath"
        echo "Skipping installation for addon $addon_name."
    else
        cp "$xpi" "$new_filepath"
    fi
}

Let's install Adblock..

wget https://addons.mozilla.org/firefox/downloads/latest/1865/addon-1865-latest.xpi
install_addon addon-1865-latest.xpi "$EXTENSIONS_USER"

Solution 3:

Global plugins are not enabled by default, and you need to add them to the list in the prefs.js file in their profile in order for them to be enabled. A major pain for mass deployments.

Here's a sample script for when we dumped a bunch of prefs.js files from client machines up to the network and changed from IETab to IETab2, migrated their preferences, etc.

Another way is to create a user profile you like on a box, upload it to the network, then clone it across machines in Firefox\defaults\profile and all new users will inherit that profile.

`

----------------------------------------------------------------------
#!/bin/bash

pjsbase="/data/M_drive/Temp/prefsjs"

for userf in `find ${pjsbase} -maxdepth 1 -type f -name *.prefs.js -printf "%f\n"`
do
  echo ${userf}
  # add in IETab2 GUID and remove IETab GUID
  grep extensions.enabledItems ${pjsbase}/${userf} | 
  /bin/sed 's/\")/\,\{1BC9BA34-1EED-42ca-A505-6D2F1A935BBB\}\:2\.12\.21\.1\")/' | 
  /bin/sed 's/{77b819fa-95ad-4f2c-ac7c-486b356188a9}:1.5.20090525,//' > \
     ${pjsbase}/tmp1
  /bin/sed 's/0\.3\.8\.[0-9]*/0\.3\.8\.4/g' ${pjsbase}/tmp1 > ${pjsbase}/tmp
  /bin/sed /extensions.enabledItems/d ${pjsbase}/${userf}   > ${pjsbase}/tmp2
  cat ${pjsbase}/tmp2 > ${pjsbase}/${userf}.new2
  cat ${pjsbase}/tmp >> ${pjsbase}/${userf}.new2
  # add in IETab2 preferences
  echo user_pref\(\"extensions.ietab2.hasRun\"\,\ true\)\; >> \
     ${pjsbase}/${userf}.new2
  echo user_pref\(\"extensions.ietab2.ietab2PrefsMigrated\"\,\ true\)\; >> \
     ${pjsbase}/${userf}.new2
  echo user_pref\(\"extensions.ietab2.prefsMigrated\"\,\ true\)\; >> \
     ${pjsbase}/${userf}.new2
  echo user_pref\(\"extensions.ietab2.version\"\,\ \"2.12.21.1\"\)\; >> \
     ${pjsbase}/${userf}.new2
  echo user_pref\(\"extensions.update.notifyUser\"\,\ false\)\; >> \
     ${pjsbase}/${userf}.new2
  # if they have a preference list then migrate it
  if [ ! `grep user_pref\(\"ietab.filterlist\" ${pjsbase}/${userf} |
          wc -l` -eq 0 ]; then
    echo "user_pref(\"extensions.ietab2.filterlist"$(
       grep user_pref\(\"ietab.filterlist\" ${pjsbase}/${userf} | 
       sed 's/user\_pref(\"ietab\.filterlist//')"" >> ${pjsbase}/${userf}.new2
  fi
  # make sure prefs are alphabetised
  egrep -v ^u > ${pjsbase}/${userf}.new ${pjsbase}/${userf}.new2
  egrep ^u  ${pjsbase}/${userf}.new2 | sort >> ${pjsbase}/${userf}.new
done

`

Solution 4:

For some of you this shell script might be helpful. It parses the first occurrence of the em:id tag in install.rdf (described by Bruno Pereira):

#!/bin/sh
var=`grep -m 1 -e em:id install.rdf`   
var=${var#*\>}
var=${var%<*}

...giving you the id (including the {}).

Solution 5:

Firefox add-ons one liner. Adblock Plus, FlashBlock, and Download Helper downloaded in that order, then in firefox opening all found .xpi files, then removing those .xpi files:

wget \ 
 https://addons.mozilla.org/firefox/downloads/latest/1865/addon-1865-latest.xpi \ 
 https://addons.mozilla.org/firefox/downloads/latest/433/addon-433-latest.xpi \
 https://addons.mozilla.org/firefox/downloads/latest/3006/addon-3006-latest.xpi && 
firefox *.xpi && rm *.xpi