WaniKani Turtle Total

This will optionally add the cumulative turtle counts for each category to the srs-progress display. It will also mark increases and decreases in all the counts since the last display (for example, right after a review session.) By RhosVeedcy.

  1. // ==UserScript==
  2. // @name WaniKani Turtle Total
  3. // @namespace https://www.wanikani.com
  4. // @description This will optionally add the cumulative turtle counts for each category to the srs-progress display. It will also mark increases and decreases in all the counts since the last display (for example, right after a review session.) By RhosVeedcy.
  5. // @version 3.2.0
  6. // @include https://www.wanikani.com/
  7. // @include https://www.wanikani.com/dashboard
  8. // @run-at document-end
  9. // @grant GM_registerMenuCommand
  10. // ==/UserScript==
  11.  
  12.  
  13.  
  14.  
  15. function GMsetup() {
  16. if (GM_registerMenuCommand) {
  17.  
  18. GM_registerMenuCommand('WaniKani Turtle Total: Show/Hide overall totals', function() {
  19. var curDType = localStorage.getItem("WKturtotlsDType") || "0";
  20. curDType = JSON.parse(curDType);
  21. curDType++;
  22. curDType %= 2;
  23. localStorage.setItem("WKturtotlsDType", JSON.stringify(curDType));
  24. location.reload();
  25. });
  26.  
  27. GM_registerMenuCommand('WaniKani Turtle Total: Reverse direction', function() {
  28. var curDirection = localStorage.getItem("WKturtotlsDirection") || "0";
  29. curDirection = JSON.parse(curDirection);
  30. curDirection++;
  31. curDirection %= 2;
  32. localStorage.setItem("WKturtotlsDirection", JSON.stringify(curDirection));
  33. sessionStorage.removeItem("WKturtotPrevs");
  34. location.reload();
  35. });
  36.  
  37. }
  38. }
  39.  
  40.  
  41.  
  42. function totalTheTurtles() { // cumulative (enl = enl + bur, etc.)
  43.  
  44. console.log("totalTheTurtles()");
  45.  
  46. var burEl = document.getElementById("burned");
  47. var enlEl = document.getElementById("enlightened");
  48. var masEl = document.getElementById("master");
  49. var gurEl = document.getElementById("guru");
  50. var appEl = document.getElementById("apprentice");
  51.  
  52. var burned = Number( burEl.getElementsByTagName("span")[0].innerHTML || 0 );
  53. var enlightened = Number( enlEl.getElementsByTagName("span")[0].innerHTML || 0 );
  54. var master = Number( masEl.getElementsByTagName("span")[0].innerHTML || 0 );
  55. var guru = Number( gurEl.getElementsByTagName("span")[0].innerHTML || 0 );
  56. var apprentice = Number( appEl.getElementsByTagName("span")[0].innerHTML || 0 );
  57.  
  58. console.log("raw counts: ", apprentice, guru, master, enlightened, burned);
  59.  
  60. var curDirection = localStorage.getItem("WKturtotlsDirection") || "0";
  61. curDirection = JSON.parse(curDirection);
  62.  
  63. var totBu = burned;
  64. var totEn;
  65. var totMa;
  66. var totGu;
  67. var totAp = apprentice;
  68.  
  69. if (curDirection == 0) {
  70.  
  71. totEn = enlightened + burned;
  72. totMa = master + totEn;
  73. totGu = guru + totMa;
  74. totAp = apprentice + totGu;
  75. } else {
  76.  
  77. totGu = apprentice + guru;
  78. totMa = master + totGu;
  79. totEn = enlightened + totMa;
  80. totBu = burned + totEn;
  81. }
  82.  
  83. console.log("cumulative counts: ", totAp, totGu, totMa, totEn, totBu);
  84.  
  85. var curDType = localStorage.getItem("WKturtotlsDType") || "0";
  86. curDType = JSON.parse(curDType);
  87.  
  88. if (curDType == 0) {
  89.  
  90. if (curDirection == 0) {
  91.  
  92. if (burned != 0) {
  93.  
  94. document.getElementById("enlightened").getElementsByTagName("span")[0].innerHTML += " ( " + totEn;
  95. }
  96. if (enlightened != 0) {
  97.  
  98. document.getElementById("master").getElementsByTagName("span")[0].innerHTML += " ( " + totMa;
  99. }
  100. if (master != 0) {
  101.  
  102. document.getElementById("guru").getElementsByTagName("span")[0].innerHTML += " ( " + totGu;
  103. }
  104. if (guru != 0) {
  105.  
  106. document.getElementById("apprentice").getElementsByTagName("span")[0].innerHTML += " ( " + totAp;
  107. }
  108.  
  109. } else {
  110.  
  111. document.getElementById("enlightened").getElementsByTagName("span")[0].innerHTML += " ) " + totEn;
  112. document.getElementById("master").getElementsByTagName("span")[0].innerHTML += " ) " + totMa;
  113. document.getElementById("guru").getElementsByTagName("span")[0].innerHTML += " ) " + totGu;
  114. document.getElementById("burned").getElementsByTagName("span")[0].innerHTML += " ) " + totBu;
  115. }
  116. }
  117.  
  118. burEl.style.padding = "30px 0px 22.5px";
  119. enlEl.style.padding = "30px 0px 22.5px";
  120. masEl.style.padding = "30px 0px 22.5px";
  121. gurEl.style.padding = "30px 0px 22.5px";
  122. appEl.style.padding = "30px 0px 22.5px";
  123.  
  124. return [apprentice, guru, master, enlightened, burned, totAp, totGu, totMa, totEn, totBu];
  125. }
  126.  
  127.  
  128.  
  129. function markTheDiffs (cur, prev) {
  130.  
  131. console.log("markTheDiffs()");
  132.  
  133. // check for null
  134.  
  135. if (!prev || !cur) { return; }
  136.  
  137. var uparrow = "↑";
  138. var downarrow = "↓";
  139. var tags = ["apprentice", "guru", "master", "enlightened", "burned"];
  140.  
  141. var curDirection = localStorage.getItem("WKturtotlsDirection") || "0";
  142. curDirection = JSON.parse(curDirection);
  143.  
  144. if ((curDirection == 0 && (cur[5] != prev[5])) || (curDirection == 1 && (cur[9] != prev[9]))) {
  145.  
  146. // if total active turtles decreased... must be a different account... don't mark anything.
  147. // if total active turtles increased... user did lesson, not review... don't mark anything.
  148. return;
  149. }
  150.  
  151. for (var x=0; x<5; x++) {
  152.  
  153. var theEl = document.getElementById(tags[x]).getElementsByTagName("span")[0];
  154.  
  155. if (cur[x] > prev[x]) {
  156.  
  157. theEl.innerHTML = uparrow + theEl.innerHTML;
  158. }
  159. else if (cur[x] < prev[x]) {
  160.  
  161. theEl.innerHTML = downarrow + theEl.innerHTML;
  162. }
  163. if ((theEl.innerHTML.indexOf(")") > 0) || (theEl.innerHTML.indexOf("(") > 0)) {
  164.  
  165. if (cur[x+5] > prev[x+5]) {
  166.  
  167. theEl.innerHTML += uparrow;
  168. }
  169. else if (cur[x+5] < prev[x+5]) {
  170.  
  171. theEl.innerHTML += downarrow;
  172. }
  173. }
  174. }
  175. }
  176.  
  177.  
  178.  
  179. function main () {
  180.  
  181. console.log('Turle Total main()');
  182.  
  183. GMsetup();
  184.  
  185. // calculate and display current totals
  186.  
  187. var curTotals = totalTheTurtles();
  188. var ct = JSON.stringify(curTotals);
  189. console.log('curTotals: ', ct);
  190.  
  191. // get previous totals
  192.  
  193. var pt = sessionStorage.getItem("WKturtotPrevs") || -1;
  194.  
  195. if (pt == -1) {
  196.  
  197. console.log("no record of previous totals, saving current totals for next time");
  198. pt = ct;
  199. sessionStorage.setItem("WKturtotPrevs", ct);
  200. }
  201.  
  202. var prevTotals = JSON.parse( pt );
  203. console.log('prevTotals: ', pt);
  204.  
  205. if (prevTotals.length != 10) {
  206.  
  207. console.log("old format previous totals, saving current totals for next time");
  208. pt = ct;
  209. sessionStorage.setItem("WKturtotPrevs", ct);
  210. }
  211.  
  212. if (ct !== pt) { // mark increases and decreases with up-arrows and down-arrows
  213.  
  214. markTheDiffs(curTotals, prevTotals);
  215.  
  216. // save current totals for next comparison
  217.  
  218. sessionStorage.setItem("WKturtotPrevs", ct);
  219. console.log("saved curTotals");
  220. }
  221. }
  222.  
  223.  
  224. window.addEventListener("DOMContentLoaded", main, false);
  225.  
  226. console.log('Turle Total script load end');