WK Custom Review Question (KunOn+)

Changes the text of the review question. Specifies ON or KUN reading for Kanji. Options to have the text in English or Japanese.

目前為 2015-02-22 提交的版本,檢視 最新版本

// ==UserScript==
// @name        WK Custom Review Question (KunOn+)
// @namespace   WK_CustomQuestion
// @description Changes the text of the review question. Specifies ON or KUN reading for Kanji. Options to have the text in English or Japanese.
// @author      hoovard
// @include     https://www.wanikani.com/review/session*
// @include     http://www.wanikani.com/review/session*
// @version     0.4.1
// @license     Do what you want with it (Preferably improve it).
// @grant       none
// ==/UserScript==

// Tested on the following system:
// Firefox 35.0.1 and Chrome 39.0.2171.95 (64-bit), Linux Mint 17.1 Cinnamon 64-bit
// Firefox 35.0.1 and Chrome 40.0.2214.115 m, Windows 8.1 64-bit

// Thanks to Rui Pinheiro for the original script and to ethan for the idea to use MutationObserver to detect changes in the DOM.


// Option to have the question text in:
// English (en)
// Japanese (ja)
var strLang = "en";

// Vars to compose the replacement question string
var strKanji;
var strRadical;
var strVocab;
var strMeaning;
var strReading;
var strOn;
var strKun;
var strName;

// Translations
switch (strLang)
{
	case "en":
		strKanji = "Kanji";
		strRadical = "Radical";
		strVocab = "Vocabulary";
		strMeaning = "Meaning";
		strReading = "yomi";
		strVocabReading = "Reading";
		strOn = "on'";
		strKun = "kun'";
		strName = "Name";
		break;
	case "ja":
		strKanji = "漢字";
		strRadical = "部首";
		strVocab = "単語";
		strMeaning = "意味";
		strReading = "読み";
		strVocabReading = "読み";
		strOn = "音";
		strKun = "訓";
		strName = "名前";
		break;
}

// Variable to save and check against the previous contents of the jStorage item
var savedCurrentItem;

// Code from Stack Overflow to detect changes in the DOM. 
// (http://stackoverflow.com/questions/3219758/detect-changes-in-the-dom/14570614#14570614)
var observeDOM = (function(){
    var MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver,
        eventListenerSupported = window.addEventListener;

    return function(obj, callback){
        if( MutationObserver ){
            // define a new observer
            var obs = new MutationObserver(function(mutations, observer){
                if( mutations[0].addedNodes.length || mutations[0].removedNodes.length )
                    callback();
            });
            // have the observer observe for changes in children
            obs.observe( obj, { childList:true, subtree:true });
        }
        else if( eventListenerSupported ){
            obj.addEventListener('DOMNodeInserted', callback, false);
            obj.addEventListener('DOMNodeRemoved', callback, false);
        }
    }
})();

// Callback function observing the 'question-type' div 'h1' element
var observeMe = $('#question-type h1')[0];
// console.log("observeMe: " + observeMe);	
observeDOM( observeMe ,function(){ 
    //console.log('dom changed');
    var curItem = $.jStorage.get("currentItem");
    // Make sure that the text has changed before calling 
    // the function to prevent infinite looping.
    if (curItem != savedCurrentItem) {
		savedCurrentItem = curItem;
		var questionType = $.jStorage.get("questionType");
		
		var strItemType = "";
		var strAnswerRequired = "";
		var readingType = "Reading";
		
		//console.log(curItem);		
		//console.log("emph: " + curItem.emph);
		
		// Compose the string elements to be sent into the div
		if ("kan" in curItem)
		{
			// Kanji
			strItemType = strKanji;
			if (questionType == "reading") {
				if(curItem.emph == "onyomi")
					readingType = strOn + strReading;
				else
					readingType = strKun + strReading;
			} else {
				readingType = strMeaning;
			}
		}
		else if ("voc" in curItem)
		{
			// Vocabulary
			strItemType = strVocab;
			if (questionType == "reading") {			
					readingType = strVocabReading;
			} else {
				readingType = strMeaning;
			}
		}
		else if ("rad" in curItem)
		{
			strItemType = strRadical;
			readingType = strName;
		}
		
		// replace the contents of #question-type h1
		switch (strLang)
		{
			case "en":
				$('#question-type h1').html(strItemType + ' <strong>' + readingType + '</strong>');
				break;
			case "ja":
				$('#question-type h1').html(readingType);
				break;					
		}
	}
	
});

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址