firefox-bookmarks.sh 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #!/usr/bin/env bash
  2. # Script that lists bookmarks from a Firefox profile directory without Firefox.
  3. # Should work with IceCat, IceWeasel, ABrowser, LibreWolf etc.
  4. # by Adnan Shameem (adnan360); License: MIT (Expat)
  5. #
  6. # Usage:
  7. # - Make sure you have sqlite installed on your system
  8. # - Edit the 'profiles_path=' line below to match your browser/path
  9. # - Run the script: ./firefox-bookmarks.sh
  10. # Extracts and prints bookmarks from a Firefox or based browser (such as IceCat, ABrowser, LibreWolf etc.)
  11. # Params:
  12. # - $1: Profile dir, e.g. "~/.librewolf/k2fdb24c.default"
  13. function extract_bookmarks() {
  14. places_db=$(find "${1}/" -name "places.sqlite")
  15. # Ref: https://www.sqlite.org/uri.html#the_uri_path
  16. places_db=$(echo "$places_db" | sed 's|?|%3f|' | sed 's|#|%23|' | sed 's|\/\/|\/|')
  17. # Ref:
  18. # https://wiki.mozilla.org/Places/Places_SQL_queries_best_practices#Querying_multiple_tables
  19. # https://gist.github.com/iafisher/d624c04940fa46c6d9afb26cb1bf222a
  20. sql_query="SELECT b.title, h.url FROM moz_places h JOIN moz_bookmarks b ON h.id = b.fk WHERE b.type = 1 AND b.title IS NOT NULL"
  21. if [ -z "$(command -v sqlite3)" ]; then
  22. echo 'Error: sqlite3 binary is not found on system. Maybe sqlite is not installed.'; exit 8723
  23. fi
  24. # Ref: https://sleeplessbeastie.eu/2014/01/02/how-to-open-firefox-bookmarks-from-openbox-menu/
  25. # "immutable=1" is used as a workaround to access locked database. Ref:
  26. # https://www.sqlite.org/uri.html#recognized_query_parameters
  27. sqlite3 -separator '^' "file:${places_db}?immutable=1" "$sql_query" | while IFS=^ read title url; do
  28. echo "$title -> $url"
  29. done
  30. }
  31. # Enter the dir where the file profiles.ini lives
  32. # - IceCat: "$HOME/.mozilla/icecat/"
  33. # - Firefox/IceWeasel: "$HOME/.mozilla/firefox/"
  34. # - ABrowser (Trisquel): "$HOME/.mozilla/abrowser/"
  35. # - LibreWolf: "$HOME/.librewolf/"
  36. profiles_path="$HOME/.mozilla/icecat/"
  37. # Pick the profile dir
  38. if [ -z "$(grep 'Default=' "${profiles_path}/profiles.ini")" ]; then
  39. profile_dir_name=$(sed -n -e 's|Path=\(.*\)|\1|p' "${profiles_path}/profiles.ini" | head -n 1)
  40. else
  41. # "{8,}" is to avoid "Default=1" line. This looks for a value of 8 or more
  42. # characters. 8 is the usual profile dir name length.
  43. profile_dir_name=$(sed -n -e 's|Default=\(.\{8,\}\)|\1|p' "${profiles_path}/profiles.ini" | head -n 1)
  44. fi
  45. # Determine the full profile dir
  46. profile_dir="${profiles_path}/${profile_dir_name}"
  47. # Finally, show the bookmarks!
  48. extract_bookmarks "$profile_dir"