WaniKani Study Config

Provides website settings for easier studying.

目前為 2014-10-06 提交的版本,檢視 最新版本

// ==UserScript==
// @name        WaniKani Study Config
// @namespace   wkstudycfg
// @version     1.0.0
// @description Provides website settings for easier studying.
// @author      Robin Findley
// @copyright   2014+, Robin Findley
// @license     GPL version 3 or any later version; http://www.gnu.org/copyleft/gpl.html
// @include     http://www.wanikani.com/level/*
// @include     https://www.wanikani.com/level/*
// @include     http://www.wanikani.com/radicals?*
// @include     https://www.wanikani.com/radicals?*
// @include     http://www.wanikani.com/kanji?*
// @include     https://www.wanikani.com/kanji?*
// @include     http://www.wanikani.com/vocabulary?*
// @include     https://www.wanikani.com/vocabulary?*
// @grant       GM_addStyle
// ==/UserScript==

// --[ Description ]-------------------------
// This script is intended to assist in studying kanji on WaniKani.com by performing the following tasks:
//   - Hides the meaning and pronunciation (kana) of radicals, kanji, and vocabulary on various screens until you hover over them with the mouse, to prevent you from seeing them until you are ready when studying.
//   - Sorts vocabulary, to group items by kanji.  Also puts locked vocabulary at the end of the vocab list.
//
// Provides settings:
//   - Enable/disable the hiding of meaning and pronunciation (kana)
// ---------------------------

// --[ License ]-------------------------
// The MIT License (MIT)
// 
// Copyright (c) 2014 Robin Findley
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
// ---------------------------

var wksc = {};
wksc.criteria = {};
wksc.criteria.unlocked_first = true;

function sort_vocab()
{
  var vocab_groups = document.querySelectorAll('.multi-character-grid');
  
  var grp_cnt = vocab_groups.length;
  for (var grp_idx = 0; grp_idx < grp_cnt; grp_idx++)
  {
    var group = vocab_groups[grp_idx];
    if (group.querySelector('li[id|=vocabulary]')==undefined) continue;
    
    var words = [];
    var word_cnt = group.childElementCount;
    for (var word_idx = 0; word_idx < word_cnt; word_idx++)
      words.push(group.children[word_idx]);

    words.sort(function (a,b) {
      var a_text = a.querySelector('.character').innerHTML;
      var b_text = b.querySelector('.character').innerHTML;

      if (wksc.criteria.unlocked_first == true) {
        var a_lock = (a.className.match(/\blocked\b/)!=null);
        var b_lock = (b.className.match(/\blocked\b/)!=null);
        if (!a_lock && b_lock) return -1;
        if (a_lock && !b_lock) return 1;
      }

      if (a_text < b_text) return -1;
      if (a_text > b_text) return 1;
      return 0;

    });

    for (var word_idx = 0; word_idx < word_cnt; word_idx++)
      group.appendChild(words[word_idx]);
  }
}

function add_styles() {
    GM_addStyle('.wksc_hide ul {opacity: 0} .wksc_hide:hover ul {opacity:1.0}');
}

function set_visibility(hidden) {
    if (hidden) {
        $('.character-item').addClass('wksc_hide');
    } else {
        $('.character-item.wksc_hide').removeClass('wksc_hide');
    }
}

function check_storage() {
	wksc.hide_info = Number(localStorage.getItem("wksc_hide") || 1);
    if (!(wksc.hide_info >= 0  && wksc.hide_info <= 1)) {
        wksc.hide_info = 1;
        localStorage.setItem("wksc_hide", wksc.hide_info);
    }
}

function GMsetup() {
	if (GM_registerMenuCommand) {
        GM_registerMenuCommand('WaniKani Study: Hide info until hover', function() {
            wksc.hide_info++;
            wksc.hide_info %= 2;
            localStorage.setItem("wksc_hide", wksc.hide_info);
            set_visibility(wksc.hide_info);
        });
    }
}

function main() {
    check_storage();
    GMsetup();
	add_styles();
    sort_vocab();
    set_visibility(wksc.hide_info);
}

window.addEventListener("load", main, false);