WaniKani Pitch Info

Grabs Pitch value for a given Vocab from weblio.jp and displays it on a WaniKani vocab or session page.

目前為 2017-07-03 提交的版本,檢視 最新版本

// ==UserScript==
// @name         WaniKani Pitch Info
// @include     https://www.wanikani.com/*
// @include     http://www.wanikani.com/*
// @run-at document-end
// @namespace    https://gf.qytechs.cn/en/scripts/31070-wanikani-pitch-info
// @version      0.15
// @description  Grabs Pitch value for a given Vocab from weblio.jp and displays it on a WaniKani vocab or session page.
// @author       Invertex
// @supportURL http://invertex.xyz
// @grant GM_xmlhttpRequest
// @connect weblio.jp/*
// @connect weblio.jp
// ==/UserScript==

var vocab = "";
var vocabPitchType = -1;
var savedReading = "";
var sessionReadingElem;
var pitchInfoElem;
var descriptionElem;
var readingElem;

$(document).ready(function(){
    parsePage();
    
    var observer = new MutationObserver(function(mutations){
    mutations.forEach(function(mutation){
        parsePage();
    });
});

    descriptionElem = document.getElementsByClassName('pure-g-r')[0];
    if(descriptionElem != null){
        observer.observe(descriptionElem, {attributes: true, attributeFilter: ['style', 'class'], subtree:true}); 
    }
    readingElem = document.getElementById('supplement-voc-reading');
    if(readingElem != null){
        observer.observe(readingElem, {attributes: true, attributeFilter: ['style', 'class'], subtree:true}); 
    }
    /*    var allInfo = document.getElementById('all-info');
    if(itemInfo != null){
        observer.observe(itemInfo, {attributes: true, attributeFilter: ['style', 'class']}); 
        console.log("all info watch");
    }*/
});

function parsePage(){
    var tmpVocab = "";
    var tmpSessionElem;
    
    var sessionChar = document.getElementById("character"); //Check for seassion character
	if(sessionChar != null && ($(sessionChar).hasClass('vocabulary') || $('#main-info').hasClass('vocabulary')))
	{
		var sessionVocElem = sessionChar.getElementsByTagName("span")[0]; //Get sub element that contains the characters.
        if(sessionVocElem != null)
        {
            
            tmpVocab = sessionVocElem.innerHTML;
            tmpSessionElem = document.getElementById("item-info-reading");
        }
		else //We must be in Lesson session if there is no "span" element, characters are in first element we got.
		{
           tmpVocab = sessionChar.innerHTML;
           var descripDivs = document.getElementById("supplement-voc-reading").getElementsByClassName("col1")[0];
           tmpSessionElem = descripDivs;
        }
	}
    else //Check for Vocab page element
    {
       var vocabIcon = document.getElementsByClassName("vocabulary-icon")[0];
	   if(vocabIcon != null)
	   {
           tmpVocab = $(vocabIcon.getElementsByTagName("span")[0]).html();
           tmpSessionElem = document.getElementsByClassName("vocabulary-reading")[0];
        }
        //Individual Kanji shouldn't be looked for I guess, so deprecated. Uncomment this section if you'd like it to be
        /*else //Must be on a Kanji page instead of Vocab, check for other element
        {
            var kanjiIcon = document.getElementsByClassName("kanji-icon")[0];
            if(kanjiIcon != null)
            {
                vocab = $(kanjiIcon.getElementsByTagName("span")[0]).html();
                var kunYomiDisp = document.getElementsByTagName("H3");
                var kunYomiElem;
                if(kunYomiDisp != null)
                {
                    for(i = 0; i < kunYomiDisp.length; i++)
                    {
                        if(kunYomiDisp[i].innerHTML === "Kun'yomi")
                        {
                            sessionReadingElem = kunYomiDisp[i].parentNode;
                            break;
                        }
                    }
                }
            }
        }*/
	}
	if((tmpVocab != null && tmpVocab != "" && !tmpVocab.includes("nbsp") && tmpSessionElem != null))
	{
        if(!tmpSessionElem.hasAttribute("vocabpitch"))
        {
	        tmpSessionElem.setAttribute("vocabpitch", true);
        }
        sessionReadingElem = tmpSessionElem;
        pitchInfoElem = sessionReadingElem.getElementsByClassName("pitchInfo")[0];
        if(pitchInfoElem == null)
        {
            pitchInfoElem = document.createElement("P");
            pitchInfoElem.className = "pitchInfo";
            sessionReadingElem.appendChild(pitchInfoElem);
            pitchInfoElem.innerHTML = "Loading Pitch info...";
            getVocabPitch(tmpVocab);
        }
        else if((pitchInfoElem.innerHTML != "Loading Pitch info..." && tmpVocab != vocab) || tmpVocab != vocab)
        {
            pitchInfoElem.innerHTML = "Loading Pitch info...";
            getVocabPitch(tmpVocab);
        }
	}
}

function getVocabPitch(inVocab){
        vocab = inVocab;
        GM_xmlhttpRequest({
            method: "GET",
            url: "http://www.weblio.jp/content/" + inVocab,
            onload: parseResponse
        });
}

function parseResponse(responseObj){
	var dparser = new DOMParser();
    var respDoc = dparser.parseFromString(responseObj.responseText, "text/html").documentElement;
	var vocabResults = respDoc.getElementsByClassName('midashigo');
    
	if(vocabResults != null)
    {
		for(i = 0; i < vocabResults.length; i++)
        {
            var title = $(vocabResults[i]).attr("title");
			if(title == vocab)
			{
				var spans = vocabResults[i].getElementsByTagName("span");
                if(spans != null)
                {
                    for(s = 0; s < spans.length; s++)
                    {
                        var spanText = $(spans[s]).html();
                        if(spanText.includes("["))
                        {
			               var numMatch = spanText.match(/\d+/g);
				           if(numMatch != null)
				           {
                                vocabPitchType = parseInt(numMatch);
                                writeToPage(getPitchType(vocabPitchType));
                                return null;
				            }
                        }
                    }
                }
			}
		}
	}
   vocabPitchType = -1;
   writeToPage(getPitchType(vocabPitchType));
}

function getPitchType(pitchNum){
    if(pitchNum === 0){
        return "Pitch starts low, ends high.";
    }
    else if(pitchNum === 1){
        return "Pitch starts high, ends low.";
    }
    else if(pitchNum >= 0)
    {
        return "Pitch starts low, stays high for " + pitchNum + " moji, ends low.";
    }
    else
    {
        return "No pitch value found, click ? for more info.";
    }
}

function writeToPage(pitchInfo){
    var appendHtml = pitchInfo + " <a href=\"http://www.weblio.jp/content/" + vocab +"\" target=\"_blank\" >[?]</a>";
    
    if(pitchInfoElem != null)
    {
        pitchInfoElem.innerHTML = appendHtml;
    }
}

QingJ © 2025

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