WK Auto Commit

Auto commit for Wanikani

Per 09-02-2016. Zie de nieuwste versie.

Voor het installeren van scripts heb je een extensie nodig, zoals Tampermonkey, Greasemonkey of Violentmonkey.

Voor het installeren van scripts heb je een extensie nodig, zoals {tampermonkey_link:Tampermonkey}.

Voor het installeren van scripts heb je een extensie nodig, zoals Tampermonkey of Violentmonkey.

Voor het installeren van scripts heb je een extensie nodig, zoals Tampermonkey of Userscripts.

Voor het installeren van scripts heb je een extensie nodig, zoals {tampermonkey_link:Tampermonkey}.

Voor het installeren van scripts heb je een gebruikersscriptbeheerder nodig.

(Ik heb al een user script manager, laat me het downloaden!)

Voor het installeren van gebruikersstijlen heb je een extensie nodig, zoals {stylus_link:Stylus}.

Voor het installeren van gebruikersstijlen heb je een extensie nodig, zoals {stylus_link:Stylus}.

Voor het installeren van gebruikersstijlen heb je een extensie nodig, zoals {stylus_link:Stylus}.

Voor het installeren van gebruikersstijlen heb je een gebruikersstijlbeheerder nodig.

Voor het installeren van gebruikersstijlen heb je een gebruikersstijlbeheerder nodig.

Voor het installeren van gebruikersstijlen heb je een gebruikersstijlbeheerder nodig.

(Ik heb al een beheerder - laat me doorgaan met de installatie!)

// ==UserScript==
// @name         WK Auto Commit
// @namespace    WKAUTOCOMMIT
// @version      0.3
// @description  Auto commit for Wanikani
// @author       Johannes Mikulasch
// @match        http://www.wanikani.com/review/session*
// @match        https://www.wanikani.com/review/session*
// @match        http://www.wanikani.com/lesson/session*
// @match        https://www.wanikani.com/lesson/session*
// @grant        none
// @run-at       document-end
// @license      
// ==/UserScript==

/*
 * WK Auto Commit
 * If you typed in the correct answer then it is automatically commited.
 * Therefore, you have to use the 'enter' key way less than before.
 *
 * Version 0.35
 *  Edit by WillNiels to stop auto committing critical items
 * Version 0.3
 *  Script works now on the Lessons page too
 * Version 0.2
 *  Makes script work with Greasemonkey and Firefox
 * Version 0.1
 *  Initial version
 *
 */


/* jshint -W097 */
'use strict';

var activated = true;
var click_threshold = 600;

var on_lessons_page = false;

var api_key;
var crit_list = [];
var critical = false;

//Anything less than this will not be autosubmitted!
var percentCritical = '95';

var detect_lessons_page = function() {
    // Returns true if on lessons page
    var current_url = window.location.href;
    var lessonsPattern = /^http[s]?:\/\/www.wanikani.com\/lesson\/session.*/;
    return lessonsPattern.test(current_url);
};

var toggle = function () {
    if (activated) {
        // Deactivates WK Auto Commit mode
        $("#WKAUTOCOMMIT_button").prop('title', "Switch auto commit on");
        $("#WKAUTOCOMMIT_button").css({"opacity":"0.5"});
        $("#WKAUTOCOMMIT_button").text("Auto Commit is off");
        activated = false;
    } else {
        // Activates WK Auto Commit mode
        $("#WKAUTOCOMMIT_button").prop('title', "Switch auto commit off");
        $("#WKAUTOCOMMIT_button").css({"opacity":"1.0"});
        $("#WKAUTOCOMMIT_button").text("Auto Commit is on");
        activated = true;
    }
};

var sanitize = function (str1) {
    var str2 = str1.replace(/\s/g, ''); // Removes Whitespaces
    str2 = str2.toLowerCase();
    return str2;
};

var commit = function () {
    console.log(crit_list);
    $("#answer-form form button").click();
    setTimeout(function(){ $("#answer-form form button").click();}, click_threshold);
};

var check_input = function () {

    if (on_lessons_page) {
        var currentItem = $.jStorage.get("l/currentQuizItem");
        var currentquestiontype = $.jStorage.get("l/questionType");
    } else {
        var currentItem = $.jStorage.get("currentItem");
        var currentquestiontype = $.jStorage.get("questionType");
        // If the item is critical, don't auto submit
        var thisitem = (currentItem.rad || currentItem.voc || currentItem.kan);
        for( var i in crit_list ){
            if( crit_list[i] == thisitem ){
                critical = true;
            }else{
                critical = false;
            }
        }
    }

    var currentresponse = $("#user-response").val();

    var currentitem_response = null;

    // Get possible responses from current item depending on the task (reading or meaning)
    if (currentquestiontype === "meaning") {
        currentitem_response = currentItem.en;
        if (currentItem.syn) {
            currentitem_response = currentitem_response.concat(currentItem.syn);
        }
    } else if (currentquestiontype === "reading") {
        if (currentItem.voc) { // Vocab word
            currentitem_response = currentItem.kana;
        } else if (currentItem.emph === 'kunyomi') { // Kanji: Kun reading
            currentitem_response = currentItem.kun;
        } else if (currentItem.emph === 'onyomi') { // Kanji: On reading 
            currentitem_response = currentItem.on;
        } else {
            console.log("WK Auto Commit: Could not find response");
        }
    }

    for (var i in currentitem_response) {
        if (sanitize(currentresponse) === sanitize(currentitem_response[i]) && critical == false) {
            commit();
        } 
    }
};

var register_check_input = function () {
    $("#user-response").on("keyup", function (event) {    
        if (activated) {
            check_input();
        }
    });
};

var addButtons = function () {

    $("<div />", {
        id : "WKAUTOCOMMIT_button",
        title : "Toggle Auto Commit Mode",
    })
        .text("Auto Commit is on")
        .css({"background-color":"#C55"})
        .css({"opacity":"1"})
        .css({"display":"inline-block"})
        .css({"font-size":"0.8125em"})
        .css({"color":"#FFF"})
        .css({"cursor":"pointer"})
        .css({"padding":"10px"})
        .css({"vertical-align":"bottom"})
        .on("click", toggle)
        .prependTo("footer");
};

var init = function () {  
    console.log('WK Auto Commit (a plugin for Wanikani): Initialization started');
    on_lessons_page = detect_lessons_page();
    addButtons();
    register_check_input();
    
    if(!localStorage.getItem('apiKey')){
        api_key = prompt('enter your api-key');
    }else{
        api_key = localStorage.getItem('apiKey');
    }

    while (typeof api_key !== 'string' || api_key.length !== 32){
        api_key = prompt('Api-key incorrect format: Please re-enter your api-key.', api_key);
    }

    localStorage.setItem('apiKey',api_key);
    
    crit_list = [];
    
    $.getJSON('/api/user/'+api_key+'/critical-items/' + percentCritical, function(json){
        if (json.error && json.error.code === 'user_not_found') {
            localStorage.removeItem('apiKey');
        }
        $(json.requested_information).each(function(i,v){
            try {
                var thing = v.character
                crit_list.push(thing);
            } catch(e) {}
        });
    });
    
    
    console.log('WK Auto Commit: Initialization ended');
};

$(function(){
    init();
});