Wanikani: Dashboard Vacation Mode

Adds a vacation mode shortcut button to the dashboard menu.

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Wanikani: Dashboard Vacation Mode
// @namespace    http://tampermonkey.net/
// @version      1.1.0
// @description  Adds a vacation mode shortcut button to the dashboard menu.
// @author       kumirei
// @include      /^https://(www|preview).wanikani.com*/
// @grant        none
// ==/UserScript==
/*jshint esversion: 8 */

(function($) {
    let header = document.getElementsByClassName('global-header')[0];
    if (!header) return;

    $.ajax('/settings/account')
        .then(extract_vacation_form)
        .then(function(form) {
        var btn = $(form.btn)[0];
        btn.className = 'sitemap__page dashboardVacationMode';
        $('head').append(`<style id="dashboardVacationModeCSS">
                              .dashboardVacationMode {
                                  background: transparent;
                                  color: #e5e5e5;
                                  border: none;
                                  margin: 0 -8px 5px;
                                  padding: 8px;
                                  width: -webkit-fill-available;
                                  text-align: left;
                                  border-radius: 4px;
                                  font-family: "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;
                              }
                              .dashboardVacationMode:hover {
                                  background: rgba(255, 255, 255, 0.2);
                              }
                              .system-alert > h3 > a:not([href]) {
                                  cursor: pointer;
                              }
                          </style>`);
        var post = function() { $.post(form.url, form.data, function() { window.location.reload(); } );};
        btn.onclick = post;
        $('.sitemap__expandable-chunk--account >> .sitemap__page')[1].after(btn);
        $('[href="/settings/account#vacation-mode').text('Deactivate vacation mode').removeAttr('href').click(post);
    });

    function extract_vacation_form(html){
        let HTML = $(html);
        let state = HTML.find('#vacation-btn')[0].value == "Activate vacation mode";
        var form = HTML.find('#edit_user_vacation');
        var url = form.attr('action');
        var data = {};
        form.find('input').toArray().forEach(function(i){
            var name = $(i).attr('name');
            var value = $(i).attr('value');
            data[name] = value;
        });
        var btn = $(html).find('#vacation-btn')[0].outerHTML;
        return ({btn:btn, url:url, data:data, state:state});
    }
})(window.jQuery);