WaniKani Lesson Ordering II

This allows you to sort the items in your WaniKani lessons, either putting the Radicals and Kanji first, or the Vocabulary first. By RhosVeedcy, based on scripts originally by Alucardeck.

  1. // ==UserScript==
  2. // @name WaniKani Lesson Ordering II
  3. // @namespace https://www.wanikani.com
  4. // @description This allows you to sort the items in your WaniKani lessons, either putting the Radicals and Kanji first, or the Vocabulary first. By RhosVeedcy, based on scripts originally by Alucardeck.
  5. // @version 1.3.4
  6. // @include http://www.wanikani.com/lesson/session
  7. // @include https://www.wanikani.com/lesson/session
  8. // @run-at document-end
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12.  
  13. function get(id) {
  14. if (id && typeof id === 'string') {
  15. id = document.getElementById(id);
  16. }
  17. return id || null;
  18. }
  19.  
  20.  
  21.  
  22. function simulateKeyPressA() {
  23. var f4Event = document.createEvent("KeyboardEvent");
  24. var evtTarget = get("lesson");
  25. f4Event.initKeyEvent(
  26. "keydown", // event type
  27. true, // if your event can bubble
  28. true, // if your event is cancelable
  29. window, // abstract view (an obscure DOM requirement)
  30. false, // if ctrlKey is down
  31. false, // if altKey is down
  32. false, // if shiftKey is down
  33. false, // if the "meta" key is down (usually, the Windows key)
  34. 65, // the keyCode -- 65 is keyCode for “a”
  35. 65 // the charCode
  36. );
  37. evtTarget.dispatchEvent(f4Event);
  38. console.log('simulateKeyPressA() keydown');
  39. var f4Event2 = document.createEvent("KeyboardEvent");
  40. f4Event2.initKeyEvent(
  41. "keyup", // event type
  42. true, // if your event can bubble
  43. true, // if your event is cancelable
  44. window, // abstract view (an obscure DOM requirement)
  45. false, // if ctrlKey is down
  46. false, // if altKey is down
  47. false, // if shiftKey is down
  48. false, // if the "meta" key is down (usually, the Windows key)
  49. 65, // the keyCode -- 65 is keyCode for “a”
  50. 65 // the charCode
  51. );
  52. evtTarget.dispatchEvent(f4Event2);
  53. console.log('simulateKeyPressA() keyup');
  54. }
  55.  
  56.  
  57. function cancelOffer() {
  58.  
  59. console.log("cancelOffer()");
  60. var divSt = get("divSt");
  61. hideButtons();
  62. divSt.innerHTML = '';
  63. clearInterval(cancelInt);
  64. }
  65.  
  66.  
  67. function checkForMovement(){
  68.  
  69. console.log('checkForMovement()');
  70.  
  71. var lastRead = $.jStorage.get("l/lastRead", -1);
  72. if (lastRead >= 0) {
  73.  
  74. cancelOffer();
  75. }
  76. }
  77.  
  78.  
  79.  
  80. function init(){
  81.  
  82. console.log('init() start');
  83.  
  84. /*
  85. $.jStorage.flush();
  86. $.jStorage.reInit();
  87.  
  88. var rads = $.jStorage.get("l/count/rad", 1); -- can't rely on these being inited properly... commenting out this block for now
  89. var kans = $.jStorage.get("l/count/kan", 1);
  90. var vocs = $.jStorage.get("l/count/voc", 1);
  91.  
  92. // if all three are zero, it means WK just hasn't initialized them yet
  93. if ((rads || kans || vocs) && ((vocs == 0 && (rads == 0 || kans == 0)) || (vocs > 0 && rads == 0 && kans == 0))) {
  94.  
  95. return false; // no need for reordering
  96. }
  97. */
  98.  
  99. var stats = $("#stats")[0];
  100. var t = document.createElement('div');
  101. var t2 = document.createElement('div');
  102. stats.appendChild(t);
  103. stats.appendChild(t2);
  104.  
  105. t.innerHTML = '<div id="divSt">Click below to reorder lesson items</div>'+
  106. '<button id="reorderBtn" type="button" onclick="window.dispatchEvent(new Event(\'reorderRKevt\'));">Radicals and Kanji first</button>'+
  107. '</div>';
  108. t2.innerHTML = '<button id="reorderVBtn" type="button" onclick="window.dispatchEvent(new Event(\'reorderVevt\'));">Vocabulary items first</button>'+
  109. '</div>';
  110.  
  111. window.addEventListener('reorderRKevt',reorder);
  112. window.addEventListener('reorderVevt',reorderV);
  113.  
  114. /*
  115. var theElem;
  116.  
  117. if (rads == 0) { -- can't rely on these being inited properly... commenting out this block for now
  118.  
  119. theElem = get("reorderBtn");
  120. theElem.innerHTML = "Kanji first";
  121. theElem = get("reorderVBtn");
  122. theElem.innerHTML = "Vocabulary first";
  123. }
  124.  
  125. if (kans == 0) {
  126.  
  127. theElem = get("reorderBtn");
  128. theElem.innerHTML = "Radicals first";
  129. theElem = get("reorderVBtn");
  130. theElem.innerHTML = "Vocabulary first";
  131. }
  132.  
  133. if (vocs == 0) {
  134.  
  135. theElem = get("reorderBtn");
  136. theElem.innerHTML = "Radicals first";
  137.  
  138. theElem = get("reorderVBtn");
  139. theElem.style.visibility="hidden";
  140. }
  141. */
  142.  
  143. console.log('init() end');
  144. return true;
  145. }
  146.  
  147. function hideButtons(){
  148. var reorderBtn = get("reorderBtn");
  149. var reorderVBtn = get("reorderVBtn");
  150. reorderBtn.style.visibility="hidden";
  151. reorderVBtn.style.visibility="hidden";
  152. }
  153.  
  154. function reorder(){
  155. console.log('reorder() start');
  156. var divSt = get("divSt");
  157. hideButtons();
  158. divSt.innerHTML = 'Reordering.. please wait!';
  159.  
  160. var f4Event = document.createEvent("KeyboardEvent");
  161. var strt = 0;
  162. if (! f4Event.initKeyEvent) { strt++; }
  163.  
  164. var actList = $.jStorage.get("l/activeQueue");
  165. var lesList = $.jStorage.get("l/lessonQueue");
  166. var removedCount = 0;
  167. for(var i=strt;i<actList.length;i++){
  168. var it = actList[i];
  169. actList.splice(i--,1);
  170. lesList.push(it);
  171. removedCount++;
  172. }
  173. console.log('Items removed from ActiveQueue: '+removedCount);
  174. for(var i=lesList.length-1;i>=0;i--){
  175. var it=lesList[i];
  176. if(it.kan){
  177. lesList.splice(i,1);
  178. lesList.push(it);
  179. }
  180. }
  181. for(var i=lesList.length-1;i>=0;i--){
  182. var it=lesList[i];
  183. if(it.rad){
  184. lesList.splice(i,1);
  185. lesList.push(it);
  186. }
  187. }
  188. for(var i=0;i<removedCount;i++){
  189. actList.push(lesList.pop());
  190. }
  191.  
  192. lesList.reverse();
  193. console.log('Ordered LessonQueue:');
  194. for(var i=0;i<lesList.length;i++){
  195. var it=lesList[i];
  196. if(it.rad)
  197. console.log('rad '+it.rad);
  198. else if(it.kan)
  199. console.log('kan '+it.kan);
  200. else if(it.voc)
  201. console.log('voc '+it.voc);
  202. }
  203. $.jStorage.set("l/lessonQueue",lesList);
  204. $.jStorage.set("l/activeQueue",actList);
  205. if (strt == 0) {
  206. for(var i=0;i<actList.length;i++){
  207. simulateKeyPressA();
  208. }
  209. }
  210. divSt.innerHTML = 'Ordered: radicals and kanji first';
  211. clearInterval(cancelInt);
  212.  
  213. console.log('reorder() end');
  214. }
  215.  
  216. function reorderV(){
  217. console.log('reorderV() start');
  218. var divSt = get("divSt");
  219. hideButtons();
  220. divSt.innerHTML = 'Reordering.. please wait!';
  221.  
  222. var f4Event = document.createEvent("KeyboardEvent");
  223. var strt = 0;
  224. if (! f4Event.initKeyEvent) { strt++; }
  225.  
  226. var actList = $.jStorage.get("l/activeQueue");
  227. var lesList = $.jStorage.get("l/lessonQueue");
  228. var removedCount = 0;
  229. for(var i=strt;i<actList.length;i++){
  230. var it = actList[i];
  231. actList.splice(i--,1);
  232. lesList.push(it);
  233. removedCount++;
  234. }
  235. console.log('Items removed from ActiveQueue: '+removedCount);
  236. for(var i=lesList.length-1;i>=0;i--){
  237. var it=lesList[i];
  238. if(it.voc){
  239. lesList.splice(i,1);
  240. lesList.push(it);
  241. }
  242. }
  243. for(var i=0;i<removedCount;i++){
  244. actList.push(lesList.pop());
  245. }
  246. lesList.reverse();
  247.  
  248. console.log('Ordered LessonQueue:');
  249. for(var i=0;i<lesList.length;i++){
  250. var it=lesList[i];
  251. if(it.rad)
  252. console.log('rad '+it.rad);
  253. else if(it.kan)
  254. console.log('kan '+it.kan);
  255. else if(it.voc)
  256. console.log('voc '+it.voc);
  257. }
  258. $.jStorage.set("l/lessonQueue",lesList);
  259. $.jStorage.set("l/activeQueue",actList);
  260. if (strt == 0) {
  261. for(var i=0;i<actList.length;i++){
  262. simulateKeyPressA();
  263. }
  264. }
  265. divSt.innerHTML = 'Ordered: vocabulary first';
  266. clearInterval(cancelInt);
  267.  
  268. console.log('reorderV() end');
  269. }
  270.  
  271.  
  272. function main() {
  273.  
  274. if (init()) {
  275. cancelInt = setInterval(function(){checkForMovement()},100);
  276. }
  277. }
  278.  
  279.  
  280.  
  281. //console.log('WK Lesson Ordering II script load start');
  282.  
  283. var cancelInt;
  284.  
  285. //window.addEventListener("load", main, false);
  286.  
  287. console.log('WK Lesson Ordering II script load end');
  288.  
  289. main();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址