Greasy Fork

Greasy Fork is available in English.

Bunpro: Copy Sentences

Allows you to click Japanese sentences and their translations to copy them to clipboard.

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

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Bunpro: Copy Sentences
// @namespace    http://tampermonkey.net/
// @version      0.2.0
// @description  Allows you to click Japanese sentences and their translations to copy them to clipboard.
// @author       Kumirei
// @include      https://bunpro.jp/*
// @require      http://greasyfork.icu/scripts/5392-waitforkeyelements/code/WaitForKeyElements.js?version=115012
// @grant        none
// ==/UserScript==

(function() {
		waitForKeyElements('body', function(e) {
				e.on('click', function(e) {
						//reviews
						if ($(e.target).closest('div')[0] == null) copyText($('div[class^="study-question-english"] > span')[0].innerText); //english in review is a special case
						else if ($(e.target.closest('div'))[0].className == "study-question-japanese") copyText(parseSentence($(e.target.closest('div > span'))[0])); //japanese in reviews
						//english example sentences
						else if (["english-example-sentence", "example-sentence-english"].includes(e.target.className)) copyText(e.target.innerText); //while studying
						else if (e.target.className == "example-sentence-english hide-english") copyText(e.target.innerText); //grammar page
						else if (["english-example-sentence", "example-sentence-english"].includes(e.target.parentNode.className)) copyText(e.target.parentNode.innerText);
						//japanese example sentences
						else if (e.target.className == "japanese-example-sentence") copyText(parseSentence(e.target));
						else if ($(e.target).closest('li')[0] != undefined && $(e.target).closest('li')[0].className == "japanese-example-sentence") copyText(parseSentence($(e.target).closest('li')[0]));

				});
		});

		//add buttons
		waitForKeyElements('#check-grammar', function(e) {
				if (!$('#buttonsBar').length) {
						$('#check-grammar').before('<div id="buttonsBar"></div>');
						$('head').append('<style id="ButtonBarStyle">'+
										 '#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:only-child {'+
										 'margin: 0;'+
										 '}'+
										 '.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>');
				}
				$('#buttonsBar').append('<div class="barButton"><input id="copyJP" type="button" value="Copy JP" onclick="copyText(parseSentence($(\'.study-question-japanese > span\')[0]));"></div>');
				$('#buttonsBar').append('<div class="barButton"><input id="copyEN" type="button" value="Copy EN" onclick="copyText($(\'.study-question-english-hint > span\')[0].innerText);"></div>');
		});
})();

//Extracts the sentence from the sentence element
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 string
								//sentence += elem.children[0].children[1].innerText;     // with kana in string
						}
						else {
								sentence += elem.innerText;
						}
				}
				else if (name == "RUBY") {
						sentence += elem.childNodes[0].data;       // with kanji in string
						//sentence += elem.children[1].innerText;     // with kana in string
				}
		});
		return sentence;
}

//copies the text
copyText = function(text) {
		var textArea = document.createElement("textarea");
		textArea.style.position = 'fixed';
		textArea.style.top = 0;
		textArea.style.left = 0;
		textArea.style.width = '2em';
		textArea.style.height = '2em';
		textArea.style.padding = 0;
		textArea.style.border = 'none';
		textArea.style.outline = 'none';
		textArea.style.boxShadow = 'none';
		textArea.style.background = 'transparent';
		textArea.value = text;
		document.body.appendChild(textArea);
		textArea.focus();
		textArea.select();
		try {
				var successful = document.execCommand('copy');
				var msg = successful ? 'successful' : 'unsuccessful';
				console.log('Copying text command was ' + msg);
				console.log('Copied Sentence:', text);
		} catch (err) {
				console.log('Oops, unable to copy');
		}

		document.body.removeChild(textArea);
}