user.js

mirror of https://github.com/arkenfox/user.js
Log | Files | Refs | README

prefsCleaner.sh (6137B)


      1 #!/usr/bin/env bash
      2 
      3 ## prefs.js cleaner for Linux/Mac
      4 ## author: @claustromaniac
      5 ## version: 2.1
      6 
      7 ## special thanks to @overdodactyl and @earthlng for a few snippets that I stol..*cough* borrowed from the updater.sh
      8 
      9 ## DON'T GO HIGHER THAN VERSION x.9 !! ( because of ASCII comparison in update_prefsCleaner() )
     10 
     11 readonly CURRDIR=$(pwd)
     12 
     13 ## get the full path of this script (readlink for Linux, greadlink for Mac with coreutils installed)
     14 SCRIPT_FILE=$(readlink -f "${BASH_SOURCE[0]}" 2>/dev/null || greadlink -f "${BASH_SOURCE[0]}" 2>/dev/null)
     15 
     16 ## fallback for Macs without coreutils
     17 [ -z "$SCRIPT_FILE" ] && SCRIPT_FILE=${BASH_SOURCE[0]}
     18 
     19 
     20 AUTOUPDATE=true
     21 QUICKSTART=false
     22 
     23 ## download method priority: curl -> wget
     24 DOWNLOAD_METHOD=''
     25 if command -v curl >/dev/null; then
     26 	DOWNLOAD_METHOD='curl --max-redirs 3 -so'
     27 elif command -v wget >/dev/null; then
     28 	DOWNLOAD_METHOD='wget --max-redirect 3 --quiet -O'
     29 else
     30 	AUTOUPDATE=false
     31 	echo -e "No curl or wget detected.\nAutomatic self-update disabled!"
     32 fi
     33 
     34 fQuit() {
     35 	## change directory back to the original working directory
     36 	cd "${CURRDIR}"
     37 	[ "$1" -eq 0 ] && echo -e "\n$2" || echo -e "\n$2" >&2
     38 	exit $1
     39 }
     40 
     41 fUsage() {
     42 	echo -e "\nUsage: $0 [-ds]"
     43 	echo -e "
     44 Optional Arguments:
     45     -s           Start immediately
     46     -d           Don't auto-update prefsCleaner.sh"
     47 }
     48 
     49 download_file() { # expects URL as argument ($1)
     50   declare -r tf=$(mktemp)
     51 
     52   $DOWNLOAD_METHOD "${tf}" "$1" &>/dev/null && echo "$tf" || echo '' # return the temp-filename or empty string on error
     53 }
     54 
     55 fFF_check() {
     56 	# there are many ways to see if firefox is running or not, some more reliable than others
     57 	# this isn't elegant and might not be future-proof but should at least be compatible with any environment
     58 	while [ -e lock ]; do
     59 		echo -e "\nThis Firefox profile seems to be in use. Close Firefox and try again.\n" >&2
     60 		read -r -p "Press any key to continue."
     61 	done
     62 }
     63 
     64 ## returns the version number of a prefsCleaner.sh file
     65 get_prefsCleaner_version() {
     66 	echo "$(sed -n '5 s/.*[[:blank:]]\([[:digit:]]*\.[[:digit:]]*\)/\1/p' "$1")"
     67 }
     68 
     69 ## updates the prefsCleaner.sh file based on the latest public version
     70 update_prefsCleaner() {
     71 	declare -r tmpfile="$(download_file 'https://raw.githubusercontent.com/arkenfox/user.js/master/prefsCleaner.sh')"
     72 	[ -z "$tmpfile" ] && echo -e "Error! Could not download prefsCleaner.sh" && return 1 # check if download failed
     73 
     74 	[[ $(get_prefsCleaner_version "$SCRIPT_FILE") == $(get_prefsCleaner_version "$tmpfile") ]] && return 0
     75 
     76 	mv "$tmpfile" "$SCRIPT_FILE"
     77 	chmod u+x "$SCRIPT_FILE"
     78 	"$SCRIPT_FILE" "$@" -d
     79 	exit 0
     80 }
     81 
     82 fClean() {
     83 	# the magic happens here
     84 	prefs="@@"
     85 	prefexp="user_pref[ 	]*\([ 	]*[\"']([^\"']+)[\"'][ 	]*,"
     86 	while read -r line; do
     87 		if [[ "$line" =~ $prefexp && $prefs != *"@@${BASH_REMATCH[1]}@@"* ]]; then
     88 			prefs="${prefs}${BASH_REMATCH[1]}@@"
     89 		fi
     90 	done <<< "$(grep -E "$prefexp" user.js)"
     91 
     92 	while IFS='' read -r line || [[ -n "$line" ]]; do
     93 		if [[ "$line" =~ ^$prefexp ]]; then
     94 			if [[ $prefs != *"@@${BASH_REMATCH[1]}@@"* ]]; then
     95 				echo "$line"
     96 			fi
     97 		else
     98 			echo "$line"
     99 		fi
    100 	done < "$1" > prefs.js
    101 }
    102 
    103 fStart() {
    104 	if [ ! -e user.js ]; then
    105 		fQuit 1 "user.js not found in the current directory."
    106 	elif [ ! -e prefs.js ]; then
    107 		fQuit 1 "prefs.js not found in the current directory."
    108 	fi
    109 
    110 	fFF_check
    111 	mkdir -p prefsjs_backups
    112 	bakfile="prefsjs_backups/prefs.js.backup.$(date +"%Y-%m-%d_%H%M")"
    113 	mv prefs.js "${bakfile}" || fQuit 1 "Operation aborted.\nReason: Could not create backup file $bakfile"
    114 	echo -e "\nprefs.js backed up: $bakfile"
    115 	echo "Cleaning prefs.js..."
    116 	fClean "$bakfile"
    117 	fQuit 0 "All done!"
    118 }
    119 
    120 
    121 while getopts "sd" opt; do
    122 	case $opt in
    123 		s)
    124 			QUICKSTART=true
    125 			;;
    126 		d)
    127 			AUTOUPDATE=false
    128 			;;
    129 	esac
    130 done
    131 
    132 ## change directory to the Firefox profile directory
    133 cd "$(dirname "${SCRIPT_FILE}")"
    134 
    135 # Check if running as root and if any files have the owner as root/wheel.
    136 if [ "${EUID:-"$(id -u)"}" -eq 0 ]; then
    137 	fQuit 1 "You shouldn't run this with elevated privileges (such as with doas/sudo)."
    138 elif [ -n "$(find ./ -user 0)" ]; then
    139 	printf 'It looks like this script was previously run with elevated privileges,
    140 you will need to change ownership of the following files to your user:\n'
    141 	find . -user 0
    142 	fQuit 1
    143 fi
    144 
    145 [ "$AUTOUPDATE" = true ] && update_prefsCleaner "$@"
    146 
    147 echo -e "\n\n"
    148 echo "                   ╔══════════════════════════╗"
    149 echo "                   ║     prefs.js cleaner     ║"
    150 echo "                   ║    by claustromaniac     ║"
    151 echo "                   ║           v2.1           ║"
    152 echo "                   ╚══════════════════════════╝"
    153 echo -e "\nThis script should be run from your Firefox profile directory.\n"
    154 echo "It will remove any entries from prefs.js that also exist in user.js."
    155 echo "This will allow inactive preferences to be reset to their default values."
    156 echo -e "\nThis Firefox profile shouldn't be in use during the process.\n"
    157 
    158 [ "$QUICKSTART" = true ] && fStart
    159 
    160 echo -e "\nIn order to proceed, select a command below by entering its corresponding number.\n"
    161 
    162 select option in Start Help Exit; do
    163 	case $option in
    164 		Start)
    165 			fStart
    166 			;;
    167 		Help)
    168 			fUsage
    169 			echo -e "\nThis script creates a backup of your prefs.js file before doing anything."
    170 			echo -e "It should be safe, but you can follow these steps if something goes wrong:\n"
    171 			echo "1. Make sure Firefox is closed."
    172 			echo "2. Delete prefs.js in your profile folder."
    173 			echo "3. Delete Invalidprefs.js if you have one in the same folder."
    174 			echo "4. Rename or copy your latest backup to prefs.js."
    175 			echo "5. Run Firefox and see if you notice anything wrong with it."
    176 			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."
    177 			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"
    178 			;;
    179 		Exit)
    180 			fQuit 0
    181 			;;
    182 	esac
    183 done
    184 
    185 fQuit 0