Wanikani Review Forecast 24H Format

24H format for WaniKani review forecast

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 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)
}