WaniKani Lesson Ordering II

This allows you to sort the items in your WaniKani lessons, either putting the Radicals and Kanji first, or the Vocabulary first. By RhosVeedcy, based on scripts originally by Alucardeck.

目前为 2014-03-28 提交的版本。查看 最新版本

// ==UserScript==
// @name          WaniKani Lesson Ordering II
// @namespace     https://www.wanikani.com
// @description   This allows you to sort the items in your WaniKani lessons, either putting the Radicals and Kanji first, or the Vocabulary first. By RhosVeedcy, based on scripts originally by Alucardeck.
// @version 1.2
// @include       https://www.wanikani.com/lesson/session
// @include       http://www.wanikani.com/lesson/session
// @grant       none
// ==/UserScript==
function get(id) {
    if (id && typeof id === 'string') {
        id = document.getElementById(id);
    }
    return id || null;
}

function simulateKeyPressA() {
	var f4Event = document.createEvent("KeyboardEvent");
	var evtTarget = get("lesson");
	f4Event.initKeyEvent(
		"keydown", // event type
		true, // if your event can bubble
		true, // if your event is cancelable
		window, // abstract view (an obscure DOM requirement)
		false, // if ctrlKey is down
		false, // if altKey is down
		false, // if shiftKey is down
		false, // if the "meta" key is down (usually, the Windows key)
		65, // the keyCode -- 65 is keyCode for “a”
		65 // the charCode 
	);
	evtTarget.dispatchEvent(f4Event);
	console.log('simulateKeyPressA() keydown');
	var f4Event2 = document.createEvent("KeyboardEvent");
	f4Event2.initKeyEvent(
		"keyup", // event type
		true, // if your event can bubble
		true, // if your event is cancelable
		window, // abstract view (an obscure DOM requirement)
		false, // if ctrlKey is down
		false, // if altKey is down
		false, // if shiftKey is down
		false, // if the "meta" key is down (usually, the Windows key)
		65, // the keyCode -- 65 is keyCode for “a”
		65 // the charCode 
	);
	evtTarget.dispatchEvent(f4Event2);
	console.log('simulateKeyPressA() keyup');
}

function init(){
	console.log('init() start');
	var stats = $("#stats")[0];
    var t = document.createElement('div');
    var t2 = document.createElement('div');
    stats.appendChild(t);
    stats.appendChild(t2);
    
    t.innerHTML = '<div id="divSt">Click below to reorder lesson items</div>'+
        '<button id="reorderBtn" type="button" onclick="window.dispatchEvent(new Event(\'reorderRKevt\'));">Radicals and Kanji first</button>'+
        '</div>';
    t2.innerHTML = '<button id="reorderVBtn" type="button" onclick="window.dispatchEvent(new Event(\'reorderVevt\'));">Vocabulary items first</button>'+
        '</div>';
	window.addEventListener('reorderRKevt',reorder); 
	window.addEventListener('reorderVevt',reorderV); 
	console.log('init() end');
}

function hideButtons(){
    var reorderBtn = get("reorderBtn");
    var reorderVBtn = get("reorderVBtn");
    reorderBtn.style.visibility="hidden";
    reorderVBtn.style.visibility="hidden";
}

function reorder(){
    console.log('reorder() start');
    //var divSt = get("divSt");
    //hideButtons();
    //divSt.innerHTML = 'Reordering.. please wait!';

    var actList = $.jStorage.get("l/activeQueue");
    var lesList = $.jStorage.get("l/lessonQueue");
    
    var removedCount = 0;
    for(var i=0;i<actList.length;i++){
        var it = actList[i];
        actList.splice(i--,1);
        lesList.push(it);
        removedCount++;
    }
    console.log('Items removed from ActiveQueue: '+removedCount);
    
    for(var i=lesList.length-1;i>=0;i--){
        var it=lesList[i];
        if(it.kan){
           lesList.splice(i,1);
           lesList.push(it);
        }
    }
    for(var i=lesList.length-1;i>=0;i--){
        var it=lesList[i];
        if(it.rad){
           lesList.splice(i,1);
           lesList.push(it);
        }
    }
    
    for(var i=0;i<removedCount;i++){
        actList.push(lesList.pop());
    }

    lesList.reverse();
        
    console.log('Ordered LessonQueue:');
    for(var i=0;i<lesList.length;i++){
        var it=lesList[i];
        if(it.rad)
        	console.log('rad '+it.rad);
        else if(it.kan)
           	console.log('kan '+it.kan);
        else if(it.voc)
            console.log('voc '+it.voc);
    }
    
    $.jStorage.set("l/lessonQueue",lesList);
    $.jStorage.set("l/activeQueue",actList);
    for(var i=0;i<actList.length;i++){
	simulateKeyPressA();
    }
    divSt.innerHTML = 'Ordered: radicals and kanji first';
    hideButtons();
    console.log('reorder() end');
}

function reorderV(){
    console.log('reorderV() start');
    //var divSt = get("divSt");
    //hideButtons();
    //divSt.innerHTML = 'Reordering.. please wait!';

    var actList = $.jStorage.get("l/activeQueue");
    var lesList = $.jStorage.get("l/lessonQueue");
    
    var removedCount = 0;
    for(var i=0;i<actList.length;i++){
        var it = actList[i];
        actList.splice(i--,1);
        lesList.push(it);
        removedCount++;
    }
    console.log('Items removed from ActiveQueue: '+removedCount);
    
    for(var i=lesList.length-1;i>=0;i--){
        var it=lesList[i];
        if(it.voc){
           lesList.splice(i,1);
           lesList.push(it);
        }
    }
    
    for(var i=0;i<removedCount;i++){
        actList.push(lesList.pop());
    }
        
    lesList.reverse();

    console.log('Ordered LessonQueue:');
    for(var i=0;i<lesList.length;i++){
        var it=lesList[i];
        if(it.rad)
        	console.log('rad '+it.rad);
        else if(it.kan)
           	console.log('kan '+it.kan);
        else if(it.voc)
            console.log('voc '+it.voc);
    }
    
    $.jStorage.set("l/lessonQueue",lesList);
    $.jStorage.set("l/activeQueue",actList);
    for(var i=0;i<actList.length;i++){
	simulateKeyPressA();
    }
    divSt.innerHTML = 'Ordered: vocabulary first';
    hideButtons();
    console.log('reorderV() end');
}

init();
console.log('script load end');