WK Auto Commit

Auto commit for Wanikani

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

  1. // ==UserScript==
  2. // @name WK Auto Commit
  3. // @namespace WKAUTOCOMMIT
  4. // @version 0.4.3
  5. // @description Auto commit for Wanikani
  6. // @author Johannes Mikulasch
  7. // @match http://www.wanikani.com/subjects/*
  8. // @match https://www.wanikani.com/subjects/*
  9. // @grant none
  10. // @run-at document-end
  11. // @license
  12. // ==/UserScript==
  13.  
  14. /*
  15. * WK Auto Commit
  16. * If you typed in the correct answer then it is automatically commited.
  17. * Therefore, you have to use the 'enter' key way less than before.
  18. *
  19. * Version 0.4.3
  20. * Bugfix: prevent a double commit when typing fast, which led to a shaking input window or in the worst case to
  21. * wrong input.
  22. * Version 0.4.2
  23. * Quickfix: adapt to Wanikani update, which was deployed on March 27th, 2023
  24. * (see https://community.wanikani.com/t/updates-to-lessons-reviews-and-extra-study/60912)
  25. * - removed jStorage and jQuery references
  26. * - changed the @match for the new lesson and review urls
  27. * - Note: did not check with compatibilites of other user scripts (like Lightning mode or Katakana for On'yomi) yet.
  28. * Version 0.4.1
  29. * Bugfix: call commit() at most one time for each item
  30. * (see https://community.wanikani.com/t/userscript-auto-commit-the-end-of-the-enter-key/11825/64)
  31. * Version 0.4
  32. * Compatibility with Lightning mode from the Double-Check userscript
  33. * Compatibility with Katakana For On'yomi userscript
  34. * Version 0.3
  35. * Script works now on the Lessons page too
  36. * Version 0.2
  37. * Makes script work with Greasemonkey and Firefox
  38. * Version 0.1
  39. * Initial version
  40. *
  41. */
  42.  
  43. /* jshint -W097 */
  44. 'use strict';
  45.  
  46. var activated = true;
  47. var click_threshold = 600;
  48.  
  49. let expected_answers = [];
  50.  
  51. var is_userscript_lightningmode_active = function () {
  52. /* Returns true if "Lightning Mode" from Userscript Double-Check is active */
  53. return document.querySelector('.doublecheck-active')?.length >= 1;
  54. };
  55.  
  56. var toggle = function () {
  57. var button = document.querySelector("#WKAUTOCOMMIT_button");
  58. if (activated) {
  59. // Deactivates WK Auto Commit mode
  60. button.title = "Switch auto commit on";
  61. button.style.opacity = 0.5;
  62. button.textContent = "Auto Commit is off";
  63. activated = false;
  64. } else {
  65. // Activates WK Auto Commit mode
  66. button.title = "Switch auto commit off";
  67. button.style.opacity = 1.0;
  68. button.textContent = "Auto Commit is on";
  69. activated = true;
  70. }
  71. };
  72.  
  73. var sanitize = function (str1) {
  74. var str2 = str1.replace(/\s/g, ''); // Removes Whitespaces
  75. str2 = str2.toLowerCase();
  76. return str2;
  77. };
  78.  
  79. var commit = function () {
  80. if(!commit.usable) return;
  81. const inputbutton = document.querySelector(".quiz-input__submit-button");
  82. inputbutton.click();
  83. if (!is_userscript_lightningmode_active()) {
  84. setTimeout(function(){ inputbutton.click();}, click_threshold);
  85. }
  86.  
  87. // Temporarily deactivate the commit function to prevent double commits
  88. commit.usable = false;
  89. };
  90.  
  91. var check_input = function () {
  92. // console.log("Checking Input", current_expected);
  93. const currentresponse = document.querySelector("#user-response").value;
  94. for (var i in expected_answers) {
  95. if (sanitize(currentresponse) === sanitize(expected_answers[i])) {
  96. commit();
  97. break;
  98. }
  99. }
  100. };
  101.  
  102. var register_check_input = function () {
  103. var userinput = document.querySelector("#user-response");
  104. userinput.onkeyup = function (event) {
  105. if (activated) {
  106. check_input();
  107. }
  108. };
  109. };
  110.  
  111. var addButton = function () {
  112. /* Define button */
  113. var button = document.querySelector("#WKAUTOCOMMIT_button");
  114. if (!button) {
  115. button = document.createElement("div");
  116. button.id = "WKAUTOCOMMIT_button";
  117. button.title = "Toggle Auto Commit Mode";
  118. button.textContent = "Auto Commit is on";
  119. button.style.backgroundColor = "#C55";
  120. button.style.opacity = 1;
  121. button.style.display = "inline-block";
  122. button.style.fontSize = "0.8125em";
  123. button.style.color = "#FFF";
  124. button.style.cursor = "pointer"
  125. button.style.padding = "10px";
  126. button.style.verticalAlign = "bottom";
  127. button.onclick = toggle;
  128.  
  129. /* Prepend button to footer */
  130. var body = document.querySelector("#turbo-body");
  131. body.appendChild(button);
  132. }
  133. };
  134.  
  135. /* React on a willShowNextQuestion event, which is triggered by WaniKani when a new question is shown */
  136. window.addEventListener("willShowNextQuestion", function(event) {
  137. //console.log("Received willShowNextQuestion event from WaniKani", event);
  138.  
  139. register_check_input();
  140. addButton();
  141.  
  142. /* Get expected answers from current item depending on the task (reading or meaning) */
  143. expected_answers = []
  144. const item = event.detail;
  145. const subject = item.subject;
  146. if (item.questionType === "meaning") {
  147. expected_answers = expected_answers.concat(subject.meanings);
  148. } else if (item.questionType === "reading") {
  149. if (subject.type === 'Vocabulary') {
  150. expected_answers = expected_answers.concat(subject.readings.map((e) => e.reading));
  151. } else if (subject.type === 'Kanji') {
  152. if (subject.primary_reading_type === 'kunyomi') {
  153. expected_answers = expected_answers.concat(subject.kunyomi);
  154. } else if (subject.primary_reading_type === 'onyomi') {
  155. expected_answers = expected_answers.concat(subject.onyomi);
  156. }
  157. }
  158. }
  159.  
  160. // Make the commit function usable again
  161. commit.usable = true;
  162. });
  163.  
  164. (function () {
  165. console.log('WK Auto Commit (a plugin for Wanikani): Initialized');
  166. })();
  167.