WaniKani SRS grid

try to take over the world!

当前为 2016-08-25 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         WaniKani SRS grid
// @namespace    http://tampermonkey.net/
// @version      0.25
// @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 useApiKey = localStorage.getItem('useApiKeyForSRS');
    var srsGridDisplay = localStorage.getItem('srsGridDisplay');
    debugger;
    if(srsGridDisplay === null){
        srsGridDisplay = "center/center";
    }
    settings(apiKey,useApiKey,srsGridDisplay);
    if(useApiKey == "false") {
        noApi();
    } else if(useApiKey == "true" && 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,useApiKey,srsGridDisplay){
    var SRSGridSettings = "<div id='divSRSGridSettings'>" +
        '<table>' +
        '<tr><td><span>Use API:</span></td><td><input type="checkbox" id="chkUseApi"></input></td></tr>' +
        '<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>' +
        '</table>' +
        '<br/><span>Using API Key is recommended.  But if you want to keep API calls to a minimum, non-API mode may work.</span><br/><span>Other UserScipts can cause issues with non-API mode.</span>';
    $('section.progression').after(SRSGridSettings);
    if(useApiKey == "true"){
        $('#chkUseApi').prop('checked','checked');
    }
    $('#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('useApiKeyForSRS', $('#chkUseApi').prop('checked'));
                        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" );
}