WK Auto Commit

Auto commit for Wanikani

当前为 2022-05-07 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name WK Auto Commit
  3. // @namespace WKAUTOCOMMIT
  4. // @version 0.4
  5. // @description Auto commit for Wanikani
  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.4
  22. * Compatibility with Lightning mode from the Double-Check userscript
  23. * Compatibility with Katakana For On'yomi userscript
  24. * Version 0.3
  25. * Script works now on the Lessons page too
  26. * Version 0.2
  27. * Makes script work with Greasemonkey and Firefox
  28. * Version 0.1
  29. * Initial version
  30. *
  31. */
  32.  
  33. /* global $, wanakana */
  34.  
  35. /* jshint -W097 */
  36. 'use strict';
  37.  
  38. var activated = true;
  39. var click_threshold = 600;
  40.  
  41. var on_lessons_page = false;
  42.  
  43. var detect_lessons_page = function() {
  44. // Returns true if on lessons page
  45. var current_url = window.location.href;
  46. var lessonsPattern = /^http[s]?:\/\/www.wanikani.com\/lesson\/session.*/;
  47. return lessonsPattern.test(current_url);
  48. };
  49.  
  50. var is_userscript_lightningmode_active = function () {
  51. /* Returns true if "Lightning Mode" from Userscript Double-Check is active */
  52. return $('.doublecheck-active').length >= 1;
  53. };
  54.  
  55. var toggle = function () {
  56. if (activated) {
  57. // Deactivates WK Auto Commit mode
  58. $("#WKAUTOCOMMIT_button").prop('title', "Switch auto commit on");
  59. $("#WKAUTOCOMMIT_button").css({"opacity":"0.5"});
  60. $("#WKAUTOCOMMIT_button").text("Auto Commit is off");
  61. activated = false;
  62. } else {
  63. // Activates WK Auto Commit mode
  64. $("#WKAUTOCOMMIT_button").prop('title', "Switch auto commit off");
  65. $("#WKAUTOCOMMIT_button").css({"opacity":"1.0"});
  66. $("#WKAUTOCOMMIT_button").text("Auto Commit is on");
  67. activated = true;
  68. }
  69. };
  70.  
  71. var sanitize = function (str1) {
  72. var str2 = str1.replace(/\s/g, ''); // Removes Whitespaces
  73. str2 = str2.toLowerCase();
  74. str2 = wanakana.toRomaji(str2);
  75. return str2;
  76. };
  77.  
  78. var commit = function () {
  79. $("#answer-form form button").click();
  80. if (!is_userscript_lightningmode_active()) {
  81. setTimeout(function(){ $("#answer-form form button").click();}, click_threshold);
  82. }
  83. };
  84.  
  85. var check_input = function () {
  86. if (on_lessons_page) {
  87. var currentItem = $.jStorage.get("l/currentQuizItem");
  88. var currentquestiontype = $.jStorage.get("l/questionType");
  89. } else {
  90. var currentItem = $.jStorage.get("currentItem");
  91. var currentquestiontype = $.jStorage.get("questionType");
  92. }
  93. var currentresponse = $("#user-response").val();
  94. var currentitem_response = null;
  95.  
  96. // Get possible responses from current item depending on the task (reading or meaning)
  97. if (currentquestiontype === "meaning") {
  98. currentitem_response = currentItem.en;
  99. if (currentItem.syn) {
  100. currentitem_response = currentitem_response.concat(currentItem.syn);
  101. }
  102. } else if (currentquestiontype === "reading") {
  103. if (currentItem.voc) { // Vocab word
  104. currentitem_response = currentItem.kana;
  105. } else if (currentItem.emph === 'kunyomi') { // Kanji: Kun reading
  106. currentitem_response = currentItem.kun;
  107. } else if (currentItem.emph === 'onyomi') { // Kanji: On reading
  108. currentitem_response = currentItem.on;
  109. } else {
  110. console.log("WK Auto Commit: Could not find response");
  111. }
  112. }
  113.  
  114. for (var i in currentitem_response) {
  115. if (sanitize(currentresponse) === sanitize(currentitem_response[i])) {
  116. commit();
  117. }
  118. }
  119. };
  120.  
  121. var register_check_input = function () {
  122. $("#user-response").on("keyup", function (event) {
  123. if (activated) {
  124. check_input();
  125. }
  126. });
  127. };
  128.  
  129. var addButtons = function () {
  130. $("<div />", {
  131. id : "WKAUTOCOMMIT_button",
  132. title : "Toggle Auto Commit Mode",
  133. })
  134. .text("Auto Commit is on")
  135. .css({"background-color":"#C55"})
  136. .css({"opacity":"1"})
  137. .css({"display":"inline-block"})
  138. .css({"font-size":"0.8125em"})
  139. .css({"color":"#FFF"})
  140. .css({"cursor":"pointer"})
  141. .css({"padding":"10px"})
  142. .css({"vertical-align":"bottom"})
  143. .on("click", toggle)
  144. .prependTo("footer");
  145. };
  146.  
  147. var init = function () {
  148. console.log('WK Auto Commit (a plugin for Wanikani): Initialization started');
  149. on_lessons_page = detect_lessons_page();
  150. addButtons();
  151. register_check_input();
  152. console.log('WK Auto Commit: Initialization ended');
  153. };
  154.  
  155. $(function(){
  156. init();
  157. });
  158.