Wanikani Forums Lesson/Review Status

Shows status of your Wanikani lessons/reviews while in the forums.

目前為 2017-03-09 提交的版本,檢視 最新版本

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

You will need to install an extension such as Tampermonkey to install this script.

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

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

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

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

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name        Wanikani Forums Lesson/Review Status
// @namespace   rfindley
// @description Shows status of your Wanikani lessons/reviews while in the forums.
// @version     1.0.2
// @include     https://community.wanikani.com/*
// @require     https://greasyfork.org/scripts/27967-wanikani-forums-global-framework/code/Wanikani%20Forums%20Global%20Framework.js?version=179603
// @copyright   2017+, Robin Findley
// @license     MIT; http://opensource.org/licenses/MIT
// @run-at      document-end
// @grant       none
// ==/UserScript==

window.wkf_lrstatus = {};

(function(gobj) {

    var settings_url = '/scripts/lrstatus';
    var randomize_query = 300; // Randomize API query times over a 300 sec period to spread server load.

    //-------------------------------------------------------------------
    // Let the global script know about our settings page (if we have one)
    //-------------------------------------------------------------------
//    window.wkf_global.add_script('Lesson/Review Status', settings_url);
    var apikey = window.wkf_global.get_apikey();
    if (apikey === null) return; // Just in case the auto-redirect doesn't work

    // Don't run past this point if we are on a settings screen.
    if (window.location.pathname.match(/^\/scripts\//) !== null) return;

    //-------------------------------------------------------------------
    // Styling info for this script.
    //-------------------------------------------------------------------
    var css =
        '.dashboard_bubble {background-color:#6cf; font-size:0.8em; border-radius:0.5em; padding:0 6px; margin:0 4px; color:#fff; font-weight:bold;}'+
        '.dashboard_bubble.zero {background-color:#bdbdbd;}'+
        '.float_wkappnav .d-header {height:inherit;}'+
        '.float_wkappnav .wanikani-app-nav-container {border-top:1px solid #ccc; line-height:2em;}'+
        '.float_wkappnav .wanikani-app-nav ul {padding-bottom:0; margin-bottom:0; border-bottom:inherit}';

    //-------------------------------------------------------------------
    // Update the lesson/review count info on the screen.
    //-------------------------------------------------------------------
    function update_counts(lessons, reviews) {
        var lc = $('#lesson_count');
        var rc = $('#review_count');
        if (lc.length != 1) {
            $('head').append('<style type="text/css">'+css+'</style>');
            lc = $('<span id="lesson_count" class="dashboard_bubble"></span>');
            rc = $('<span id="review_count" class="dashboard_bubble"></span>');
            $('.wanikani-app-nav > ul > li:contains("Lessons")').append(lc);
            $('.wanikani-app-nav > ul > li:contains("Reviews")').append(rc);
        }
        lc.text(lessons);
        rc.text(reviews);
        if (lessons === 0)
            lc.addClass('zero');
        else
            lc.removeClass('zero');
        if (reviews === 0)
            rc.addClass('zero');
        else
            rc.removeClass('zero');
    }

    //-------------------------------------------------------------------
    // Fetch lesson/review count info from the server.
    //-------------------------------------------------------------------
    function fetch_data() {
        var now = Math.round(new Date().getTime()/1000);
        window.wkf_global.query_api('/study-queue')
        .then(function(json){
            var lessons = json.requested_information.lessons_available;
            var reviews = json.requested_information.reviews_available;
            localStorage.setItem('wkf_lrstatus.last_query', now);
            localStorage.setItem('wkf_lrstatus.lesson_count', lessons);
            localStorage.setItem('wkf_lrstatus.review_count', reviews);
            update_counts(lessons, reviews);
        });
        var next_query = Math.trunc((now/900)+1) * 900 + Math.round(Math.random()*randomize_query) + 10 - now;
        setTimeout(fetch_data, next_query*1000);
    }

    //-------------------------------------------------------------------
    // Startup. Runs at document 'load' event.
    //-------------------------------------------------------------------
    function startup() {

        // Attach the Dashboard menu to the stay-on-top menu.
        var wk_app_nav = $('.wanikani-app-nav').closest('.container');
        var top_menu = $('.d-header');
        var main_content = $('#main-outlet');
        $('body').addClass('float_wkappnav');
        wk_app_nav.addClass('wanikani-app-nav-container');
        var original_top_menu_height = top_menu.height();
        top_menu.find('>.wrap > .contents:eq(0)').after(wk_app_nav);
        // We need give the browser an opportunity to redraw the menu, so we can get the new height.
        setTimeout(function(){
            // Adjust the main content's top padding, so it won't be hidden under the new taller top menu.
            var new_top_menu_height = top_menu.height();
            var original_main_content_toppad = Number(main_content.css('padding-top').match(/[0-9]*/)[0]);
            main_content.css('padding-top', original_main_content_toppad + (new_top_menu_height - original_top_menu_height) + 'px');
        },1);

        var last_user = localStorage.getItem('wkf_lrstatus.last_user') || '';
        var current_user = $('#current-user img').attr('title');
        if (last_user != current_user && current_user !== undefined) {
            localStorage.removeItem('wkf_lrstatus.last_query');
            localStorage.removeItem('wkf_lrstatus.lesson_count');
            localStorage.removeItem('wkf_lrstatus.review_count');
            localStorage.setItem('wkf_lrstatus.last_user', current_user);
        }
        var now = Math.trunc(new Date().getTime()/1000);
        var last_qtr_hr = Math.trunc(now / 900) * 900;
        var last_query = Number(localStorage.getItem('wkf_lrstatus.last_query'));

        if (last_query < last_qtr_hr) {
            fetch_data();
        } else {
            var lessons = Number(localStorage.getItem('wkf_lrstatus.lesson_count'));
            var reviews = Number(localStorage.getItem('wkf_lrstatus.review_count'));
            update_counts(lessons, reviews);
        }

        var next_query = Math.trunc((now/900)+1) * 900 + Math.round(Math.random()*randomize_query) + 10 - now;
        setTimeout(fetch_data, next_query*1000);
    }

    // Run startup() after window.onload event.
    if (document.readyState === 'complete')
        startup();
    else
        window.addEventListener("load", startup, false);

})(window.wkf_lrstatus);