[For Userscript Authors] WK Item Info Injector

Version 3.11:

  • Updated to work with recent lesson start URL change and fixed some layout issues
// @require      https://greasyfork.org/scripts/430565-wanikani-item-info-injector/code/WaniKani%20Item%20Info%20Injector.user.js?version=1416982

I’m not following the WaniKani developments that closely anymore, so I only found out about the recent changes this morning. Sorry for the delay. If anything still does not work, let me know.

3 Likes

Sorry, it looks like wkItemInfo.currentState.reading is now returning [undefined].
It used to contain a list of readings, even when the user has not yet expanded the itemInfo.

To reproduce, just go to any vocab review and type wkItemInfo.currentState into your browser console. Expectation is that reading contains the readings and not [undefined].

2 Likes

Here’s a temporary quick and dirty fix for reviews. Just swap out the existing _updateCurrentStateReview function to the following.

	function _updateCurrentStateReview() {
		let subjects               = JSON.parse((_getRootElement().querySelector(`[data-quiz-queue-target="subjects"]`) ?? document.querySelector(`[data-quiz-queue-target="subjects"]`)).textContent);
		let currentId              = parseInt(document.querySelector(`[data-subject-id]`)?.dataset.subjectId ?? subjects[0]?.id);
		let currentItem            = subjects.find(s => s.id === currentId) ?? getController(`quiz-queue`).quizQueue.activeQueue.find(b => b.id == currentId);

		_currentState.id           = currentItem.id;
		_currentState.type         = _firstCharToLower(currentItem.type);
		_currentState.characters   = currentItem.characters;
		_currentState.emphasis     = currentItem.primary_reading_type || currentItem.readings?.find(r => r.kind === `primary`)?.type;
		_currentState.partOfSpeech = [..._getRootElement().querySelectorAll(`.subject-section__meanings h2`)].find(h => h.textContent === `Word Type`)?.nextElementSibling.textContent.split(`,`).map(p => p.trim().replace(/\b\w/g, c => c.toUpperCase()));
		_currentState.meanings     = currentItem.meanings;
		_currentState.meaning      = currentItem.meanings?.filter(r => [`primary`, `alternative`].includes(r.kind)).map(m => m.text);
		_currentState.onyomi       = currentItem.readings?.filter(r => r.type === `onyomi`).map(r => r.text);
		_currentState.kunyomi      = currentItem.readings?.filter(r => r.type === `kunyomi`).map(r => r.text);
		_currentState.nanori       = currentItem.readings?.filter(r => r.type === `nanori`).map(r => r.text);
		_currentState.composition  = [..._getRootElement().querySelectorAll(`.subject-section--components .subject-character__characters`)].map(s => { let result = {meaning: [...s.nextElementSibling.querySelectorAll(`.subject-character__meaning`)].map(m => m.textContent.trim()), reading: [...s.nextElementSibling.querySelectorAll(`.subject-character__reading`)].map(r => r.textContent.trim()), characters: s.textContent.trim()}; if (result.reading.length === 0) delete result.reading; if (result.characters === ``) delete result.characters; return result; });
		_currentState.readings     = currentItem.readings;
		_currentState.reading      = currentItem.readings?.filter(r => [`primary`, `alternative`].includes(r.kind)).map(r => r.text);
        if (_currentState.reading?.length === 0) _currentState.reading = currentItem[_currentState.emphasis];
		Object.entries(_currentState).filter(e => !e[1]).forEach(e => delete _currentState[e[0]]);
        for (const prop of [`onyomi`, `kunyomi`, `nanori`, `composition`, `reading`, `meaning`]) {
            if (_currentState[prop]?.length === 0) delete _currentState[prop];
        }

		_updateCurrentStateUnder();
	}

I made a few assumptions about what form the data should be in, but I tried to cross reference it with the format retrieved in item pages and lessons, so it should be a good start. It’s somewhat of a backwards compatibility hack, while also now including the new structures under the plural forms of the properties (i.e., readings and meanings).
And @Sinyaven can make modifications as necessary.

2 Likes

Thanks for letting me know about the WK change, and thanks @Inserio for posting a fix. I have based my update on your proposal, but I decided to not add readings and meanings. I want to only include properties that I can provide in reviews, lessons AND item pages.


Version 3.12:

  • Fixed missing values in currentState after a recent WK update
// @require      https://greasyfork.org/scripts/430565-wanikani-item-info-injector/code/WaniKani%20Item%20Info%20Injector.user.js?version=1492607
3 Likes

Version 3.13:

  • Fixed extraction of characters from HTML on item pages
  • Changed some CSS to match page design
// @require      https://greasyfork.org/scripts/430565-wanikani-item-info-injector/code/WaniKani%20Item%20Info%20Injector.user.js?version=1673042

Seems like there’s an issue on lessons pages.

In particular, _getRootElement().querySelector(`[id=user_synonyms]`) in _updateCurrentStateLesson() is null by the time it is called.
This happens when the user refreshes the page on the lessons page or opens them in a new tab, and in my experience it (and/or some other issue) results in an endless page refresh loop.

Turned off instant-inject mode in Tampermonkey and the issue persists.

I’m not able to reproduce this problem. Since the element with id user_synonyms is already part of the initial html, it cannot be a timing issue. So my next best guess was, that my e.detail.newBody + MutationObserver hack is failing once more. But this hack only applies to turbo page navigations, not to page refreshes, so I guess this can be ruled out as well.

Maybe there is a conflict between scripts? What other scripts are you using?

image
So, this is why I didn’t want to bring that up :wink:
The fact that the page refreshes constantly makes my troubleshooting annoyingly difficult.

Buuut, if you can’t reproduce it, then I dunno. I guess you can table it until if/when someone else has the same issue.

Hey, I wanted to know if you could add the ability to get the onyomi of an item on the lesson page, if it exists, where they teach you the kunyomi.

Because currently your script returns ‘none’ there because I think this info doesn’t exist on the page. But on other pages like the item info or quizzes the onyomi exists.

Or if you don’t want to add this, do you have an idea how I could access this info on the lesson page?