WaniKani External Definition

Get External Definition from Weblio, Kanjipedia

目前為 2017-12-06 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         WaniKani External Definition
// @namespace    http://www.wanikani.com
// @version      0.4.2
// @description  Get External Definition from Weblio, Kanjipedia
// @author       polv
// @match        *://www.wanikani.com/review/session*
// @match        *://www.wanikani.com/lesson/session*
// @grant        GM_xmlhttpRequest
// @connect      kanjipedia.jp
// @connect      weblio.jp
// ==/UserScript==

(function() {
    'use strict';

    var word = $('div#character').text().trim().replace(/する|〜/,'');
    var url = document.URL;

    $.jStorage.listenKeyChange('currentItem', function(key) {
        word = $('div#character').text().trim().replace(/する|〜/,'');
    });

    function updateInfo(){
        if(word.length == 1) {
            var url_base = 'http://www.kanjipedia.jp/';
            console.log('Opening ' + url_base + 'search?k=' + word + '&kt=1&sk=leftHand');
            GM_xmlhttpRequest({
                method: "GET",
                url: url_base + 'search?k=' + word + '&kt=1&sk=leftHand',
                onload: function(data) {
                    var result = $('<div />').append(data.responseText).find('#resultKanjiList a')[0].href;
                    console.log('Opening ' + url_base + result.slice(25));
                    GM_xmlhttpRequest({
                        method: "GET",
                        url: url_base + result.slice(25),
                        onload: function(data) {
                            var result2 = $('<div />').append(data.responseText).find('#kanjiRightSection p').html();
                            if(result === undefined) result = "Definition not found.";

                            $('#item-info-col2').prepend('<section class="kanjipedia"></section>');
                            if(url.indexOf('lesson')!=-1) {
                                $('#supplement-kan-meaning .col2').prepend('<section class="kanjipedia"></section>');
                                $('#supplement-voc-meaning .col2').prepend('<section class="kanjipedia"></section>');
                                console.log('Prepended');
                            } else {
                                if ($.jStorage.get('questionType')=='reading') $('.kanjipedia').css('display','none');
                            }

                            $('.kanjipedia').html(result2 + '<br><a href="'+ url_base + result.slice(25) +'" target="_blank">Click for full entries</a>');
                            $('.kanjipedia').prepend('<h2>Kanjipedia Explanation</h2>');
                        }
                    });
                }
            });
        } else {
            console.log('Opening ' + 'https://www.weblio.jp/content/' + word);
            GM_xmlhttpRequest({
                method: "GET",
                url: 'https://www.weblio.jp/content/' + word,
                onload: function(data) {
                    var result =  $('<div />').append(data.responseText).find('.NetDicBody').html();
                    if(result === undefined){
                        result = "Definition not found.";
                    }
                    if(word.length == 1) {
                        var i = -1;
                        $.each($('<div />').append(data.responseText).find('.NetDicHead .midashigo'), function(index, value){
                            var str = value.textContent;
                            if( str.indexOf('漢字') != -1 ) i = index;
                        });
                        console.log(i);

                        var full_result = $('<div />').append(data.responseText).find('.NetDicBody:nth-child('+ (3*i+2) +')').html();
                        if (i != -1){
                            result = '';
                            for(var j=0; result.length < 200 && nthIndex(full_result, word, j) < result.length; ) {
                                j++;
                                result = full_result.substring( nthIndex(full_result, word, j)-2, full_result.indexOf('【 ', nthIndex(full_result, word, j) ) );
                            }
                            console.log(result.length);
                        } else
                            result = 'Kanji definition not found.';
                    }

                    $('#item-info-col2').prepend('<section class="weblio"></section>');

                    if(url.indexOf('lesson')!=-1){
                        $('#supplement-kan-meaning .col2').prepend('<section class="weblio"></section>');
                        $('#supplement-voc-meaning .col2').prepend('<section class="weblio"></section>');
                        console.log('Prepended');
                    } else {
                        if ($.jStorage.get('questionType')=='reading') $('.weblio').css('display','none');
                    }
                    
                    $('.weblio').html(result + '<br><a href="https://www.weblio.jp/content/' + word +'" target="_blank">Click for full entries</a>');
                    $('.weblio').prepend('<h2>Weblio Explanation</h2>');
                }
            });
        }
    }

    var observer = new MutationObserver(function(mutations) {
        for(var i=0; i<mutations.length; ++i) {
            for(var j=0; j<mutations[i].addedNodes.length; ++j) {
                if(mutations[i].addedNodes[j].id == "item-info-meaning-mnemonic") {
                    updateInfo();
                }
            }
        }
    });
    observer.observe($('#item-info-col2').get(0), { childList: true });

    // setup observer to change kanji info box contents for subsequent items
    var observer2 = new MutationObserver(function(mutations) {
        word = $('div#character').text().trim().replace(/する|〜/,'');
        $('.weblio').remove();
        $('.kanjipedia').remove();
        updateInfo();
    });
    observer2.observe($('div#character').get(0), { attributes: true, childList: true, characterData: true });

    var observer3 = new MutationObserver(function(mutations) {
        for(var i=0; i<mutations.length; ++i) {
            for(var j=0; j<mutations[i].addedNodes.length; ++j) {
                if(mutations[i].addedNodes[j].style.display == "block") {
                    $('.weblio').css('display','block');
                    $('.kanjipedia').css('display','block');
                }
            }
        }
    });
    observer3.observe($('#item-info-meaning-mnemonic').get(0), { attributes: true });

})();

function nthIndex(str, pat, n){
    var L= str.length, i= -1;
    while(n-- && i++<L){
        i= str.indexOf(pat, i);
        if (i < 0) break;
    }
    return i;
}

try { $('.app-store-menu-item').remove(); $('<li class="app-store-menu-item"><a href="https://community.wanikani.com/t/there-are-so-many-user-scripts-now-that-discovering-them-is-hard/20709">App Store</a></li>').insertBefore($('.navbar .dropdown-menu .nav-header:contains("Account")')); window.appStoreRegistry = window.appStoreRegistry || {}; window.appStoreRegistry[GM_info.script.uuid] = GM_info; localStorage.appStoreRegistry = JSON.stringify(appStoreRegistry); } catch (e) {}