Wanikani DotDotDot Expander

Expand "..." in English translations

目前為 2019-10-16 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name          Wanikani DotDotDot Expander
// @namespace     https://www.wanikani.com
// @description   Expand "..." in English translations
// @version       3.0.0
// @include       https://www.wanikani.com/*
// @exclude       https://www.wanikani.com/lesson*
// @exclude       https://www.wanikani.com/review*
// @run-at        document-end
// @grant         none
// ==/UserScript==

// Original by Takuya Kobayashi
// Original location: https://greasyfork.org/en/scripts/5584-wanikani-dotdotdot-expander

// Repaired by Robin Findley

window.dot_expander = {};

(function (gobj) {

    /* global $, wkof, Search */
    /* eslint no-multi-spaces: "off" */

    //===================================================================
    // Initialization of the Wanikani Open Framework.
    //-------------------------------------------------------------------
    var script_name = 'DotDotDot Expander';
    if (!window.wkof) {
        if (confirm(script_name+' requires Wanikani Open Framework.\nDo you want to be forwarded to the installation instructions?')) {
            window.location.href = 'https://community.wanikani.com/t/instructions-installing-wanikani-open-framework/28549';
        }
        return;
    }

    wkof.include('ItemData');
    wkof.ready('document,ItemData').then(load_data).then(dotExpand);

    var by_slug = {rad:{}, kan:{}, voc:{}};
    gobj.by_slug = by_slug;

    // Load Wanikani items and index by type and slug.
    function load_data() {
        return wkof.ItemData.get_items().then(function(items){
            by_slug.rad = wkof.ItemData.get_index(items.filter(item => item.object === 'radical'), 'slug');
            by_slug.kan = wkof.ItemData.get_index(items.filter(item => item.object === 'kanji'), 'slug');
            by_slug.voc = wkof.ItemData.get_index(items.filter(item => item.object === 'vocabulary'), 'slug');
        });
    }

    // Hook into the search results.
    (function () {
        var removeResults;
        if (typeof Search === 'object' && typeof Search.removeResults === 'function') {
            removeResults = Search.removeResults;
            Search.removeResults = function () {
                dotExpand();
                removeResults();
            };
        }
    }());

    function dotExpand() {
        ['.single-character-grid','.multi-character-grid'].forEach(function(sel,grid_type){
            $(sel+' .character-item li:contains("...")').each(function(idx,elem){
                var enLi = $(elem);
                var a = enLi.closest('a');
                var itype = a.attr('href').split('/')[1].slice(0,3);
                var slug = (itype === 'rad' ? a.attr('href').split('/')[2] : a.find('.character').text().replace(/[\n ]+/g,''));
                var enFull = by_slug[itype][slug].data.meanings.filter(m => m.primary)[0].meaning;
                if (enFull) {
                    if (grid_type === 0) {
                        enLi.attr('title',enFull);
                    } else {
                        enLi.text(enFull);
                    }
                } else {
                    console.log('[DotDotDot Expander]: No expansion found for '+itype+'+ "'+slug+'"');
                }
            });
        });
    }

    gobj.expand = dotExpand;

})(window.dot_expander);