WaniKani Review Audio Tweak

allow audio to be played after review meaning questions, when reading has been previously answered correctly. also includes enable autoplay when incorrect setting (default: off).

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name          WaniKani Review Audio Tweak
// @namespace     https://www.wanikani.com
// @description   allow audio to be played after review meaning questions, when reading has been previously answered correctly. also includes enable autoplay when incorrect setting (default: off).
// @version       0.1.0
// @include       http*://www.wanikani.com/review/session*
// @run-at        document-end
// @grant         none
// ==/UserScript==

/*global $, console, additionalContent*/

(function () {
    'use strict';
    // SETTINGS //
    var enableAutoPlayWhenIncorrect = false; // change to 'true' to enable
    /////
    function itemStat(item) {
        var itemStatKey = (item.voc ? "v" : item.kan ? "k" : "r") + item.id;
        return ($.jStorage.get(itemStatKey) || {});
    }
    additionalContent.audio = function (audioAutoplay) {
        var buttonElem, liElem, currentItem, questionType, audioElem;
        currentItem = $.jStorage.get("currentItem");
        questionType = $.jStorage.get("questionType");
        $("audio").remove();
        if (currentItem.aud && (questionType === "reading" || itemStat(currentItem).rc >= 1)) {
            liElem = $("#option-audio");
            buttonElem = liElem.find("button");
            if (!enableAutoPlayWhenIncorrect && !$("#answer-form fieldset").hasClass("correct")) {
                audioAutoplay = false;
            }
            buttonElem.removeAttr("disabled");
            audioElem = $("<audio></audio>", { autoplay: audioAutoplay }).appendTo(liElem.removeClass("disabled").children("span"));
            $("<source></source>", {
                src: "https://s3.amazonaws.com/s3.wanikani.com/audio/" + currentItem.aud,
                type: "audio/mpeg"
            }).appendTo(audioElem);
            $("<source></source>", {
                src: "https://s3.amazonaws.com/s3.wanikani.com/audio/" + currentItem.aud.replace(".mp3", ".ogg"),
                type: "audio/ogg"
            }).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").trigger("play");
                }
            });
        }
    };
    console.log('WaniKani Review Audio Tweak: script load end');
}());