WaniKani SRS grid

try to take over the world!

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

您需要先安裝使用者腳本管理器擴展,如 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      2.2
// @description  try to take over the world!
// @author       You
// @match        https://www.wanikani.com/
// @match        https://www.wanikani.com/dashboard
// @require      https://greasyfork.org/scripts/22751-wanikani-settings/code/WaniKani%20Settings.js?version=144744
// @grant        none
// ==/UserScript==

(function() {
    'use strict';
    makeSettings("SRS Grid",{1: {Name: "apiKeyForSRS", Display: "Api Key", Type: "textbox"},
                             2: {Name: "srsGridDisplay", Display: "Display", Type: "select", Options: {a: {Value: "left/right", Text: "Left/Right"}, b: {Value: "center/center", Text: "Center/Center"}}, Default: "center/center"}});

    var apiKey = getSetting('apiKeyForSRS');
    var srsGridDisplay = getSetting('srsGridDisplay');
    if(apiKey !== null && apiKey !== "") {
        Api(apiKey);
    } else {
        notSetup();
    }
    addStyle('.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;' +
        '}' +
        '#txtApiKey {' +
        ' width: 275px;' +
        '}' +
        '.srs-progress li table {' +
        ' display: inline;' +
        '}' +
        '#divSRSGridLink.error {' +
        '  background-color: red;' +
        '}' +
        '.dashboard section.srs-progress.noClean td {' +
        '  font-size: 15px;' +
        '}' +
        '.dashboard section.srs-progress.noClean ul li {' +
        '  display: table-cell !important;' +
        '  padding: 10px 0px !important;' +
        '  width: 500px;'+
        '}' +
        '.dashboard section.srs-progress.noClean ul li span {' +
        '  margin-bottom: 10px !important;' +
        '}' +
        '.dashboard section.srs-progress ul li:first-child {' +
        '  border-radius: 5px 0px 0px 5px !important;' +
        '}' +
        '.dashboard section.srs-progress ul li:last-child {' +
        '  border-radius: 0px 5px 5px 0px !important;' +
        '}');
    killClean();
})();

    //-------------------------------------------------------------------
    // 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;
}

function killClean(){
    if($('.dashboard section.srs-progress ul li').css('display') == 'inline-flex') {
        if($.browser.mozilla === true){
            $('.srs-progress').addClass('noClean');
        } else {
            //Erase conflicting portions of Clean Dashboard CSS: Thanks to rfindley
            var found_css = false;
            var styles = $('style').each(function(i,v){
                if (found_css) return;
                css = $(v).html();
                if (css.match(/-- SRS PROGRESS --/) === null) return;
                found_css = true;
                var css_arr = css.split('\n');
                var len = css_arr.length;
                css = '';
                var deleting = false;
                for (var idx=0; idx<len; idx++) {
                    var line = css_arr[idx];
                    if (!deleting) {
                        if (line.match(/-- SRS PROGRESS --/) !== null) {
                            deleting = true;
                        } else {
                            css += line + '\n';
                        }
                    } else {
                        if (line.match(/-- REVIEW STATUS --/) !== null) {
                            css += line + '\n';
                            deleting = false;
                        }
                    }
                }
                $(v).html(css);
            });
        }
    }
}

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 notSetup(){
    if($('.dropdown.account.open').length === 0){
        $('.dropdown.account').addClass('open');
    }
    $('#divSRSGridLink').addClass('error');
}