Bunpro: Autocommit 2.0

Automatically submits your answer once it's correct.

当前为 2018-12-17 提交的版本,查看 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Bunpro: Autocommit 2.0
// @namespace    http://tampermonkey.net/
// @version      2.0.6
// @description  Automatically submits your answer once it's correct.
// @author       Kumirei
// @include      http://bunpro.jp*
// @include      https://bunpro.jp*
// @require      https://greasyfork.org/scripts/5392-waitforkeyelements/code/WaitForKeyElements.js?version=115012
// @require      https://greasyfork.org/scripts/370219-bunpro-buttons-bar/code/Bunpro:%20Buttons%20Bar.js?version=654288
// @grant        none
// ==/UserScript==

(function() {
		//if you want to get an alert when the script can't find the correct answer so you can report the bug to Kumi, set this to true
		var bugReporting = true;

		//sets the page up for continued running
		var answer = "";
		function initialise() {
				console.log('BAC: INITIALISING');
				//add the button
				autoCommit = (localStorage.getItem('BPautoCommit') == "false" ? false : true);
				buttonsBar.addButton('AutoCommit', 'Auto Commit ' + (autoCommit == true ? "ON" : "OFF"), 'toggleAutoCommit()');

				answer = "";
				var currentInput = "";
				//check answer each time a key is released
				$('#study-answer-input').on('keyup', function(e) {
						if (autoCommit) {
								//update current input value
								currentInput = $('#study-answer-input')[0].value;
								//if the input matches the stored answer, then submit it
								if (currentInput == answer) {
										$('#submit-study-answer').click();
								}
						}
				});
		}

		//update variables when a new item is detected
		waitForKeyElements('.level_lesson_info a', function(e) {
				//if this is the first time running this code on the page then initialise
				if (!$('#AutoCommit').length) initialise();

				//find answer
				answer = "";
				var sentence = parseSentence($('.study-question-japanese > div')[0]).replace(/\[.*\]$/g, '');
				var matchSentence = new RegExp('^'+ sentence.replace(/\?/, '\?') +'$');
				var startIndex = sentence.match(/\./).index;
				var sentenceEnd = sentence.slice(startIndex + 2);
				var matchSentenceEnd = new RegExp(sentenceEnd.replace(/\?/, '\?') + '$');
				$('.examples .japanese-example-sentence').each(function(i, e) {
						var parsedSentence = parseSentence(e);
						if (parsedSentence.match(matchSentence) != null) {
								var endIndex = parsedSentence.match(matchSentenceEnd).index;
								answer = parsedSentence.slice(startIndex, endIndex);
								console.log('ANSWER:', answer);
						}
				});
				if (answer == "") {
						console.log('No answer found');
						console.log('Sentence:', sentence);
						//bug reporting
						var examples = '';
						$('.examples .japanese-example-sentence').each(function(i, e) {examples += 'Example: ' + parseSentence(e) +'\n';});
						if (bugReporting) {
								var id = $('#grammar_point_id')[0].value;
								var text = '"Bunpro: Autocommit" could not find the answer to this question.'+
									'\nCopy the following and sent it to Kumi on the Bunpro forums.'+
									'\n\nIf you don\'t want to receive these bug alerts anymore, disable them by setting bugReporting = false at the top of the script.\n'+
									'\nGrammar ID: ' + id +
									'\nSentence: ' + sentence +
									'\n' + examples;
								console.log(examples);
								alert(text);
						}
				}
		});

		//Extracts the sentence from the sentence elements
		function parseSentence(sentenceElem) {
				var text = "";
				sentenceElem.childNodes.forEach(function(elem) {
						// find the text in each kind of element and append it to the text string
						var name = elem.nodeName;
						if (name == "#text") {
								text += elem.data;
						} else if (name == "STRONG" || name == "SPAN") {
								if (elem.className == "chui") {
										if (!elem.children.length) text += elem.innerText
										else text += elem.children[0].children[1].innerText;
								} else if (elem.className == "study-area-input") {
										text += ".*";
								} else if (elem.className == "name-highlight") {
										text += parseSentence(elem);
								} else if (name == "STRONG" && elem.children.length) {
										text += parseSentence(elem);
								} else {
										text += elem.innerText;
								}
						} else if (name == "RUBY") {
								text += elem.children[1].innerText;
						}
				});
				text = text.replace(/(\(.*\))|(\[.*\])/, '');
				return text;
		}
})();

toggleAutoCommit = function() {
		autoCommit = !autoCommit;
		var name = "Auto Commit " + (autoCommit == true ? "ON" : "OFF");
		$('#AutoCommit')[0].value = name;
		console.log(name);
		localStorage.setItem('BPautoCommit', autoCommit);
}