WK Auto Commit (edited)

Auto commit for Wanikani with critical list edit.

目前為 2016-02-09 提交的版本,檢視 最新版本

  1. // ==UserScript==
  2. // @name WK Auto Commit (edited)
  3. // @namespace WKAUTOCOMMIT
  4. // @version 0.35
  5. // @description Auto commit for Wanikani with critical list edit.
  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.35
  22. * Edit by WillNiels to stop auto committing critical items
  23. * Version 0.3
  24. * Script works now on the Lessons page too
  25. * Version 0.2
  26. * Makes script work with Greasemonkey and Firefox
  27. * Version 0.1
  28. * Initial version
  29. *
  30. */
  31.  
  32.  
  33. /* jshint -W097 */
  34. 'use strict';
  35.  
  36. var activated = true;
  37. var click_threshold = 600;
  38.  
  39. var on_lessons_page = false;
  40.  
  41. var api_key;
  42. var crit_list = [];
  43. var critical = false;
  44.  
  45. //Anything less than this will not be autosubmitted!
  46. var percentCritical = '95';
  47.  
  48. var detect_lessons_page = function() {
  49. // Returns true if on lessons page
  50. var current_url = window.location.href;
  51. var lessonsPattern = /^http[s]?:\/\/www.wanikani.com\/lesson\/session.*/;
  52. return lessonsPattern.test(current_url);
  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. return str2;
  75. };
  76.  
  77. var commit = function () {
  78. console.log(crit_list);
  79. $("#answer-form form button").click();
  80. setTimeout(function(){ $("#answer-form form button").click();}, click_threshold);
  81. };
  82.  
  83. var check_input = function () {
  84.  
  85. if (on_lessons_page) {
  86. var currentItem = $.jStorage.get("l/currentQuizItem");
  87. var currentquestiontype = $.jStorage.get("l/questionType");
  88. } else {
  89. var currentItem = $.jStorage.get("currentItem");
  90. var currentquestiontype = $.jStorage.get("questionType");
  91. // If the item is critical, don't auto submit
  92. var thisitem = (currentItem.rad || currentItem.voc || currentItem.kan);
  93. for( var i in crit_list ){
  94. if( crit_list[i] == thisitem ){
  95. critical = true;
  96. }else{
  97. critical = false;
  98. }
  99. }
  100. }
  101.  
  102. var currentresponse = $("#user-response").val();
  103.  
  104. var currentitem_response = null;
  105.  
  106. // Get possible responses from current item depending on the task (reading or meaning)
  107. if (currentquestiontype === "meaning") {
  108. currentitem_response = currentItem.en;
  109. if (currentItem.syn) {
  110. currentitem_response = currentitem_response.concat(currentItem.syn);
  111. }
  112. } else if (currentquestiontype === "reading") {
  113. if (currentItem.voc) { // Vocab word
  114. currentitem_response = currentItem.kana;
  115. } else if (currentItem.emph === 'kunyomi') { // Kanji: Kun reading
  116. currentitem_response = currentItem.kun;
  117. } else if (currentItem.emph === 'onyomi') { // Kanji: On reading
  118. currentitem_response = currentItem.on;
  119. } else {
  120. console.log("WK Auto Commit: Could not find response");
  121. }
  122. }
  123.  
  124. for (var i in currentitem_response) {
  125. if (sanitize(currentresponse) === sanitize(currentitem_response[i]) && critical == false) {
  126. commit();
  127. }
  128. }
  129. };
  130.  
  131. var register_check_input = function () {
  132. $("#user-response").on("keyup", function (event) {
  133. if (activated) {
  134. check_input();
  135. }
  136. });
  137. };
  138.  
  139. var addButtons = function () {
  140.  
  141. $("<div />", {
  142. id : "WKAUTOCOMMIT_button",
  143. title : "Toggle Auto Commit Mode",
  144. })
  145. .text("Auto Commit is on")
  146. .css({"background-color":"#C55"})
  147. .css({"opacity":"1"})
  148. .css({"display":"inline-block"})
  149. .css({"font-size":"0.8125em"})
  150. .css({"color":"#FFF"})
  151. .css({"cursor":"pointer"})
  152. .css({"padding":"10px"})
  153. .css({"vertical-align":"bottom"})
  154. .on("click", toggle)
  155. .prependTo("footer");
  156. };
  157.  
  158. var init = function () {
  159. console.log('WK Auto Commit (a plugin for Wanikani): Initialization started');
  160. on_lessons_page = detect_lessons_page();
  161. addButtons();
  162. register_check_input();
  163. if(!localStorage.getItem('apiKey')){
  164. api_key = prompt('enter your api-key');
  165. }else{
  166. api_key = localStorage.getItem('apiKey');
  167. }
  168.  
  169. while (typeof api_key !== 'string' || api_key.length !== 32){
  170. api_key = prompt('Api-key incorrect format: Please re-enter your api-key.', api_key);
  171. }
  172.  
  173. localStorage.setItem('apiKey',api_key);
  174. crit_list = [];
  175. $.getJSON('/api/user/'+api_key+'/critical-items/' + percentCritical, function(json){
  176. if (json.error && json.error.code === 'user_not_found') {
  177. localStorage.removeItem('apiKey');
  178. }
  179. $(json.requested_information).each(function(i,v){
  180. try {
  181. var thing = v.character
  182. crit_list.push(thing);
  183. } catch(e) {}
  184. });
  185. });
  186. console.log('WK Auto Commit: Initialization ended');
  187. };
  188.  
  189. $(function(){
  190. init();
  191. });