Wanikani Review Forecast 24H Format

24H format for WaniKani review forecast

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Wanikani Review Forecast 24H Format
// @namespace    AksUWK24HF
// @version      0.61
// @description  24H format for WaniKani review forecast
// @author       AksU
// @match        https://www.wanikani.com/
// @match        https://www.wanikani.com/dashboard
// @grant        none
// ==/UserScript==


// Little handling, we retry a few times as it can execute the code a bit too fast
function translateTo24HStyle(timeOutInterval) {
    var cpt = 0
    const maxTimeout = 20
    const timeout = setTimeout(() => {
        if (document && document.getElementsByClassName("review-forecast-widget__title").length) {
            for (let timer of document.getElementsByClassName("review-forecast-widget__title")) {
                if (timer.innerHTML.indexOf("AM") > 0) {
                    timer.innerHTML = timer.innerHTML.substring(0, timer.innerHTML.length - 3);
                    if (parseInt(timer.innerHTML, 10) < 10) {
                        timer.innerHTML = "0" + timer.innerHTML;
                    } else if (parseInt(timer.innerHTML, 10) === 12) {
                        timer.innerHTML = "00";
                    }
                    timer.innerHTML += "h";
                } else if (timer.innerHTML.indexOf("PM") > 0) {
                    timer.innerHTML = timer.innerHTML.substring(0, timer.innerHTML.length - 3);
                    var toAdd = 12;
                    if (parseInt(timer.innerHTML, 10) === 12) {
                        toAdd = 0;
                    }
                    timer.innerHTML = parseInt(timer.innerHTML, 10) + toAdd;
                    timer.innerHTML += "h";
                }
            }
            clearTimeout(timeout)
        } else {
            cpt++
            if (cpt >= maxTimeout) {
                clearTimeout(timeout)
            }
        }
    }, timeOutInterval)
}

// Check that the widget frame has been reloaded
document.addEventListener("turbo:before-fetch-response", (e) => {
    // We don't care if it is not a review forecast
    const turboFrameUrl = e.detail.fetchResponse.response.url
    if (!turboFrameUrl.includes("review-forecast")) {
        return
    }

    // Get the matching frame and check if it is complete
    const frame = document.getElementById(e.target.id);
    if (frame && frame.complete) {
        translateTo24HStyle(200)
    }
});

// It may happens that the script is executed a bit after the frame is loaded
// This is only trigerred at start and should fix the 24H Style on the first render.
window.onload = () => {
    translateTo24HStyle(500)
}