WaniKani Show Specific SRS 2

Show "Apprentice 3" instead of "Apprentice", etc.

// ==UserScript==
// @name          WaniKani Show Specific SRS 2
// @namespace     https://www.wanikani.com
// @description   Show "Apprentice 3" instead of "Apprentice", etc.
// @author        Gijsc1
// @version       2.0.4
// @match         https://www.wanikani.com/subjects/review*
// @license       MIT; http://opensource.org/licenses/MIT
// @grant         none
// ==/UserScript==


// taken from doublecheck
(async function(){
function get_controller(name) {
        return Stimulus.getControllerForElementAndIdentifier(document.querySelector(`[data-controller~="${name}"]`),name);
    }

// Waits for both stimulus and the quiz_queue to be defined, which happens after some unkown precondition is met somewhere else.
async function grab_queue(){
    let temp_queue;
    for (let i = 0; i < 50; i++) {
        // Probably the worst line of code I have ever written.
        //console.log('Stimulus status in iteration ',i,' :',typeof Stimulus);
        if (typeof Stimulus !== 'undefined' && (temp_queue = get_controller('quiz-queue')))
            {return temp_queue;}
        // apparantly this is what passes for a sleep function in javascript.
        await new Promise(r => setTimeout(r, 100));
    }
    // If we get here than the script will crash when trying to acess the queue.
    return temp_queue
}

let quiz_queue = await grab_queue();
let srs_manager = quiz_queue.quizQueue.srsManager;
//console.log('actual srs_manager:',srs_manager);
// swap out method with updated one below.
srs_manager.updateSRS = new_updateSRS;

// Mapping from srs levels to displayable strings.
function updateSrsNames(e) {
    let value;
    switch (e) {
        case 1:
            value = "Apprentice 1";
            break;
        case 2:
            value = "Apprentice 2";
            break;
        case 3:
            value = "Apprentice 3";
            break;
        case 4:
            value = "Apprentice 4";
            break;
        case 5:
            value = "Guru 1";
            break;
        case 6:
            value = "Guru 2";
            break;
        case 7:
            value = "Master";
            break;
        case 8:
            value = "Enlighten";
            break;
        case 9:
            value = "Burn";
            break;
    }
    // console.log('obtained srs value:',value);
    return value;

	}

// This seems like a weird way to import something, but doublecheck does it this way and it just works.
let DidChangeSRSEvent = (await importShim('events/did_change_srs_event')).default;

// A copy of the updateSRS method of the controllers/quiz_queue/srs_manager class.
// Apparantly it is not possible to override static methods, so I override the method that calls the static method to instead call updateSrsNames.
// I do not know if monkeypatching a method with the keyword 'this' in it will work, and what 'this' will then refer to.
// I sidestep the issue by replacing 'this' with the specific srs_manager object.
function new_updateSRS({subject: e, stats: t}) {
        if (!srs_manager.srsMap.has(e.id))
            return;
        const n = srs_manager.srsMap.get(e.id)
          , r = t.meaning.incorrect + t.reading.incorrect;
        if (0 === r)
            window.dispatchEvent(new DidChangeSRSEvent({
                wentUp: !0,
                newLevelText: updateSrsNames(n + 1)
            }));
        else {
            const e = n >= 5 ? 2 : 1
              , t = Math.max(1, n - e * Math.round(r / 2));
            window.dispatchEvent(new DidChangeSRSEvent({
                wentUp: !1,
                newLevelText: updateSrsNames(t)
            }))
        }
    }
})();