WaniKani External Definition

Get External Definition from Weblio, Kanjipedia

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

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

(function() {
    'use strict';

    var word = $('div#character').text().trim().replace(/する|〜/,'');
    
    $.jStorage.listenKeyChange('currentItem', function(key) {
        word = $('div#character').text().trim().replace(/する|〜/,'');
    });
    
    const updateInfo = function(){
        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.";

                            $('section#item-info-meaning-mnemonic').clone().prependTo('#item-info-col2').addClass('kanjipedia');
                            $('.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.';
                    }

                    $('section#item-info-meaning-mnemonic').clone().prependTo('#item-info-col2').addClass('weblio');
                    $('.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) {
        // look through all mutations that just occured
        for(var i=0; i<mutations.length; ++i) {
            // look through all added nodes of this mutation
            for(var j=0; j<mutations[i].addedNodes.length; ++j) {
                // was a child added with ID of 'bar'?
                if(mutations[i].addedNodes[j].id == "note-meaning") {
                    updateInfo();
                }
            }
        }
    });
    var config = { childList: true };
    observer.observe($('#item-info-col2').get(0), config);
})();

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;
}

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址