WaniKani SRS grid

try to take over the world!

目前為 2016-08-25 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         WaniKani SRS grid
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  try to take over the world!
// @author       You
// @match        https://www.wanikani.com/dashboard
// @require       https://code.jquery.com/jquery-1.12.4.js
// @require     https://code.jquery.com/ui/1.12.0/jquery-ui.js
// @grant        none
// ==/UserScript==

(function() {
    'use strict';
    var apiKey = localStorage.getItem('apiKeyForSRS');
    var srsGridDisplay = localStorage.getItem('srsGridDisplay');
    if(srsGridDisplay === null){
        srsGridDisplay = "center/center";
    }
    settings(apiKey,srsGridDisplay);
    if(apiKey !== null && apiKey !== "") {
        Api(apiKey);
    } else {
        notSetup();
    }
        var css =
        '.title {' +
        '  width: 25px;' +
        '}' +
        '.progressDetailTable {' +
        '  width: 100%;' +
        '}' +
        '.progressDetailTable td {' +
        '  color: white;' +
        '}' +
        '.popover.srs {' +
        ' display: none !important;' +
        '}' +
        '.progressDetailTableTDFirst {' +
        ' text-align: ' + srsGridDisplay.split("/")[0] +';' +
        ' padding-right: 10px;' +
        '}' +
        '.progressDetailTableTDSecond {' +
        ' text-align: ' + srsGridDisplay.split("/")[1] +';' +
        '}' +
        '.progressDetailTableTDFirst, .progressDetailTableTDSecond {' +
        ' color: white;' +
        '}' +
        '#btnSRSGrisSettings {' +
        ' position: relative;' +
        ' top: 5px;' +
        ' left: 5px;' +
        ' cursor: pointer;' +
        ' text-shadow: 2px 2px #584e4e;' +
        '}' +
        '#btnSRSGrisSettings.error {' +
        ' color: red;' +
        ' text-shadow: 2px 2px #584e4e;' +
        '}' +
        '#txtApiKey {' +
        ' width: 275px;' +
        '}' +
        '.srs-progress li table {' +
        ' display: inline;' +
        '}';

    //-------------------------------------------------------------------
    // Add a <style> section to the document.
    //-------------------------------------------------------------------
    function addStyle(aCss) {
        var head, style;
        head = document.getElementsByTagName('head')[0];
        if (head) {
            style = document.createElement('style');
            style.setAttribute('type', 'text/css');
            style.textContent = aCss;
            head.appendChild(style);
            return style;
        }
        return null;
    }
    addStyle(css);
    $('head').append('<link rel="stylesheet" href="https://code.jquery.com/ui/1.12.0/themes/base/jquery-ui.css" type="text/css" />');
})();

function noApi(){
    $.each($('[rel="popover-srs"]'),function(item,value){
        var total = $(value).find('span:first').text();
        var type = properCase($(value).attr('id'));
        var data = $(value).attr('data-content');
        var radicals = $(data).find("li:eq(0) span").text();
        var kanji = $(data).find("li:eq(1) span").text();
        var vocab = $(data).find("li:eq(2) span").text();
        var outTable = $("<table>").addClass('progressDetailTable');
        outTable.append(("<tr><td class='progressDetailTableTDFirst'>Radicals</td><td class='progressDetailTableTDSecond'>" + radicals + "</td></tr>"));
        outTable.append(("<tr><td class='progressDetailTableTDFirst'>Kanji</td><td class='progressDetailTableTDSecond'>" + kanji + "</td></tr>"));
        outTable.append(("<tr><td class='progressDetailTableTDFirst'>Vocabulary</td><td class='progressDetailTableTDSecond'>" + vocab + "</td></tr>"));
        outTable.append(("<tr><td class='progressDetailTableTDFirst'>Total</td><td class='progressDetailTableTDSecond'>" + total + "</td></tr>"));
        $(value).html("<span>" + type + "</span>").append(outTable);
        $('.dashboard section.srs-progress ul li').css('padding','10px 22.5px 10px');
    });
}

