Bunpro: Auto Commit 2.0

Automatically submits your answer once it's correct.

目前為 2019-07-27 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Bunpro: Auto Commit 2.0
// @namespace    http://tampermonkey.net/
// @version      2.0.9
// @description  Automatically submits your answer once it's correct.
// @author       Kumirei
// @include      *bunpro.jp/*
// @exclude      *community.bunpro.jp*
// @require      https://greasyfork.org/scripts/5392-waitforkeyelements/code/WaitForKeyElements.js?version=115012
// @require      https://greasyfork.org/scripts/370623-bunpro-helpful-events/code/Bunpro:%20Helpful%20Events.js?version=615700
// @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() {
		//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
	$('HTML')[0].addEventListener('new-review-item', function() {
		//if this is the first time running this code on the page then initialise
		if (!$('#AutoCommit').length) initialise();

		//find answer
		var lst = $('.level_lesson_info > a')[0].href.split("/");
		var id = lst[lst.length-1];
		answer = "";
		var sentence = parseSentence($('.study-question-japanese > div')[0]).replace(/\[.*\]$/g, '');
		var matchSentence = new RegExp('^'+ sentence +'$');
		var startIndex = sentence.match(/\./).index;
		var sentenceEnd = sentence.slice(startIndex + 2);
		var matchSentenceEnd = new RegExp(sentenceEnd + '$');

		var found = false;
		$('.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 == "") {
			//bug reporting
			var examples = '';
			$('.examples .japanese-example-sentence').each(function(i, e) {examples += 'Example: ' + parseSentence(e) +'\n';});
			if (bugReporting) {
				var lst = $('.level_lesson_info > a')[0].href.split("/");
				var id = lst[lst.length-1];
				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;
				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;
	localStorage.setItem('BPautoCommit', autoCommit);
}