WaniKani Reviews Color Progress Bar And Real Percentage

try to take over the world!

// ==UserScript==
// @name         WaniKani Reviews Color Progress Bar And Real Percentage
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  try to take over the world!
// @author       You
// @match        https://www.wanikani.com/review/session
// @match        https://www.wanikani.com/
// @match        https://www.wanikani.com/dashboard
// @require      https://greasyfork.org/scripts/5392-waitforkeyelements/code/WaitForKeyElements.js?version=115012
// @require      https://greasyfork.org/scripts/369353-jigen-s-other-stuff/code/Jigen's%20other%20stuff.js?version=604095
// @grant        none
// ==/UserScript==

window.wk_reviewsColorProgressBar = {};

(function(global) {
    'use strict';

    if (!window.wkof) {
        if (confirm('WaniKani Reviews Color Progress Bar And Real Percentage requires Wanikani Open Framework.\nDo you want to be forwarded to the installation instructions?'))
            window.location.href = 'https://community.wanikani.com/t/instructions-installing-wanikani-open-framework/28549';
        return;
    }

    var settings_dialog;
    var defaults = {
        barHeight: 5,
        swap: false,
        colorBar: true,
        realPercent: true,
        seperateRightWrong: true
    };
    //var failList = [];

    var total = 0;
    var wrongCount = 0;
    var rightCount = 0;
    if(window.location.pathname == "/" || window.location.pathname == "/dashboard"){
        wkof.include('Apiv2, Menu, Settings');
        wkof.ready('Menu').then(install_menu);
        wkof.ready('Settings').then(install_settings);
    } else {
        wkof.include('Settings');
        waitForKeyElements("#progress-bar", function(){
            wkof.ready('Settings')
                .then(install_settings)
                .then(function(){
            //wkof.settings.rcpb = $.extend(true, {}, defaults, wkof.settings.rcpb);
                //$.jStorage.listenKeyChange('wrongCount', function (key, action) {
                //    debugger;
                //    if(wrongCount != $.jStorage.get('wrongCount')){
                //        wrongCount = $.jStorage.get('wrongCount');
                //        if(failList.includes($.jStorage.get('lastItems')[$.jStorage.get('lastItems').length - 1]) == false){
                //            failList.push($.jStorage.get('lastItems')[$.jStorage.get('lastItems').length - 1]);
                //        }
                //    }
                //});
                $.jStorage.listenKeyChange('lastItems', function (key, action) {
                    //jQuery.grep($.jStorage.index(), function( n, i ) {
                    //    return ( n.match('^[rkv][0-9]+') );
                    //});

                    //$.jStorage.get('lastItems')[$.jStorage.get('lastItems').length - 1];
                    if($.jStorage.get($.jStorage.get('lastItems')[$.jStorage.get('lastItems').length - 1]).mi + ($.jStorage.get($.jStorage.get('lastItems')[$.jStorage.get('lastItems').length - 1]).ri == null ? 0 : $.jStorage.get($.jStorage.get('lastItems')[$.jStorage.get('lastItems').length - 1]).ri) > 0){
                        wrongCount = wrongCount + 1;
                    } else {
                        rightCount = rightCount + 1;
                    }
                    $('#barCorrect').css('width',(rightCount / total * 100) + '%');
                    $('#barWrong').css('width',(wrongCount / total * 100) + '%');
                    $('#real-correct-rate').text((rightCount / (rightCount + wrongCount) * 100).toFixed(2));
                    $('#completed-correct-count').text(rightCount);
                    $('#completed-incorrect-count').text(wrongCount);
                });
            });
        });
    }

    function install_menu() {
        wkof.Menu.insert_script_link({
            script_id: 'rcpb',
            name: 'rcpb',
            submenu:   'Settings',
            title:     'WaniKani Reviews Color Progress Bar And Real Percentage',
            on_click:  open_settings
        });
    }
    function open_settings() {
        settings_dialog.open();
    }
    function install_settings() {
        settings_dialog = new wkof.Settings({
            script_id: 'rcpb',
            name: 'rcpb',
            title: 'WaniKani Reviews Color Progress Bar',
            on_save: process_settings,
            settings: {
                'grp_main': {
                    type:'group',
                    label:'Main',
                    content:{
                        'colorBar': {type:'checkbox',label:'Show Color Progress',default:defaults.colorBar},
                        'realPercent': {type:'checkbox',label:'Show Real Percent',default:defaults.realPercent},
                        'swap': {type:'checkbox',label:'Swap Correct/Incorrect',default:defaults.swap},
                        'barHeight': {type:'number',label:'Bar Height (in px)',default:defaults.barHeight}
                    }
                }
            }
        });
        settings_dialog.load().then(function(){
            wkof.settings.rcpb = $.extend(true, {}, defaults, wkof.settings.rcpb);
                total = $.jStorage.get('reviewQueue').length + $.jStorage.get('activeQueue').length;
                if(wkof.settings.rcpb.swap == true){
                    $('#progress-bar #bar').after('<div id="barWrong" style="width: 0%;">&nbsp;</div><div id="barCorrect" style="width: 0%;">&nbsp;</div>');
                    $('#completed-count').after('<span id="completed-correct-count">0</span>');
                    $('#completed-count').prev().before('<i id="completed-incorrect-count-icon" class="icon-remove"></i><span id="completed-incorrect-count">0</span>');
                } else {
                    $('#progress-bar #bar').after('<div id="barCorrect" style="width: 0%;">&nbsp;</div><div id="barWrong" style="width: 0%;">&nbsp;</div>');
                    $('#completed-count').after('<span id="completed-correct-count">0</span><i id="completed-incorrect-count-icon" class="icon-remove completed-incorrect-count-icon"></i><span id="completed-incorrect-count">0</span>');
                }
                $('#correct-rate').after('<span id="real-correct-rate">100</span>');
            var css = '#barCorrect { ' +
                ' background-color: #88cc00;' +
                '}' +
                '#barWrong {' +
                ' background-color: #ff0033;' +
                '}' +
                '#real-correct-rate:after {' +
                ' content: "%";' +
                '}'
            if(wkof.settings.rcpb.colorBar == true){
                css = css + '#progress-bar {' +
                    ' display: flex;' +
                    ' height: ' + wkof.settings.rcpb.barHeight + 'px; '+
                    '}' +
                    '#progress-bar #bar {' +
                    ' display: none;' +
                    '}'
            } else {
                css = css + '#progress-bar #barWong, #progress-bar #barCorrect {' +
                    ' display: none;' +
                '}'
            }
            if(wkof.settings.rcpb.realPercent == true){
                css = css + '#completed-count, #correct-rate {' +
                    ' display: none;' +
                    '}'
            } else {
                css = css + '#completed-correct-count, #completed-incorrect-count, #completed-incorrect-count-icon, #real-correct-rate {'+
                    ' display: none;' +
                    '}'
            }
            jigen.addStyle(css);
        });
    }
    function process_settings(){
        settings_dialog.save();
        console.log('Settings saved!');
    }
})(window.wk_reviewsColorProgressBar);