WaniKani Review Search

Do a WaniKani Search on your review answer. By RhosVeedcy.

Mint 2014.04.06.. Lásd a legutóbbi verzió

// ==UserScript==
// @name          WaniKani Review Search
// @namespace     https://www.wanikani.com
// @description   Do a WaniKani Search on your review answer.  By RhosVeedcy.
// @version 1.2
// @include       https://www.wanikani.com/review/session
// @grant       none
// ==/UserScript==



function get(id) {
    if (id && typeof id === 'string') {
        id = document.getElementById(id);
    }
    return id || null;
}



function init(){
	console.log('init() start');
	var footer = document.getElementsByTagName('footer');
	
	footer[0].innerHTML = '<button id="hook_button" type="button" onclick="window.dispatchEvent(new Event(\'evtWKSearch\'));">Search</button>' +
	                      footer[0].innerHTML;
	window.addEventListener('evtWKSearch',doWKSearch); 

	window.addEventListener("keypress", hook_hotkey);

	console.log('init() end');
}


function hook_hotkey( e ) {

	//console.log("hook_hotkey");

	var ansbox = get("user-response");

	//console.log(ansbox);
	//console.log(e);

	if (e && ansbox && e.target != ansbox) {

		var thechar = e.which || e.keyCode;
		if (thechar == 83 || thechar == 115) {    // 's' key

			doWKSearch();
		}
	}
}



var sText = "";
var sWindow = null;



function copytext(){

	var qin;

	if (sWindow && sWindow.document) {

		qin = sWindow.document.getElementById("query");
	}

	if (qin) {

		qin.focus();
		qin.value = sText;
	}

	sWindow = null;
	sText = "";
}



function doWKSearch(){

    sText = get("user-response").value;
    sWindow = window.open("https://www.wanikani.com/about");

    if (sWindow) {
    	sWindow.addEventListener("load", copytext, false);
    }
}


init();
console.log('WK Rvw Srch load end');