[Userscript] Leech training

This used to be another script, written by hitechbunny, but it stopped being maintained. I will now crib the other script’s description:

The script is here: https://greasyfork.org/en/scripts/372086-shin-wanikani-leech-trainer

The script will put together a lesson of 10 leeches. It will exclude any leech that is too recent (less than 24 hours since the last review), or too old (less than half the time between reviews remaining), or already trained. Any trained leech that is subsequently reviewed incorrectly goes back in the pool.

This is more of less what it looks like (this animation is slightly out of date):

Every leech is repeated 3 times in the lesson. To clear a leech you must get it right each time. Furthermore, the lesson will include up to 3 items related to the leech. You must get those right too in order to clear the leech.

Leech training data is stored on the server-side. So you can switch browsers or computers without losing track of your progress.

As always bug reports and suggestions for improvements welcome. Enjoy!

PS This script steals heavily from the awesome [Userscript] Self-Study Quiz If you haven’t installed that script you should go ahead and do it now.


Amazing script! It works especially well with those leeches that are too similar with others, thanks to the addition of three items that are similar but not the same as the leeches that are being reviewed. Awesome script!


I’m afraid that an update of WaniKani broke this userscript. There’s no one API key in version 2 anymore, but up to 16 “personal access tokens” (see the new section in the settings menu). Because of that, when the function get_api_key() in your script looks for the element with id user_api_key_v2, it doesn’t find anything, an rejected promise gets returned, but that promise isn’t handled: Unhandled promise rejection Error: "generate_apikey".

If you used this userscript before the change, it’ll probably continued to work, because it remembers the API key if it found one previously (not sure if the old API v2 keys even work anymore). But for new users, it’s unfortunately broken.

1 Like

I’m confused how to test this script. I have @rfindley self study and @seanblue filter installed.

Which one should I disable and enable?

1 Like

Cool, thabks for letting me know, I must have a cached key. I think I have time this evening to update it.


Very quick ‘n’ dirty update made that just grabs the default, read-only token for now. I need to make another pass at understanding what the server-side code is doing and refactor that a bit. One of the issues is that there’s an explosion of database rows as more people use it, at which point I start pushing up against what the free heroku tier allows, and so some pruning needs to happen. Time to reshuffle where I spend my time on some personal projects.


You’ll need to enable “Shin Wanikani Leech Trainer” in that userscript list.

Hmm. I think this one still isn’t working for me (a relatively new user), even with the update. It just does the same ten leeches over and over, even if I get them all correct. The leeches to be trained number doesn’t change after I finish training them. Until at some point, after I’ve completed some reviews, it suddenly changes and I have a different ten leeches to train.

That’s really weird, not heard of anyone having that before. Which browser are you using, and how happy are you with looking around in the developer tools?

Pushed a small update, 2.3.0. Only noticeable change is the behaviour when you get one wrong. I was forever getting double incorrect answers by hitting return in an attempt to dismiss the error message. Now if you get one wrong, pressing return will dismiss the message, clear the input field, and leave the focus in it.

1 Like

It is really weird. I assumed the script was just broken. But then someone on a forum mentioned using the same one and being able to get down to zero leeches, so I made sure I had the most updated version but it still isn’t working. I’m using Chrome. I can look around but I don’t know much about the developer tools. I updated it and tried again. I started with 79 leeches. I trained six of them. And I still have 79 leeches.

Okay. If you open the Chrome menu from the three dots in the top right, go to “More Tools” and then “Developer Tools” you should see the developer tools appear. If you try a leech session with that open and see if anything appears in the “console” tab that could be useful.

The less subtle approach would be to go to the “Application” tab in the dev tools, expand the Storage -> Local Storage menu and look for “www.wanikani.com”. Once you’ve got that, look for a key called “leech_train_cache”, delete it, refresh the page and try again.

There appear to be several errors upon starting a leech session.

and several more errors upon finishing the leech session.

It seems to be having issues with the api key. In going into the error detail of an error after I trained some leaches it says error: “apiKey = 507dae25-543e-495f-94d3-77bf5f8beb93 | url = https://api.wanikani.com/v2/study_materials?updated_after= | resp.StatusCode = 422 | resp.Status = 422 Unprocessable Entity | resp.Body = {“error”:“The updated_after parameter must be a valid ISO8601 format”,“code”:422}↵”

I tried expiring the key but it had the same error with the new key listed.

I’m not sure what’s going on with that. Turns out the server was having memory issues and falling over, so it could be coincidental, but could also explain all the Internal Server Error results you’re seeing.

I’m going to start pulling the code for the server apart soon and rewrite it myself. That’s no disrespect intended to the original developer, but I don’t know the language it’s written in and so debugging it isn’t straightforward.

1 Like

Hey all, the leech trainer has stopped clearing my recently trained leeches. :confused:

Ex. I complete a session and I receive the “X leeches trained!” message but the counter doesn’t reset to zero…and it won’t replace the set with any new leeches. I’ve been stuck with the same ones for about three days now. I’m running Firefox 68.0.1 and Tampermonkey on macOS Mojave.

Are there any known incompatibilities with other userscripts? The only thing I can think of that changed recently is that I installed the WaniKani Heatmap the other day.


I have just installed it and it’s not working :frowning:

If I click the button it just reloads the page, and it doesn’t show any number

Yeah, my bad. Give me a few minutes and I’ll put it into maintenance mode, fix the issue, then come back with a longer explanation.

Longer Explanation

Big red warning! I was running the server on a free tier on Heroku which allows for 10,000 rows in a database and if I try and store more I get some leeway and then I’m cut off. Until now, I’ve been “solving” the issue by, well, just emptying the database when it got full. This time I’ve upgraded it to a paid tier instead:

Hopefully 10,000,000 rows should keep everyone in leeches for a while to come, and buys me some time to finish the rewrite I’ve been slowly working on for a long time.


It’s a bit hacky, but if it bothers you that the Leeches button doesn’t turn grey when it hits zero, add this directly after line 209:

        if (!json.leeches_available) {
            $('.navigation-shortcut--leeches a span').css("background-color", "#aaa");

Awesome… but it still doesn’t work for me.

Is there anything I can do to fix it?

If you try the steps from here, that might give us some more info. The console might show some interesting stuff.