[Unsupported] [Userscript] WaniKani Review Audio Tweak (reupload/fixed)

@Kumirei If you haven’t noticed any issues with your tweaked version can you post it here? Much appreciated!

The only issue I have had is that it sometimes picks the less common reading when an item has more than one reading. It surprises me when I enter the only reading I know and it plays a different reading. For me this is more of a feature, since I get familiar with other readings, but it could be considered a serious issue by some.

Either way, here’s the code. I believe I only added that check for the preferred VA, and the fix for multiple audio being played at the same time.

// ==UserScript==
// @name          WaniKani Review Audio Tweak 2
// @namespace     https://www.wanikani.com
// @description   Allow audio to be played after review meaning questions, when reading has been previously answered correctly. Also includes setting for enabling autoplay when answer is incorrect (default: off). Originally by Takuya Kobayashi.
// @author        seanblue
// @version       1.0.1
// @include       https://www.wanikani.com/review/session*
// @run-at        document-end
// @grant         none
// ==/UserScript==

// Original version by Takuya Kobayashi: https://greasyfork.org/en/scripts/10184-wanikani-review-audio-tweak
// This version is a reupload with the new URL for audio files.

(function ($) {
	'use strict';
	// BEGIN SETTINGS //
	const enableAutoPlayWhenIncorrect = true; // change to 'true' to enable
	// END SETTINGS //

	function itemStat(item) {
		let itemStatKey = (item.voc ? 'v' : item.kan ? 'k' : 'r') + item.id;
		return ($.jStorage.get(itemStatKey) || {});
	}

	window.additionalContent.audio = function (audioAutoplay) {
		let currentItem = $.jStorage.get('currentItem');
		let questionType = $.jStorage.get('questionType');

		$('audio').remove();

		if (currentItem.aud && (questionType === 'reading' || itemStat(currentItem).rc >= 1)) {
			let liElem = $('#option-audio');
			let buttonElem = liElem.find('button');

			if (!enableAutoPlayWhenIncorrect && !$('#answer-form fieldset').hasClass('correct')) {
				audioAutoplay = false;
			}

			buttonElem.removeAttr('disabled');
			let audioElem = $('<audio></audio>', { autoplay: audioAutoplay }).appendTo(liElem.removeClass('disabled').children('span'));

			for (let i = 0; i < currentItem.aud.length; i++) {
				let audio = currentItem.aud[i];

				if (audio.voice_actor_id == WaniKani.default_voice_actor_id) {
					$('<source></source>', {
						src: audio.url,
						type: audio.content_type
					}).appendTo(audioElem);
				}
			}

			audioElem[0].addEventListener('play', function () {
				buttonElem.removeClass('audio-idle').addClass('audio-play');
			});

			audioElem[0].addEventListener('ended', function () {
				buttonElem.removeClass('audio-play').addClass('audio-idle');
			});

			buttonElem.off('click');
			buttonElem.on('click', function () {
				audioElem[0].play();
			});

			liElem.off('click');
			liElem.on('click', function () {
				if ($('#user-response').is(':disabled')) {
					$('audio.preferred').trigger('play');
				}
			});
		}
	};
}(window.jQuery));
1 Like

I can’t get this script to work. I disabled all other scripts (except WK open framework) in case something was conflicting with it but that didn’t seem to make a difference.

Are there settings for this script because the icon I go to normally to adjust script settings during reviews doesn’t show any menu item for this script. Thanks in advance for any assistance!

This script is not integrated with WKOF and only has one setting in the script at the top of the code.

I don’t know why it wouldn’t work, as it works for me. What exactly is not working?

Nevermind, I really should spend more time tinkering with things before asking for help. Problem was related to audio hardware… this is why the first step of troubleshooting should be to ask if everything is plugged in lol.

2 Likes

Happens to all of us 278113942751150080

Thanks, I’ve updated the script in greasyfork. I’m going to officially stop supporting this script now though. Of course if I’m still around I’ll still take someone else’s fix and update the script for everyone’s convenience, but I won’t try to fix this script myself anymore.

1 Like

I’ll be happy to fix things as long as I am using it.

1 Like

This is not active on the preview page

I’m not planning to enable any of my scripts on the preview pages by default. I’d recommend just enabling it in your settings for the script.

1 Like

ukKr5

Why?

Well, when I open the preview site my main goal is to check out the new WaniKani design, and I don’t want my scripts interfering with that by default.

1 Like

True, but you can do it in a different browser.

I’d like to test integration of scripts on preview, and to edit javascript for 30 of them is laborious task.

I believe all scripts should work on preview, the dev community just not aware of it, and there is no formal peer review/approval process.

Saying all of that, I’m using bunch of your scripts, and I’m very grateful you took time to add them snd support them!

1 Like

In the Tampermonkey settings for a script you can enable it for a new URL without editing the script. You’d still have to do this once for each script, but it’s safer than editing the script. If you edit the script itself, updates to that script would overwrite your changes.

My point was not about how doable or feasible to test scripts on preview, but about default/standard for WK scripts in general. Most of developers just did not think about, but you’re done it on purpose, hence I’ve tried to convince you otherwise. I’ll stop now, since it’s open source and your decision as an author.

1 Like

@mochismiles Did you find this script?

2 Likes

I did not, thank you for letting me know about it!

1 Like

Question: I have a ~400 loc patch which adds a handful of features and configuration options:

  • preloads audio before question is answered to avoid lag in audio playing and caches previously loaded audio in memory for the duration of the quiz
  • ui for configuring settings via wkof (uses default settings if wkof not installed)
  • optional setting to randomize voice actor
  • optional setting so audio matches the reading you typed (matching WaniKani’s behavior)
  • optional setting so audio is different from the reading you typed
  • optional setting for audio to be random reading
  • broke out “autoplay on wrong answer” setting into separate settings for reading and meaning questions
  • fixes bug (also present in WaniKani’s own code) that could cause audio loading to fail if your browser does not support the first audio <source> (the spec/browsers require calling load() after adding all sources; if this isn’t done, only the first source will be checked)
  • changes “play” icon to circle with line through it if audio is unavailable after answering (e.g., for radicals)

Would you consider accepting a contribution along these lines or would it be better if I just post it on my own?

1 Like

That’s an impressive set of changes! Given how much is added (and that it adds a dependency on wkof, even if optional), it might be better for you to release your new and improved version separately. I wouldn’t be against putting your changes in this script per se, but I don’t really want to deal with the impact if there are bugs and further changes are needed. And if you upload the script yourself, you have a lot more flexibility and don’t have to deal with me either.

1 Like