Show Total Lesson Count - WaniKani

Changes the count of lessons on the Today's Lessons tile to show the total number of available lessons in addition to the number selected for you

Fra og med 07.02.2024. Se den nyeste version.

// ==UserScript==
// @name         Show Total Lesson Count - WaniKani
// @namespace    http://tampermonkey.net/
// @version      0.3.3
// @description  Changes the count of lessons on the Today's Lessons tile to show the total number of available lessons in addition to the number selected for you
// @license      MIT
// @author       LupoMikti
// @match        https://www.wanikani.com
// @match        https://www.wanikani.com/dashboard
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    /* global wkof */

    let scriptId = 'show_total_lesson_count';
    let scriptName = 'Show Total Lesson Count';
    let wkBatchSize = 0;
    let initial_load = true;
    let todaysLessonsCount;
    let settings;

    if (!window.wkof) {
        if (confirm(scriptName + ' 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;
    }

    wkof.include('Settings, Menu, Apiv2');
    wkof.ready('Settings, Menu, Apiv2').then(loadSettings).then(insertMenu).then(main);

    function loadSettings() {
        wkBatchSize = wkof.user.preferences.lessons_batch_size;

        let defaults = {
            showTotalOnly: false,
            setOwnPreferredDaily: false,
            preferredDailyAmount: wkBatchSize * 3
        };

        return wkof.Settings.load(scriptId, defaults).then(function(wkof_settings) {settings = wkof_settings;});
    }

    function insertMenu() {
        let config = {
            name: scriptId,
            submenu: 'Settings',
            title: scriptName,
            on_click: openSettings
        };

        wkof.Menu.insert_script_link(config);
    }

    function openSettings() {
        let config = {
            script_id: scriptId,
            title: scriptName,
            on_save: main,
            content: {
                showTotalOnly: {
                    type: 'checkbox',
                    label: 'Show Only Total Lesson Count',
                    hover_tip: `Changes display between "<today's lesson count> / <total lesson count>" and just "<total lesson count>"`,
                    default: false
                },
                setOwnPreferredDaily: {
                    type: 'checkbox',
                    label: 'Set Your Own Daily Lesson Count',
                    hover_tip: `Choose whether to display the value you set as your daily lesson count or not`,
                    default: false
                },
                preferredDailyAmount: {
                    type: 'number',
                    label: 'Preferred Daily Lesson Amount',
                    hover_tip: `The number you want displayed for "Today's Lessons". Maximum of batch size * 3. NOTE: this does not actually change the number of available lessons.`,
                    default: wkBatchSize * 3,
                    min: 0,
                    max: wkBatchSize * 3
                }
            }
        };

        let dialog = new wkof.Settings(config);
        dialog.open();
    }

    function getCountContainers() {
        let dashboardTileCountContainer = document.querySelector('.todays-lessons__count-text .count-bubble');
        let navBarCountContainer = document.querySelector('.lesson-and-review-count__count'); // must rely on lessons being first

        if (initial_load && dashboardTileCountContainer && navBarCountContainer) {
            todaysLessonsCount = parseInt(dashboardTileCountContainer.textContent);
            initial_load = false;
        }

        return [dashboardTileCountContainer, navBarCountContainer];
    }

    async function main() {
        let summary_data = await wkof.Apiv2.get_endpoint('summary');
        let totalLessonCount = summary_data.lessons[0].subject_ids.length;
        let lessonCountContainers = getCountContainers();
        let todaysCountForDisplay = todaysLessonsCount;

        if (lessonCountContainers.some(node => node == null)) {
            return;
        }

        if (isNaN(todaysLessonsCount)) {
            todaysCountForDisplay = 0;
        }
        else {
            if (settings.setOwnPreferredDaily)
                todaysCountForDisplay = todaysLessonsCount - (wkBatchSize * 3 - settings.preferredDailyAmount);
        }

        lessonCountContainers[0].textContent = settings.showTotalOnly ? totalLessonCount : todaysCountForDisplay + ' / ' + totalLessonCount;
        lessonCountContainers[1].textContent = todaysCountForDisplay

        if (todaysCountForDisplay === 0) {
            // hide the start button if it is not already, TODO: disable nav bar button if it is not already
            let startButton = document.querySelector('.todays-lessons-button--start')
            if (startButton && startButton.checkVisibility()) {
                startButton.style['display'] = 'none';
            }
        }

        // hide "Today's" subtitle
        let lessonSubtitle = document.querySelector('.todays-lessons__subtitle');

        if (lessonSubtitle && lessonSubtitle.checkVisibility()) {
            lessonSubtitle.style['display'] = 'none';
        }
    }
})();