function Api(apiKey){
    $.getJSON('https://www.wanikani.com/api/user/' + apiKey + '/srs-distribution', function (data) {
        setTimeout(function () {
            if (data.error) {
                alert('API Error: ' + data.error.message);
            } else {
                var apprenticeTable = "<table>" +
                    "<tr><td class='progressDetailTableTDFirst'>Radicals</td><td class='progressDetailTableTDSecond'>" + data.requested_information.apprentice.radicals + "</td></tr>" +
                    "<tr><td class='progressDetailTableTDFirst'>Kanji</td><td class='progressDetailTableTDSecond'>" + data.requested_information.apprentice.kanji + "</td></tr>" +
                    "<tr><td class='progressDetailTableTDFirst'>Vocabulary</td><td class='progressDetailTableTDSecond'>" + data.requested_information.apprentice.vocabulary + "</td></tr>" +
                    "<tr><td class='progressDetailTableTDFirst'>Total</td><td class='progressDetailTableTDSecond'>" + data.requested_information.apprentice.total + "</td></tr>" +
                    "</table>";
                var guruTable = "<table>" +
                    "<tr><td class='progressDetailTableTDFirst'>Radicals</td><td class='progressDetailTableTDSecond'>" + data.requested_information.guru.radicals + "</td></tr>" +
                    "<tr><td class='progressDetailTableTDFirst'>Kanji</td><td class='progressDetailTableTDSecond'>" + data.requested_information.guru.kanji + "</td></tr>" +
                    "<tr><td class='progressDetailTableTDFirst'>Vocabulary</td><td class='progressDetailTableTDSecond'>" + data.requested_information.guru.vocabulary + "</td></tr>" +
                    "<tr><td class='progressDetailTableTDFirst'>Total</td><td class='progressDetailTableTDSecond'>" + data.requested_information.guru.total + "</td></tr>" +
                    "</table>";
                var masterTable = "<table>" +
                    "<tr><td class='progressDetailTableTDFirst'>Radicals</td><td class='progressDetailTableTDSecond'>" + data.requested_information.master.radicals + "</td></tr>" +
                    "<tr><td class='progressDetailTableTDFirst'>Kanji</td><td class='progressDetailTableTDSecond'>" + data.requested_information.master.kanji + "</td></tr>" +
                    "<tr><td class='progressDetailTableTDFirst'>Vocabulary</td><td class='progressDetailTableTDSecond'>" + data.requested_information.master.vocabulary + "</td></tr>" +
                    "<tr><td class='progressDetailTableTDFirst'>Total</td><td class='progressDetailTableTDSecond'>" + data.requested_information.master.total + "</td></tr>" +
                    "</table>";
                var enlightenedTable = "<table>" +
                    "<tr><td class='progressDetailTableTDFirst'>Radicals</td><td class='progressDetailTableTDSecond'>" + data.requested_information.enlighten.radicals + "</td></tr>" +
                    "<tr><td class='progressDetailTableTDFirst'>Kanji</td><td class='progressDetailTableTDSecond'>" + data.requested_information.enlighten.kanji + "</td></tr>" +
                    "<tr><td class='progressDetailTableTDFirst'>Vocabulary</td><td class='progressDetailTableTDSecond'>" + data.requested_information.enlighten.vocabulary + "</td></tr>" +
                    "<tr><td class='progressDetailTableTDFirst'>Total</td><td class='progressDetailTableTDSecond'>" + data.requested_information.enlighten.total + "</td></tr>" +
                    "</table>";
                var burnedTable = "<table>" +
                    "<tr><td class='progressDetailTableTDFirst'>Radicals</td><td class='progressDetailTableTDSecond'>" + data.requested_information.burned.radicals + "</td></tr>" +
                    "<tr><td class='progressDetailTableTDFirst'>Kanji</td><td class='progressDetailTableTDSecond'>" + data.requested_information.burned.kanji + "</td></tr>" +
                    "<tr><td class='progressDetailTableTDFirst'>Vocabulary</td><td class='progressDetailTableTDSecond'>" + data.requested_information.burned.vocabulary + "</td></tr>" +
                    "<tr><td class='progressDetailTableTDFirst'>Total</td><td class='progressDetailTableTDSecond'>" + data.requested_information.burned.total + "</td></tr>" +
                    "</table>";
                $('.srs-progress #apprentice').html("<span>Apprentice</span>").append(apprenticeTable);
                $('.srs-progress #guru').html("<span>Guru</span>").append(guruTable);
                $('.srs-progress #master').html("<span>Master</span>").append(masterTable);
                $('.srs-progress #enlightened').html("<span>Enlightened</span>").append(enlightenedTable);
                $('.srs-progress #burned').html("<span>Burned</span>").append(burnedTable);
                $('.dashboard section.srs-progress ul li').css('padding','10px 22.5px 10px');
            }
        }, 0);
    });
}

function properCase(word){
    return word.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
}

function settings(apiKey,srsGridDisplay){
    var SRSGridSettings = "<div id='divSRSGridSettings'>" +
        '<table>' +
        '<tr><td><span>API Key:</span></td><td><input type="textbox" id="txtApiKey"></input></td></tr>' +
        //'<tr><td colspan="2" style="float: right;"><input type="button" id="btnSRSGridSettingsUpdate"></input></td></tr>' +
        '<tr><td>Display Mode:<span></td><td><select id="ddlDisplayMode"><option value="Right/Left">Right/Left</option><option value="Center/Center">Center/Center</option></select></td></tr>';
    $('section.progression').after(SRSGridSettings);
    $('#txtApiKey').val(apiKey);
    $('#ddlDisplayMode').val(srsGridDisplay);
    $('#divSRSGridSettings').dialog({
				autoOpen: false,
				height: 300,
				width: 400,
				modal: true,
				buttons: {
					"Save": function () {
                        localStorage.setItem('apiKeyForSRS', $('#txtApiKey').val());
                        localStorage.setItem('srsGridDisplay', $('#ddlDisplayMode option:selected').val());
						$(this).dialog("close");
					},
					Cancel: function () {
						$(this).dialog("close");
					}
				}
			});
    $('.srs-progress ul').before('<div style="position: absolute;"><i id="btnSRSGrisSettings" class="link icon-gear" title="Change SRS Grid Settings" onclick="$(\'#divSRSGridSettings\').dialog(\'open\')"></i></div>');
}

function notSetup(){
    $( "#btnSRSGrisSettings" ).addClass('error').tooltip({content: "SRS Grid needs to be setup!", position: { my: "left+15 center", at: "right center" }}).tooltip( "option", "show", { effect: "blind", duration: 800 } ).tooltip( "open" );
}