Last active
November 20, 2017 19:27
-
-
Save jealie/ed5ebdc6c5e637deb8127ae0f5e7f54b to your computer and use it in GitHub Desktop.
Kanji Meanings from jisho.org
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Kanji Meanings from jisho.org | |
// @namespace koohiijisho | |
// @description Adds kanji definitions from jisho.org to the study sections on kanji.koohii.com | |
// @include http://kanji.koohii.com/study/kanji/* | |
// @include https://kanji.koohii.com/study/kanji/* | |
// @include http://staging.koohii.com/study/kanji/* | |
// @include https://staging.koohii.com/study/kanji/* | |
// @require https://greasemonkey.github.io/gm4-polyfill/gm4-polyfill.js | |
// @grant GM_xmlhttpRequest | |
// @grant GM.xmlHttpRequest | |
// @version 1.03 | |
// @downloadURL https://gist.github.com/jealie/ed5ebdc6c5e637deb8127ae0f5e7f54b/raw/koohiistroke.user.js | |
// ==/UserScript== | |
var languages = ['french']; | |
//// all available languages: | |
//var languages = ['english', 'french', 'spanish', 'portuguese']; | |
var inject_container = document.createElement("div"); | |
inject_container.style.marginLeft = '82px'; inject_container.style.marginTop = '20px'; // quick-and-dirty styling | |
// Study section | |
if(window.location.href.indexOf("study") > -1) { | |
document.querySelector(".right").parentNode.appendChild(inject_container); | |
jisho_url = "http://jisho.org/search/"+document.querySelector(".kanji>span").textContent+"%23kanji"; | |
GM.xmlHttpRequest({ | |
method: "GET", | |
url: "http://www.ig.gmodules.com/gadgets/proxy/refresh=31556926&container=ig/" + jisho_url, | |
onload: function(response) { | |
var responseHTML = new DOMParser().parseFromString(response.responseText, "text/html"); | |
var trans = document.createElement("p"); | |
for (var i=0; i < languages.length; i++) { | |
var l = languages[i]; | |
if (l == 'english') { | |
mainMeanings = responseHTML.documentElement.querySelectorAll('div .kanji-details__main-meanings'); | |
trans.innerHTML = trans.innerHTML + 'ENGLISH: ' + (mainMeanings[0].innerHTML).replace(/,/g, ' /').replace(/\n/g, ''); | |
} else { | |
translations = responseHTML.documentElement.querySelectorAll('div .'+l+'_meanings ul li'); | |
var p = 0; | |
for (var ti=0; ti < translations.length; ti++) { | |
t = translations[ti]; | |
if (p === 0) { | |
trans.innerHTML = trans.innerHTML + l.toUpperCase()+': ' + t.innerHTML; | |
p = 1; | |
} else { | |
trans.innerHTML = trans.innerHTML + ' / ' + t.innerHTML; | |
} | |
} | |
} | |
trans.innerHTML = trans.innerHTML + '<br/>'; | |
} | |
inject_container.appendChild(trans); | |
var p2 = document.createElement("p"); | |
a = document.createElement("a"); | |
a.innerText = 'See on Jisho'; | |
a.href = jisho_url; | |
p2.style.float = 'left'; | |
p2.appendChild(a); | |
inject_container.appendChild(p2); | |
freq = responseHTML.documentElement.querySelectorAll('div .frequency strong'); | |
if (freq.length > 0) { | |
var p3 = document.createElement("p"); | |
p3.innerText = 'Freq: ' + freq[0].innerText + ' / 2500'; | |
p3.style.float = 'right'; | |
inject_container.appendChild(p3); | |
} | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment