Bunpro: Jisho Button

Searches the sentence on Jisho.

目前為 2018-07-04 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Bunpro: Jisho Button
// @namespace    http://tampermonkey.net/
// @version      0.3.0
// @description  Searches the sentence on Jisho.
// @author       Kumirei
// @include      https://bunpro.jp/*
// @require      https://greasyfork.org/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 styling to button
				$('head').append('<style id="JishoButtonStyle">'+
								 '#buttonsBar {'+
								     'margin: 5px 0;'+
								     'height: 40px;'+
								     'width: 100%;'+
								     'justify-content: center;'+
								     'display: flex;'+
								 '}'+
								 '.barButton {'+
								     'width: inherit;'+
								 '}'+
								 '.barButton:not(:first-child):not(:last-child) {'+
								     'margin: 0 2.5px;'+
								 '}'+
								 '.barButton:first-child {'+
								     'margin-right: 2.5px;'+
								 '}'+
								 '.barButton:last-child {'+
								     'margin-left: 2.5px;'+
								 '}'+
								 '.barButton input {'+
								     'background: rgba(25,34,49,0.8);'+
								     'height: 100% !important;'+
								     'color: white;'+
								     'border: 0;'+
								 '}'+
								 '.barButton input:hover {'+
								     'color: rgb(103, 114, 124);'+
								 '}'+
								 '</style>');


				//Add button
				if (!$('#buttonsBar').length) $('#check-grammar').before('<div id="buttonsBar"></div>');
				$('#buttonsBar').append('<div class="barButton"><input id="JishoButton" type="button" value="Jisho" onclick="window.open(\'https://www.jisho.org/search/\')"></div>');

				//Changes the target url of the button when item changes
				waitForKeyElements('.study-question-japanese > span', function(e) {
						var sentence = parseSentence(e[0]);
						$('#JishoButton').attr('onclick', 'window.open("https://jisho.org/search/' + sentence + '")');
				});

				//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
		function parseSentence(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;
		}
})();