prefsCleaner.sh 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #!/usr/bin/env bash
  2. ## prefs.js cleaner for Linux/Mac
  3. ## author: @claustromaniac
  4. ## version: 1.1
  5. ## special thanks to @overdodactyl and @earthlng for a few snippets that I stol..*cough* borrowed from the updater.sh
  6. currdir=$(pwd)
  7. ## get the full path of this script (readlink for Linux, greadlink for Mac with coreutils installed)
  8. sfp=$(readlink -f "${BASH_SOURCE[0]}" 2>/dev/null || greadlink -f "${BASH_SOURCE[0]}" 2>/dev/null)
  9. ## fallback for Macs without coreutils
  10. if [ -z "$sfp" ]; then sfp=${BASH_SOURCE[0]}; fi
  11. ## change directory to the Firefox profile directory
  12. cd "$(dirname "${sfp}")"
  13. fQuit() {
  14. ## change directory back to the original working directory
  15. cd "${currdir}"
  16. echo -e "\n$2"
  17. exit $1
  18. }
  19. fFF_check() {
  20. # there are many ways to see if firefox is running or not, some more reliable than others
  21. # this isn't elegant and might not be future-proof but should at least be compatible with any environment
  22. while [ -e webappsstore.sqlite-shm ]; do
  23. echo -e "\nThis Firefox profile seems to be in use. Close Firefox and try again.\n"
  24. read -p "Press any key to continue."
  25. done
  26. }
  27. fClean() {
  28. # the magic happens here
  29. prefs="@@"
  30. prefexp="user_pref[ ]*\([ ]*[\"']([^\"']+)[\"'][ ]*,"
  31. while read -r line; do
  32. if [[ "$line" =~ $prefexp && $prefs != *"@@${BASH_REMATCH[1]}@@"* ]]; then
  33. prefs="${prefs}${BASH_REMATCH[1]}@@"
  34. fi
  35. done <<< "`grep -E \"$prefexp\" user.js`"
  36. while IFS='' read -r line || [[ -n "$line" ]]; do
  37. if [[ "$line" =~ ^$prefexp ]]; then
  38. if [[ $prefs != *"@@${BASH_REMATCH[1]}@@"* ]]; then
  39. echo "$line"
  40. fi
  41. else
  42. echo "$line"
  43. fi
  44. done < "$1" > prefs.js
  45. }
  46. echo -e "\n\n"
  47. echo " ╔══════════════════════════╗"
  48. echo " ║ prefs.js cleaner ║"
  49. echo " ║ by claustromaniac ║"
  50. echo " ║ v1.1 ║"
  51. echo " ╚══════════════════════════╝"
  52. echo -e "\nThis script should be run from your Firefox profile directory.\n"
  53. echo "It will remove any entries from prefs.js that also exist in user.js."
  54. echo "This will allow inactive preferences to be reset to their default values."
  55. echo -e "\nThis Firefox profile shouldn't be in use during the process.\n"
  56. select option in Start Help Exit; do
  57. case $option in
  58. Start)
  59. if [ ! -e user.js ]; then
  60. fQuit 1 "user.js not found in the current directory."
  61. elif [ ! -e prefs.js ]; then
  62. fQuit 1 "prefs.js not found in the current directory."
  63. fi
  64. fFF_check
  65. bakfile="prefs.js.backup.$(date +"%Y-%m-%d_%H%M")"
  66. mv prefs.js "${bakfile}" || fQuit 1 "Operation aborted.\nReason: Could not create backup file $bakfile"
  67. echo -e "\nprefs.js backed up: $bakfile"
  68. echo "Cleaning prefs.js..."
  69. fClean "$bakfile"
  70. fQuit 0 "All done!"
  71. ;;
  72. Help)
  73. echo -e "\nThis script creates a backup of your prefs.js file before doing anything."
  74. echo -e "It should be safe, but you can follow these steps if something goes wrong:\n"
  75. echo "1. Make sure Firefox is closed."
  76. echo "2. Delete prefs.js in your profile folder."
  77. echo "3. Delete Invalidprefs.js if you have one in the same folder."
  78. echo "4. Rename or copy your latest backup to prefs.js."
  79. echo "5. Run Firefox and see if you notice anything wrong with it."
  80. 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."
  81. echo -e "If you are able to identify the cause of your issues, please bring it up on ghacks-user.js GitHub repository.\n"
  82. ;;
  83. Exit)
  84. fQuit 0
  85. ;;
  86. esac
  87. done