Real-time Anki import from your browser with Rikaisama on Arch Linux

One of the best tools for learning Japanese (or any language, really) is the spaced repetition program Anki. It is available for a lot of platforms, it's free and open source (GPL3) and can synchronize your learning progress across devices.

Another great tool is the Firefox add-on Rikaisama. Based on the more popular Rikaichan, it's a dictionary for looking up Japanese words directly in your browser. Rikaisama adds a lot more advanced functionality, like using EPWING dictionaries, querying Japanese web dictionaries and the feature I'm going to talk about here, adding words to your Anki deck automatically, complete with reading and translation.

If you do a lot of reading on the web, this can nearly completely eliminate the tedious process of creating an Anki deck. I find that, in my experience, it also encourages you to add more words to your deck than you would have without it, because it's just a button press and not several copy-and-pastes.

On the technical side, this works by installing an Anki plugin that makes Anki listen on a local UDP port for new cards. Rikaisama in turn starts a Python script that writes the cards to this UDP port when called.

And here's where the problem begins for Arch Linux users. Most distribution ship with Python 2 as a standard, but Arch Linux uses the slightly incompatible Python 3. You can install Python 2 on Arch Linux, but the script inside of Rikaisama will still refer to just 'python', which is Python 3 on Arch. It's easy to modify Rikaisama to use the right version, though. Rikaisama is provided as an .xpi file, which is a packed Firefox add-on. You can unpack the file (which is in fact a zip file) with 7zip:

$ 7z x Rikaisama_v20.3.xpi

You quick grep will tell you which file to change:

$ grep -r python
udp/!/usr/bin/env python2
udp/ "$1" $2 "$3"

As you see, udp/ doesn't use the correct python version. Change the line to read

python2 "$1" $2 "$3"

and you're done. Now you can re-pack the file for use in Firefox. In the folder with the source files, pack the files with 7zip:

$ 7z a ../Rikaisama.fixed.xpi * -r

You can now use Rikaisama.fixed.xpi to install the add-on, it will use the correct python version and you can finally import directly into Anki.

You can find a version of Rikaisama containing this change on GitHub.