Added way to set Firefox preferences via environment variables.

This commit is contained in:
Jocelyn Le Sage 2019-02-24 18:50:05 -05:00
parent 3b92cc215d
commit 5a635acffc
3 changed files with 123 additions and 0 deletions

View File

@ -39,6 +39,7 @@ Mozilla Firefox is a free and open-source web browser developed by Mozilla Found
* [Shell Access](#shell-access) * [Shell Access](#shell-access)
* [Increasing Shared Memory Size](#increasing-shared-memory-size) * [Increasing Shared Memory Size](#increasing-shared-memory-size)
* [Sound Support](#sound-support) * [Sound Support](#sound-support)
* [Setting Firefox Preferences Via Environment Variables](#setting-firefox-preferences-via-environment-variables)
* [Troubleshooting](#troubleshooting) * [Troubleshooting](#troubleshooting)
* [Crashes](#crashes) * [Crashes](#crashes)
* [Support or Contact](#support-or-contact) * [Support or Contact](#support-or-contact)
@ -460,6 +461,42 @@ For Firefox to be able to use the audio device available on
the host, `/dev/snd` must be exposed to the container by adding the the host, `/dev/snd` must be exposed to the container by adding the
`--device /dev/snd` parameter to the `docker run` command. `--device /dev/snd` parameter to the `docker run` command.
## Setting Firefox Preferences Via Environment Variables
Firefox preferences can be set via environment variables
passed to the containter. During the startup, a script process all these
variables and modify the preference file accordingly.
The name of the environment variable must start with `FF_PREF_`, followed by a
string of your choice. For example, `FF_PREF_MY_PREF` is a valid name.
The content of the variable should be in the format `NAME=VAL`, where `NAME` is
the name of the preference (as found in the `about:config` page) and `VAL` is
its value. A value can be one of the following type:
- string
- integer
- boolean
It is important to note that a value of type `string` should be surrounded by
double quotes. Other types don't need them.
For example, to set the `network.proxy.http` preference, one would pass the
environment variable to the container by adding the following argument to the
`docker run` command:
```
-e "FF_PREF_HTTP_PROXY=network.proxy.http=\"proxy.example.com\""
```
If a preference needs to be *removed*, its value should be set to `UNSET`. For
example:
```
-e "FF_PREF_HTTP_PROXY=network.proxy.http=UNSET
```
**NOTE**: This is an advanced usage and it is recommended to set preferences
via Firefox directly.
## Troubleshooting ## Troubleshooting
### Crashes ### Crashes

View File

@ -49,6 +49,44 @@ the host, `/dev/snd` must be exposed to the container by adding the
`--device /dev/snd` parameter to the `docker run` command. `--device /dev/snd` parameter to the `docker run` command.
</content> </content>
</section> </section>
<section>
<title level="2">Setting {{ defs.app.friendly_name }} Preferences Via Environment Variables</title>
<content>
{{ defs.app.friendly_name }} preferences can be set via environment variables
passed to the containter. During the startup, a script process all these
variables and modify the preference file accordingly.
The name of the environment variable must start with `FF_PREF_`, followed by a
string of your choice. For example, `FF_PREF_MY_PREF` is a valid name.
The content of the variable should be in the format `NAME=VAL`, where `NAME` is
the name of the preference (as found in the `about:config` page) and `VAL` is
its value. A value can be one of the following type:
- string
- integer
- boolean
It is important to note that a value of type `string` should be surrounded by
double quotes. Other types don't need them.
For example, to set the `network.proxy.http` preference, one would pass the
environment variable to the container by adding the following argument to the
`docker run` command:
```
-e "FF_PREF_HTTP_PROXY=network.proxy.http=\"proxy.example.com\""
```
If a preference needs to be *removed*, its value should be set to `UNSET`. For
example:
```
-e "FF_PREF_HTTP_PROXY=network.proxy.http=UNSET
```
**NOTE**: This is an advanced usage and it is recommended to set preferences
via {{ defs.app.friendly_name }} directly.
</content>
</section>
<section> <section>
<title level="2">Troubleshooting</title> <title level="2">Troubleshooting</title>
<content/> <content/>

View File

@ -0,0 +1,48 @@
#!/usr/bin/with-contenv sh
set -e
log() {
echo "[cont-init.d] $(basename $0): $*"
}
PREF_FILE="${1:-/config/profile/prefs.js}"
if [ -z "$PREF_FILE" ]; then
log "ERROR: Preference file not set."
exit 1
fi
mkdir -p "$(dirname "$PREF_FILE")"
[ -f "$PREF_FILE" ] || touch "$PREF_FILE"
env | grep "^FF_PREF_" | while read ENV
do
ENAME="$(echo "$ENV" | cut -d '=' -f1)"
EVAL="$(echo "$ENV" | cut -d '=' -f2-)"
if [ -z "$EVAL" ]; then
log "Skipping environment variable '$ENAME': no value set."
continue
fi
if echo "$EVAL" | grep -q "="; then
PNAME="$(echo "$EVAL" | cut -d '=' -f1)"
PVAL="$(echo "$EVAL" | cut -d '=' -f2-)"
[ -n "$PVAL" ] || PVAL='""'
else
PNAME="$EVAL"
PVAL='""'
fi
if [ "$PVAL" = "UNSET" ]; then
log "Removing preference '$PNAME'..."
sed -i "/user_pref(\"$PNAME\",.*);/d" "$PREF_FILE"
elif grep -q "user_pref(\"$PNAME\"," "$PREF_FILE"; then
log "Setting preference '$PNAME'..."
sed -i "s/user_pref(\"$PNAME\",.*);/user_pref(\"$PNAME\", $PVAL);/" "$PREF_FILE"
else
log "Setting new preference '$PNAME'..."
echo "user_pref(\"$PNAME\", $PVAL);" >> "$PREF_FILE"
fi
done