Chrome bookmarks in JSON format
Unlike browsing history, Chrome bookmarks are found in JSON format. It's
stored in the same directory as the SQLite database file, and for OS X, the
Chrome bookmarks can be organized in folders, so naturally the JSON representation of the bookmarks also has the corresponding recursive, hierarchical structure. If it was flat and simpler, we could have considered using jq, a handy command-line JSON processor, for processing the data but this case is a little beyond the scope of the tool's capability and we would have to resort to a scripting language with proper JSON support.
I chose Ruby, one that I'm most familiar with, for processing the JSON data and formatting it as the input to fzf. I'm not going to delve into the details of the implementation. It's just a basic JSON processing that can be written in any language and there's not much going on. Only that I took special care for CJK wide characters.
The complete script can be found here.
What's the most interesting about the script is that it starts as a bash
script, but turns into a Ruby script. The reason the code is organized so is
that I did not want to maintain two files, a Ruby script for processing JSON
and a bash script for gluing it with fzf. Instead I used this special
ruby command, which makes the interpreter ignore the lines in the
#!ruby appears, so I can safely prepend the bash script to the
A few more things to mention:
- The script is for OS X and it will simply exit when you're not on OS X. It will also try to install fzf using Homebrew when it's not found. (#5-6)
- Notice the
vim: set filetype=ruby:on the second line. It makes Vim see the file as a Ruby script even though it starts with the shebang line for bash. (#1-2)
- The use of
/usr/bin/rubyis intentional. Most Ruby developers switch between various Rubies using RVM or rbenv and you do not want to accidentally start heavyweight JRuby for this and the system default Ruby is good enough for our purpose. (#8)
- I used
-u 30%option to open fzf in a new tmux pane above the current pane. I like the layout as it remotely reminds me of the original bookmark bar of Chrome. (#9)
- There's an explicit
exitto prevent bash from interpreting the Ruby section of the file. (#13)