linux: use user.js over prefs.js for Firefox #232

Manage Firefox preferences through `user.js` instead of `prefs.js`.
Because of Mozilla's recommendation against direct `prefs.js` edits to
avoid potential profile corruption. Instead, the `user.js` file, if
present, overrides the settings in `prefs.js` at application startup.

Change AddFirefoxPrefs function to update `user.js` and manage
creation/deletion of this file:

1. Handle file creation if `user.js` does not exist.
2. Deletes file if `user.js` becomes empty after reverting settings.

Other changes:

- Improve log messages
- Minimal refactorings
This commit is contained in:
undergroundwires
2023-08-27 20:14:10 +02:00
parent ecce47fdcd
commit dae6d114da

View File

@@ -3558,82 +3558,98 @@ functions:
parameters:
- name: prefName
- name: jsonValue
# prefs.js file (https://web.archive.org/web/20221029211757/https://kb.mozillazine.org/Prefs.js_file) exists at
# - Default installation:
# ~/.mozilla/firefox/<profile-name>/prefs.js
# - Flatpak installation:
# ~/.var/app/org.mozilla.firefox/.mozilla/firefox/<profile-name>/prefs.js
# - Snap installation:
# ~/snap/firefox/common/.mozilla/firefox/<profile-name>/prefs.js
docs: |-
This script either creates or updates the `user.js` file to set specific Mozilla Firefox preferences.
The `user.js` file can be found in a Firefox profile folder [1] and its location depends on the type of installation:
- Default: `~/.mozilla/firefox/<profile-name>/user.js`
- Flatpak: `~/.var/app/org.mozilla.firefox/.mozilla/firefox/<profile-name>/user.js`
- Snap: `~/snap/firefox/common/.mozilla/firefox/<profile-name>/user.js`
While the `user.js` file is optional [2], if it's present, the Firefox application will prioritize its settings over
those in `prefs.js` upon startup [1][2]. To prevent potential profile corruption, Mozilla advises against editing
`prefs.js` directly [2].
[1]: https://web.archive.org/web/20230811005205/https://kb.mozillazine.org/User.js_file "User.js file - MozillaZine Knowledge Base"
[2]: https://web.archive.org/web/20221029211757/https://kb.mozillazine.org/Prefs.js_file "Prefs.js file - MozillaZine Knowledge Base"
code: |-
pref_name='{{ $prefName }}'
pref_value='{{ $jsonValue }}'
echo "Setting preference \"$pref_name\" to \"$pref_value\"."
pref_file_paths=(
~/.mozilla/firefox/*/prefs.js
~/.var/app/org.mozilla.firefox/.mozilla/firefox/*/prefs.js
~/snap/firefox/common/.mozilla/firefox/*/prefs.js
declare -a profile_paths=(
~/.mozilla/firefox/*/
~/.var/app/org.mozilla.firefox/.mozilla/firefox/*/
~/snap/firefox/common/.mozilla/firefox/*/
)
declare -i total_files_found=0
for pref_file in "${pref_file_paths[@]}"; do
if [ -f "$pref_file" ]; then
((total_files_found++))
echo "$pref_file:"
pref_start="user_pref(\"$pref_name\","
pref_line="user_pref(\"$pref_name\", $pref_value);"
if ! grep --quiet "^$pref_start" "${pref_file}"; then
echo $'\t'"Preference is not configured before."
echo -n $'\n'"$pref_line" >> "$pref_file"
echo $'\t'"Successfully configured."
else
if grep --quiet "^$pref_line$" "${pref_file}"; then
echo $'\t'"Skipping. Preference is already configured as expected."
else
sed --in-place "/^$pref_start/d" "$pref_file"
echo $'\t'"Deleted assignment with unexpected value."
echo -n $'\n'"$pref_line" >> "$pref_file"
echo $'\t'"Successfully reconfigured with expected value."
fi
fi
declare -i total_profiles_found=0
for profile_dir in "${profile_paths[@]}"; do
if [ ! -d "$profile_dir" ]; then
continue
fi
if [[ ! "$(basename "$profile_dir")" =~ ^[a-z0-9]{8}\..+ ]]; then
continue # Not a profile folder
fi
((total_profiles_found++))
user_js_file="${profile_dir}user.js"
echo "$user_js_file:"
if [ ! -f "$user_js_file" ]; then
touch "$user_js_file"
echo $'\t''Created new user.js file'
fi
pref_start="user_pref(\"$pref_name\","
pref_line="user_pref(\"$pref_name\", $pref_value);"
if ! grep --quiet "^$pref_start" "${user_js_file}"; then
echo -n $'\n'"$pref_line" >> "$user_js_file"
echo $'\t'"Successfully added a new preference in $user_js_file."
elif grep --quiet "^$pref_line$" "$user_js_file"; then
echo $'\t'"Skipping, preference is already set as expected in $user_js_file."
else
sed --in-place "/^$pref_start/c\\$pref_line" "$user_js_file"
echo $'\t'"Successfully replaced the existing incorrect preference in $user_js_file."
fi
done
if [ "$total_files_found" -eq 0 ]; then
echo "No changes, no preference file is found."
if [ "$total_profiles_found" -eq 0 ]; then
echo 'No profile folders are found, no changes are made.'
else
echo "Ensured that $total_files_found profiles are compilant."
echo "Preferences verified in $total_profiles_found profiles."
fi
revertCode: |-
pref_name='{{ $prefName }}'
pref_value='{{ $jsonValue }}'
echo "Restoring \"$pref_name\" to its default."
pref_file_paths=(
~/.mozilla/firefox/*/prefs.js
~/.var/app/org.mozilla.firefox/.mozilla/firefox/*/prefs.js
~/snap/firefox/common/.mozilla/firefox/*/prefs.js
echo "Reverting preference: \"$pref_name\" to its default."
declare -a profile_paths=(
~/.mozilla/firefox/*/
~/.var/app/org.mozilla.firefox/.mozilla/firefox/*/
~/snap/firefox/common/.mozilla/firefox/*/
)
declare -i total_files_found=0
for pref_file in "${pref_file_paths[@]}"; do
if [ -f "$pref_file" ]; then
((total_files_found++))
echo "$pref_file:"
pref_start="user_pref(\"$pref_name\","
pref_line="user_pref(\"$pref_name\", $pref_value);"
if ! grep --quiet "^$pref_start" "${pref_file}"; then
echo $'\t'"Skipping. Preference is not configured before."
else
if grep --quiet "^$pref_line$" "${pref_file}"; then
sed --in-place "/^$pref_line/d" "$pref_file"
echo $'\t'"Successfully restored preference value to its default."
else
echo $'\t'"Skipping, the preference has value that is not configured by privacy.sexy."
fi
declare -i total_profiles_found=0
for profile_dir in "${profile_paths[@]}"; do
user_js_file="${profile_dir}user.js"
if [ ! -f "$user_js_file" ]; then
continue
fi
((total_profiles_found++))
echo "$user_js_file:"
pref_start="user_pref(\"$pref_name\","
pref_line="user_pref(\"$pref_name\", $pref_value);"
if ! grep --quiet "^$pref_start" "${user_js_file}"; then
echo $'\t''Skipping, preference was not configured before.'
elif grep --quiet "^$pref_line$" "${user_js_file}"; then
sed --in-place "/^$pref_line/d" "$user_js_file"
echo $'\t''Successfully reverted preference to default.'
if ! grep --quiet '[^[:space:]]' "$user_js_file"; then
rm "$user_js_file"
echo $'\t''Removed user.js file as it became empty.'
fi
else
echo $'\t''Skipping, the preference has value that is not configured by privacy.sexy.'
fi
done
if [ "$total_files_found" -eq 0 ]; then
echo "No changes, no preference file is found."
if [ "$total_profiles_found" -eq 0 ]; then
echo 'No reversion was necessary.'
else
echo "Ensured that $total_files_found profiles are compilant."
echo "Preferences verified in $total_profiles_found profiles."
fi
-
name: RenameFile