WK Auto Commit

Auto commit for Wanikani

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

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