WK Auto Commit

Auto commit for Wanikani

当前为 2016-01-23 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name WK Auto Commit
  3. // @namespace WKAUTOCOMMIT
  4. // @version 0.1
  5. // @description Auto commit for Wanikani
  6. // @author Johannes Mikulasch
  7. // @match http*://www.wanikani.com/review/session*
  8. // @grant GM_addStyle
  9. // @license
  10. // ==/UserScript==
  11.  
  12. /*
  13. * WK Auto Commit
  14. * If you typed in the correct answer then it is automatically commited.
  15. * Therefore, you have to use the 'enter' key way less than before.
  16. */
  17.  
  18.  
  19. /* jshint -W097 */
  20. 'use strict';
  21.  
  22. var activated = true;
  23. var click_threshold = 600;
  24.  
  25. var toggle = function () {
  26. if (activated) {
  27. // Deactivates WK Auto Commit mode
  28. $("#WKAUTOCOMMIT_button").prop('title', "Switch auto commit on");
  29. $("#WKAUTOCOMMIT_button").css({"opacity":"0.5"});
  30. $("#WKAUTOCOMMIT_button").text("Auto Commit is off");
  31. activated = false;
  32. } else {
  33. // Activates WK Auto Commit mode
  34. $("#WKAUTOCOMMIT_button").prop('title', "Switch auto commit off");
  35. $("#WKAUTOCOMMIT_button").css({"opacity":"1.0"});
  36. $("#WKAUTOCOMMIT_button").text("Auto Commit is on");
  37. activated = true;
  38. }
  39. };
  40.  
  41. var sanitize = function (str1) {
  42. var str2 = str1.replace(/\s/g, ''); // Removes Whitespaces
  43. str2 = str2.toLowerCase();
  44. return str2;
  45. };
  46.  
  47. var commit = function () {
  48. $("#answer-form form button").click();
  49. setTimeout(function(){ $("#answer-form form button").click();}, click_threshold);
  50. };
  51.  
  52. var check_input = function () {
  53. var currentItem = $.jStorage.get("currentItem");
  54. var currentquestiontype = $.jStorage.get("questionType");
  55. var currentresponse = $("#user-response").val();
  56. var currentitem_response = null;
  57.  
  58. // Get possible responses from current item depending on the task (reading or meaning)
  59. if (currentquestiontype === "meaning") {
  60. currentitem_response = currentItem.en;
  61. if (currentItem.syn) {
  62. currentitem_response = currentitem_response.concat(currentItem.syn);
  63. }
  64. } else if (currentquestiontype === "reading") {
  65. if (currentItem.voc) { // Vocab word
  66. currentitem_response = currentItem.kana;
  67. } else if (currentItem.emph === 'kunyomi') { // Kanji: Kun reading
  68. currentitem_response = currentItem.kun;
  69. } else if (currentItem.emph === 'onyomi') { // Kanji: On reading
  70. currentitem_response = currentItem.on;
  71. } else {
  72. console.log("WK Auto Commit: Could not find response");
  73. }
  74. }
  75.  
  76. for (var i in currentitem_response) {
  77. if (sanitize(currentresponse) === sanitize(currentitem_response[i])) {
  78. commit();
  79. }
  80. }
  81. };
  82.  
  83. var register_check_input = function () {
  84. $("#user-response").on("keyup", function (event) {
  85. if (activated) {
  86. check_input();
  87. }
  88. });
  89. };
  90.  
  91. var loadCSS = function () {
  92. /*jshint multistr: true */
  93. GM_addStyle("\
  94. .WKAUTOCOMMIT_button { \
  95. background-color: #C55; \
  96. opacity: 1; \
  97. display: inline-block; \
  98. font-size: 0.8125em; \
  99. color: #FFFFFF; \
  100. cursor: pointer; \
  101. padding: 10px; \
  102. vertical-align: bottom; \
  103. } \
  104. ");
  105. };
  106.  
  107. var addButtons = function () {
  108. $("<div />", {
  109. id : "WKAUTOCOMMIT_button",
  110. title : "Toggle Auto Commit Mode",
  111. })
  112. .text("Auto Commit is on")
  113. .addClass("WKAUTOCOMMIT_button")
  114. .on("click", toggle)
  115. .prependTo("footer");
  116. };
  117.  
  118. var init = function () {
  119. console.log('WK Auto Commit (a plugin for Wanikani): Initialization started');
  120. loadCSS();
  121. addButtons();
  122. register_check_input();
  123. console.log('WK Auto Commit: Initialization ended');
  124. };
  125.  
  126. $(function(){
  127. init();
  128. });