2018-04-24 08:47:50 -04:00
#!/usr/bin/env bash
## prefs.js cleaner for Linux/Mac
## author: @claustromaniac
2023-10-07 06:33:49 -04:00
## version: 2.0
2018-04-24 08:47:50 -04:00
## special thanks to @overdodactyl and @earthlng for a few snippets that I stol..*cough* borrowed from the updater.sh
2022-12-10 09:00:23 -05:00
## DON'T GO HIGHER THAN VERSION x.9 !! ( because of ASCII comparison in update_prefsCleaner() )
readonly CURRDIR = $( pwd )
2018-04-24 08:47:50 -04:00
## get the full path of this script (readlink for Linux, greadlink for Mac with coreutils installed)
2022-12-10 09:00:23 -05:00
SCRIPT_FILE = $( readlink -f " ${ BASH_SOURCE [0] } " 2>/dev/null || greadlink -f " ${ BASH_SOURCE [0] } " 2>/dev/null)
2018-04-24 08:47:50 -04:00
## fallback for Macs without coreutils
2022-12-10 09:00:23 -05:00
[ -z " $SCRIPT_FILE " ] && SCRIPT_FILE = ${ BASH_SOURCE [0] }
2018-04-24 08:47:50 -04:00
2022-12-10 09:00:23 -05:00
AUTOUPDATE = true
QUICKSTART = false
## download method priority: curl -> wget
DOWNLOAD_METHOD = ''
if command -v curl >/dev/null; then
DOWNLOAD_METHOD = 'curl --max-redirs 3 -so'
elif command -v wget >/dev/null; then
DOWNLOAD_METHOD = 'wget --max-redirect 3 --quiet -O'
else
AUTOUPDATE = false
echo -e "No curl or wget detected.\nAutomatic self-update disabled!"
fi
2018-04-24 08:47:50 -04:00
fQuit( ) {
## change directory back to the original working directory
2022-12-10 09:00:23 -05:00
cd " ${ CURRDIR } "
2021-09-27 11:23:12 -04:00
[ " $1 " -eq 0 ] && echo -e " \n $2 " || echo -e " \n $2 " >& 2
2018-04-24 08:47:50 -04:00
exit $1
}
2021-06-07 07:18:40 -04:00
fUsage( ) {
2022-12-10 09:00:23 -05:00
echo -e " \nUsage: $0 [-ds] "
2021-06-07 07:18:40 -04:00
echo -e "
Optional Arguments:
2022-12-10 09:00:23 -05:00
-s Start immediately
-d Don' t auto-update prefsCleaner.sh"
}
download_file( ) { # expects URL as argument ($1)
declare -r tf = $( mktemp)
$DOWNLOAD_METHOD " ${ tf } " " $1 " & >/dev/null && echo " $tf " || echo '' # return the temp-filename or empty string on error
2021-06-07 07:18:40 -04:00
}
2018-04-24 08:47:50 -04:00
fFF_check( ) {
# there are many ways to see if firefox is running or not, some more reliable than others
# this isn't elegant and might not be future-proof but should at least be compatible with any environment
2020-02-10 11:47:17 -05:00
while [ -e lock ] ; do
2021-06-07 07:18:40 -04:00
echo -e "\nThis Firefox profile seems to be in use. Close Firefox and try again.\n" >& 2
2021-09-27 11:23:12 -04:00
read -r -p "Press any key to continue."
2018-04-24 08:47:50 -04:00
done
}
2022-12-10 09:00:23 -05:00
## returns the version number of a prefsCleaner.sh file
get_prefsCleaner_version( ) {
echo " $( sed -n '5 s/.*[[:blank:]]\([[:digit:]]*\.[[:digit:]]*\)/\1/p' " $1 " ) "
}
## updates the prefsCleaner.sh file based on the latest public version
update_prefsCleaner( ) {
declare -r tmpfile = " $( download_file 'https://raw.githubusercontent.com/arkenfox/user.js/master/prefsCleaner.sh' ) "
[ -z " $tmpfile " ] && echo -e "Error! Could not download prefsCleaner.sh" && return 1 # check if download failed
[ [ $( get_prefsCleaner_version " $SCRIPT_FILE " ) = = $( get_prefsCleaner_version " $tmpfile " ) ] ] && return 0
mv " $tmpfile " " $SCRIPT_FILE "
chmod u+x " $SCRIPT_FILE "
2022-12-10 09:35:48 -05:00
" $SCRIPT_FILE " " $@ " -d
2022-12-10 09:00:23 -05:00
exit 0
}
2018-04-24 08:47:50 -04:00
fClean( ) {
# the magic happens here
prefs = "@@"
2018-04-25 16:56:54 -04:00
prefexp = "user_pref[ ]*\([ ]*[\"']([^\"']+)[\"'][ ]*,"
2018-04-24 08:47:50 -04:00
while read -r line; do
if [ [ " $line " = ~ $prefexp && $prefs != *" @@ ${ BASH_REMATCH [1] } @@ " * ] ] ; then
prefs = " ${ prefs } ${ BASH_REMATCH [1] } @@ "
fi
2021-09-28 15:24:54 -04:00
done <<< " $( grep -E " $prefexp " user.js) "
2018-04-24 08:47:50 -04:00
while IFS = '' read -r line || [ [ -n " $line " ] ] ; do
if [ [ " $line " = ~ ^$prefexp ] ] ; then
if [ [ $prefs != *" @@ ${ BASH_REMATCH [1] } @@ " * ] ] ; then
echo " $line "
fi
else
echo " $line "
fi
done < " $1 " > prefs.js
}
2021-06-07 07:18:40 -04:00
fStart( ) {
if [ ! -e user.js ] ; then
fQuit 1 "user.js not found in the current directory."
elif [ ! -e prefs.js ] ; then
fQuit 1 "prefs.js not found in the current directory."
fi
fFF_check
2022-07-30 08:22:32 -04:00
mkdir -p prefsjs_backups
bakfile = " prefsjs_backups/prefs.js.backup. $( date +"%Y-%m-%d_%H%M" ) "
2021-06-07 07:18:40 -04:00
mv prefs.js " ${ bakfile } " || fQuit 1 " Operation aborted.\nReason: Could not create backup file $bakfile "
echo -e " \nprefs.js backed up: $bakfile "
echo "Cleaning prefs.js..."
fClean " $bakfile "
fQuit 0 "All done!"
}
2022-12-10 09:00:23 -05:00
while getopts "sd" opt; do
case $opt in
s)
QUICKSTART = true
; ;
d)
AUTOUPDATE = false
; ;
esac
done
## change directory to the Firefox profile directory
cd " $( dirname " ${ SCRIPT_FILE } " ) "
2023-10-07 06:33:49 -04:00
# Check if running as root and if any files have the owner/group as root/wheel.
if [ " ${ EUID :- " $( id -u) " } " -eq 0 ] ; then
fQuit 1 "You shouldn't run this with elevated privileges (such as with doas/sudo)."
elif [ -n " $( find ./ -user 0 -o -group 0) " ] ; then
printf ' It looks like this script was previously run with elevated privileges,
you will need to change ownership of the following files to your user:\n '
find . -user 0 -o -group 0
fQuit 1
fi
2022-12-10 09:35:48 -05:00
[ " $AUTOUPDATE " = true ] && update_prefsCleaner " $@ "
2022-12-10 09:00:23 -05:00
2018-04-24 08:47:50 -04:00
echo -e "\n\n"
echo " ╔══════════════════════════╗"
echo " ║ prefs.js cleaner ║"
echo " ║ by claustromaniac ║"
2023-10-07 06:33:49 -04:00
echo " ║ v2.0 ║"
2018-04-24 08:47:50 -04:00
echo " ╚══════════════════════════╝"
echo -e "\nThis script should be run from your Firefox profile directory.\n"
echo "It will remove any entries from prefs.js that also exist in user.js."
echo "This will allow inactive preferences to be reset to their default values."
echo -e "\nThis Firefox profile shouldn't be in use during the process.\n"
2021-06-07 07:18:40 -04:00
2022-12-10 09:00:23 -05:00
[ " $QUICKSTART " = true ] && fStart
echo -e "\nIn order to proceed, select a command below by entering its corresponding number.\n"
2021-06-07 07:18:40 -04:00
2018-04-24 08:47:50 -04:00
select option in Start Help Exit; do
case $option in
Start)
2021-06-07 07:18:40 -04:00
fStart
2018-04-24 08:47:50 -04:00
; ;
Help)
2021-06-07 07:18:40 -04:00
fUsage
2018-04-24 08:47:50 -04:00
echo -e "\nThis script creates a backup of your prefs.js file before doing anything."
echo -e "It should be safe, but you can follow these steps if something goes wrong:\n"
echo "1. Make sure Firefox is closed."
echo "2. Delete prefs.js in your profile folder."
echo "3. Delete Invalidprefs.js if you have one in the same folder."
echo "4. Rename or copy your latest backup to prefs.js."
echo "5. Run Firefox and see if you notice anything wrong with it."
echo "6. If you do notice something wrong, especially with your extensions, and/or with the UI, go to about:support, and restart Firefox with add-ons disabled. Then, restart it again normally, and see if the problems were solved."
2020-09-15 07:55:28 -04:00
echo -e "If you are able to identify the cause of your issues, please bring it up on the arkenfox user.js GitHub repository.\n"
2018-04-24 08:47:50 -04:00
; ;
Exit)
fQuit 0
; ;
esac
done
2022-12-10 09:00:23 -05:00
fQuit 0