Looking for generic user script to help with enhancing lesson information

Hi user-script coding gurus,

I’ve been working on a user script to add an extra section to the vocabulary reading explanation (related to rendaku). The plan is for the extra section to appear on the vocab, lesson and reviews pages (for reviews, specifically when showing the reading explanation). I looked through some existing scripts to see how to do this, and from that I’ve put something together which is mostly working.

It occurs to me that part of this is a very generic task - i.e. to add some section to the reading or meaning info for an item, and have that appear in all the relevant places, formatted as another subsection at the same level as e.g. “Reading Notes” or “Reading Explanation“, etc. An existing example would be the phonetic-semantic composition script.

If the generic part of this exists, then I would then just have to provide a function which gives my custom section info (title and contents - actually a list of these since there might be none or more than one extra sections). The function would receive the item name, type (radical, kanji or vocab) and context (meaning or reading) and return the list of custom info sections.

Does a generic wrapper of this kind exist already? If not, has one of the experts here considered writing such a thing? I’m working on my own version, but I’m new to tampermonkey and the internals of the wanikani pages, so it is a bit of a hack to figure out where/when for the script to modify the pages. I’m sure that someone with more experience will do a better job than me (and that perhaps it is not too hard a job). It would be nice if this generic component was part of a “maintained” support scripts package so that it could be kept up to date with any changes in the internals of the wanikani page formatting.

Any help/input appreciated… Thanks!

No idea about the internals but you might want to check out this? If memory serves I think it added extra sections for its stuff to different parts of the site. Not sure if it’s what you’re looking for but maybe it’ll help.

:slight_smile:


Seriously though, @nonymouse, the creator of the script you referred to (and @Raionus linked to) is very friendly and may be willing to help you make that part of their script into a generic reusable script.

The referenced Semantic Phonetic Composition script is fairly abstracted out, so it may be hard to follow if you’re unfamiliar with js (you said you’re new to Tampermonkey, so I don’t know if this extends to js).

However, the main functions you’d want to be looking at are:

  • createKeiseiSection() in wk_keisei.html.js - this looks like it handles most of the shell HTML to actually hold your new section of content
  • injectKeiseiSection() in wk_keisei.main.js - handles a handful of logic that mostly references createKeiseiSection(), but there’s some HTML checks in there you may find useful
  • populateKeiseiSection() in wk_keisei.main.js - continues building the content by referencing functions like populateCharGrid() and populateMoreInfoFold()

I’m not sure if that’s how I would implement or if that’s even where I would start, but since that script has been mentioned, those are some of the functions to focus on if you do reference that script.

However, in general, you can easily figure out the basics of how a page is structured by just using inspect element in Chrome (or other inferior browser).

For example (and keep in mind I’m level 1 so I don’t even have kanji yet and I’m sure the lists of kanji for a radical get more complex as one progresses, so I can’t really see these sections for reference), when you inspect element on the “Name Mnemonic” section of a radical, you can see it comprises three main components:

  1. A <section> tag
  2. An <h2> tag that serves as the header
  3. A <p> tag for the main content

And then if I want to find out how specific things are styled or structured, I can just look at their HTML so I can see that the name of a radical is highlighted in the mnemonic section by using a <span> with the class “radical-highlight”.

Once you’ve figured out these things, it’s pretty easy to generate the HTML in whatever way you’re comfortable with as a browser extension, Tampermonkey script, etc. The majority of the work in more complex scripts is organizing and fetching the right data, which is what most of that Semantic Phonetic Composition script does, making it a bit difficult to sift through if you’re just trying to figure out how to render some HTML.

On the other hand, it would be useful to have a broad HTML generator function like SPC has so that if/when the structure of WaniKani does change, you only need to modify your code in two places:

  1. In the code that generates the HTML/structure of your actual section
  2. In the code that inserts your section into the WaniKani DOM
1 Like

The page detection in my scripts is already pretty generic, it is located in a module named WKInteraction. Detecting whether for example the “readings” page in a lesson is available is not so straight-forward because the content is loaded dynamically. In that file you can see how to use observers to watch a page for changes yourself, but you can also re-use the module. WKInteraction sends out events, for example when there is a good moment to add a new section (that’s why it is not so easy to find out how it works).

You can see in keisei.main.js how you can use WKInteraction, look for lines with this.wki. Basically you register a callback for the event, in this case injectKeiseiSection(), which checks for the page type and puts all the content that comes out of createKeiseiSection() at the right spot.

$(document).on(`${GM_info.script.namespace}_wk_subject_ready`,
                       this.injectKeiseiSection.bind(this));

[The populateKeiseiSection() is yet in another spot because changes in the options can change the content, so you can refresh without tearing everything down.]


It is not super-straight forward to use because I had to patch some troubles along the way (I didn’t want a dependency, so every script ships its own version of WKInteraction in a namespace), and I was considering to somehow add it into the WK Framework if there is interest and I have some time.

1 Like

Thanks everyone and thanks acm2010. With the additional explanation I will look at how to reuse the existing module.

I think it would be really useful to add this to the WK Framework.

Ok, so I’m quite excited because I seem to have this working. The userscript is here, if anyone would try it out I’d be really grateful.

https://raw.githubusercontent.com/jameshippisley/wanikani/master/user_scripts/rendaku_information.js

I was inspired by the recent Tofugu article on rendaku (Rendaku: Why Hito-Bito isn't Hito-Hito) especially the onyomi vs kunyomi thing, and I found that I wanted to be able to apply the rules from the article quickly as I went through my lessons/reviews. I went through the wanikani database with a python script and checked each reading of each vocab to see whether it could rendaku (in any position) and whether it did rendaku. I then applied the rules (mostly, onyomi vs kunyomi, but also Lyman’s law, some phonetic stuff, and a few custom things) to see which cases fit the rules and which don’t. I found about 2700 possible rendaku cases, of which all but ~170 fit the rules (at least, my current rendering of those rules). The rules in my script can undoubtedly be further customized to explain the exceptions, but ~170/2700 exceptions feels pretty good and should help me get through my reviews I hope!

It’s my first tampermonkey/javascript thingy so I’m not sure how to “release” it, and I’m cautious about security - i.e. that somebody could maliciously edit the version of the script on github to make it do something bad, so if anybody has any feedback on that and whether this is ready for release, or anything, please let me know.

I can also release the python scripts which generate the raw data in case anybody else might enhance them. I have concerns there too since my local scripts currently include data files which are basically a dump of wanikani, and I wonder if people generally avoid committing that to a public repository to respect wanikani copyright?

Thanks!