Greasy Fork 还支持 简体中文。

WK Auto Commit

Auto commit for Wanikani

目前為 2016-01-31 提交的版本,檢視 最新版本

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