WK Auto Commit (edited)

Auto commit for Wanikani with critical list edit.

目前為 2016-02-09 提交的版本,檢視 最新版本

  1. // ==UserScript==
  2. // @name WK Auto Commit (edited)
  3. // @namespace WKAUTOCOMMIT
  4. // @version 0.36
  5. // @description Auto commit for Wanikani with critical list edit.
  6. // @author Johannes Mikulasch
  7. // @match http://www.wanikani.com/review/session*
  8. // @match https://www.wanikani.com/review/session*
  9. // @match http://www.wanikani.com/lesson/session*
  10. // @match https://www.wanikani.com/lesson/session*
  11. // @grant none
  12. // @run-at document-end
  13. // @license
  14. // ==/UserScript==
  15.  
  16. /*
  17. * WK Auto Commit
  18. * If you typed in the correct answer then it is automatically commited.
  19. * Therefore, you have to use the 'enter' key way less than before.
  20. *
  21. * Version 0.35
  22. * Edit by WillNiels to stop auto committing critical items
  23. * Version 0.3
  24. * Script works now on the Lessons page too
  25. * Version 0.2
  26. * Makes script work with Greasemonkey and Firefox
  27. * Version 0.1
  28. * Initial version
  29. *
  30. */
  31.  
  32.  
  33. /* jshint -W097 */
  34. 'use strict';
  35.  
  36. var activated = true;
  37. var click_threshold = 600;
  38.  
  39. var on_lessons_page = false;
  40.  
  41.  
  42. //Anything less than this will not be autosubmitted!
  43. var percentCritical = '95';
  44.  
  45. var api_key;
  46. var crit_list = [];
  47.  
  48.  
  49. var detect_lessons_page = function() {
  50. // Returns true if on lessons page
  51. var current_url = window.location.href;
  52. var lessonsPattern = /^http[s]?:\/\/www.wanikani.com\/lesson\/session.*/;
  53. return lessonsPattern.test(current_url);
  54. };
  55.  
  56. var toggle = function () {
  57. if (activated) {
  58. // Deactivates WK Auto Commit mode
  59. $("#WKAUTOCOMMIT_button").prop('title', "Switch auto commit on");
  60. $("#WKAUTOCOMMIT_button").css({"opacity":"0.5"});
  61. $("#WKAUTOCOMMIT_button").text("Auto Commit is off");
  62. activated = false;
  63. } else {
  64. // Activates WK Auto Commit mode
  65. $("#WKAUTOCOMMIT_button").prop('title', "Switch auto commit off");
  66. $("#WKAUTOCOMMIT_button").css({"opacity":"1.0"});
  67. $("#WKAUTOCOMMIT_button").text("Auto Commit is on");
  68. activated = true;
  69. }
  70. };
  71.  
  72. var sanitize = function (str1) {
  73. var str2 = str1.replace(/\s/g, ''); // Removes Whitespaces
  74. str2 = str2.toLowerCase();
  75. return str2;
  76. };
  77.  
  78. var commit = function () {
  79. $("#answer-form form button").click();
  80. setTimeout(function(){ $("#answer-form form button").click();}, click_threshold);
  81. };
  82.  
  83. var check_input = function () {
  84.  
  85. if (on_lessons_page) {
  86. var currentItem = $.jStorage.get("l/currentQuizItem");
  87. var currentquestiontype = $.jStorage.get("l/questionType");
  88. } else {
  89. var currentItem = $.jStorage.get("currentItem");
  90. var currentquestiontype = $.jStorage.get("questionType");
  91. // If the item is critical, don't auto submit
  92. var thisitem = (currentItem.rad || currentItem.voc || currentItem.kan);
  93. for( var i in crit_list ){
  94. if( crit_list[i] == thisitem ){
  95. return;
  96. }
  97. }
  98. }
  99.  
  100. var currentresponse = $("#user-response").val();
  101.  
  102. var currentitem_response = null;
  103.  
  104. // Get possible responses from current item depending on the task (reading or meaning)
  105. if (currentquestiontype === "meaning") {
  106. currentitem_response = currentItem.en;
  107. if (currentItem.syn) {
  108. currentitem_response = currentitem_response.concat(currentItem.syn);
  109. }
  110. } else if (currentquestiontype === "reading") {
  111. if (currentItem.voc) { // Vocab word
  112. currentitem_response = currentItem.kana;
  113. } else if (currentItem.emph === 'kunyomi') { // Kanji: Kun reading
  114. currentitem_response = currentItem.kun;
  115. } else if (currentItem.emph === 'onyomi') { // Kanji: On reading
  116. currentitem_response = currentItem.on;
  117. } else {
  118. console.log("WK Auto Commit: Could not find response");
  119. }
  120. }
  121.  
  122. for (var i in currentitem_response) {
  123. if (sanitize(currentresponse) === sanitize(currentitem_response[i]) ) {
  124. console.log("noncritical item", currentItem, crit_list);
  125. commit();
  126. }
  127. }
  128. };
  129.  
  130. var register_check_input = function () {
  131. $("#user-response").on("keyup", function (event) {
  132. if (activated) {
  133. check_input();
  134. }
  135. });
  136. };
  137.  
  138. var addButtons = function () {
  139.  
  140. $("<div />", {
  141. id : "WKAUTOCOMMIT_button",
  142. title : "Toggle Auto Commit Mode",
  143. })
  144. .text("Auto Commit is on")
  145. .css({"background-color":"#C55"})
  146. .css({"opacity":"1"})
  147. .css({"display":"inline-block"})
  148. .css({"font-size":"0.8125em"})
  149. .css({"color":"#FFF"})
  150. .css({"cursor":"pointer"})
  151. .css({"padding":"10px"})
  152. .css({"vertical-align":"bottom"})
  153. .on("click", toggle)
  154. .prependTo("footer");
  155. };
  156.  
  157. var init = function () {
  158. console.log('WK Auto Commit (a plugin for Wanikani): Initialization started');
  159. on_lessons_page = detect_lessons_page();
  160. addButtons();
  161. register_check_input();
  162. if(!localStorage.getItem('apiKey')){
  163. api_key = prompt('enter your api-key');
  164. }else{
  165. api_key = localStorage.getItem('apiKey');
  166. }
  167.  
  168. while (typeof api_key !== 'string' || api_key.length !== 32){
  169. api_key = prompt('Api-key incorrect format: Please re-enter your api-key.', api_key);
  170. }
  171.  
  172. localStorage.setItem('apiKey',api_key);
  173. crit_list = [];
  174. $.getJSON('/api/user/'+api_key+'/critical-items/' + percentCritical, function(json){
  175. if (json.error && json.error.code === 'user_not_found') {
  176. localStorage.removeItem('apiKey');
  177. }
  178. $(json.requested_information).each(function(i,v){
  179. try {
  180. var thing = v.character
  181. crit_list.push(thing);
  182. } catch(e) {}
  183. });
  184. });
  185. console.log('WK Auto Commit: Initialization ended');
  186. };
  187.  
  188. $(function(){
  189. init();
  190. });