Greasy Fork

Greasy Fork is available in English.

Bunpro: Jisho Button

Searches the sentence on Jisho.

当前为 2018-07-05 提交的版本,查看 最新版本

// ==UserScript==
// @name         Bunpro: Jisho Button
// @namespace    http://tampermonkey.net/
// @version      0.3.6
// @description  Searches the sentence on Jisho.
// @author       Kumirei
// @include      *bunpro.jp/*
// @require      http://greasyfork.icu/scripts/5392-waitforkeyelements/code/WaitForKeyElements.js?version=115012
// @grant        none
// ==/UserScript==

(function() {
		//Wait until we're on the study page and can add the button
		waitForKeyElements('.study-question-japanese', function(e) {
				//add button
				if (!$('#buttonsBar').length) {
						$('#check-grammar').before('<div id="buttonsBar"><div class="flexWrapper"</div></div>');
						$('head').append('<style id="ButtonBarStyle">'+
										 '@media (max-width: 480px) {'+
										     '#buttonsBar .barButton {'+
										         'height: 30px;'+
										         'font-size: 12px;'+
										     '}'+
										 '}'+
										 '#buttonsBar {'+
										     'margin-top: 2.5px;'+
										 '}'+
										 '#buttonsBar .flexWrapper {'+
										     'height: 40px;'+
										     'display: flex;'+
										     'flex-wrap: wrap;'+
										     'margin: 0 -2.5px;'+
										 '}'+
										 '#buttonsBar .barButton {'+
										     'flex: 1;'+
										     'margin: 2.5px;'+
										 '}'+
										 '#buttonsBar .barButton input {'+
										     'background: rgba(25,34,49,0.8);'+
										     'height: 100% !important;'+
										     'color: white;'+
										     'border: 0;'+
										 '}'+
										 '#buttonsBar .barButton input:hover {'+
										     'color: rgb(103, 114, 124);'+
										 '}'+
										 '</style>');
				}
				$('#buttonsBar .flexWrapper').append('<div class="barButton"><input id="JishoButton" type="button" value="Jisho" onclick="window.open(\'https://www.jisho.org/search/\' + parseSentence($(\'.study-question-japanese > span\')[0]))"></div>');

				//Bind J to the Jisho button
				$('#study-answer-input').on('keyup', function(e) {
						if (e.which == 74 && $('#submit-study-answer').attr('value') == "Next") {
								$('#JishoButton').click();
						}
				});
		});
})();

//Extracts the sentence from the sentence elements
parseSentence = function(sentenceElem) {
		var sentence = "";
		sentenceElem.childNodes.forEach(function(elem) {
				// find the text in each kind of element and append it to the sentence string
				var name = elem.nodeName;
				if (name == "#text") {
						sentence += elem.data;
				}
				else if (name == "STRONG" || name == "SPAN") {
						if (name == "STRONG" && elem.children.length) {
								sentence += elem.children[0].childNodes[0].data;       // with kanji in url
								//sentence += elem.children[0].children[1].innerText;     // with kana in url
						}
						else {
								sentence += elem.innerText;
						}
				}
				else if (name == "RUBY") {
						sentence += elem.childNodes[0].data;       // with kanji in url
						//sentence += elem.children[1].innerText;     // with kana in url
				}
		});
		return sentence;
}