Advertisement
Guest User

Untitled

a guest
Apr 27th, 2019
28
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name        WaniKani SRS Reorder Button
  3. // @namespace   towe.wk.srsreorder
  4. // @author      Towe
  5. // @description Adds button enabling item ordering by SRS level.
  6. // @include     *://www.wanikani.com/review/session
  7. // @version     1.1.2
  8. // @grant       none
  9. // ==/UserScript==
  10.  
  11. /* Settings */
  12.  
  13. const questionTypeOrder = 1; // 1 - reading first, 2 - meaning first, 3 - random
  14. const itemTypeOrder = 1;     // 1 - rad->kan->voc, 2 - voc->kan->rad, 3 - random
  15. const ascendingSRS = true;   // low-level items first
  16. const priotitizeSRS = true;  // SRS order more important than item type order
  17. const force1x1 = true;       // meaning and reading directly next to each other
  18. const startupSort = true;    //sorts on startup
  19. const keybind = 'Digit3'
  20.  
  21. /* Utilities */
  22.  
  23. const $ = window.$;
  24. let itemsByLevels;
  25.  
  26. function getTypePriority(item) {
  27.   if (item.rad) {
  28.     return 1;
  29.   } else if (item.kan) {
  30.     return 2;
  31.   } else {
  32.     return 3;
  33.   }
  34. }
  35.  
  36. function itemComparator(itemA, itemB) {
  37.   const srsOrder = ascendingSRS ? itemA.srs - itemB.srs : itemB.srs - itemA.srs;
  38.   const typeOrder = itemTypeOrder === 3 ? 0 : (getTypePriority(itemA) - getTypePriority(itemB)) * (3 - itemTypeOrder * 2);
  39.   return priotitizeSRS ? srsOrder || typeOrder : typeOrder || srsOrder;
  40. }
  41.  
  42. function showCounters(items) {
  43.   itemsByLevels = [0, 0, 0, 0, 0, 0, 0, 0];
  44.   for (let i = 0; i < items.length; ++i) {
  45.     ++itemsByLevels[items[i].srs - 1];
  46.   }
  47.   const $srsCounters = $('<div id="srsCounters" style="background-color:rgba(255,255,255,0.9);border-radius:8px;color:black;font-weight:bold;margin-top:5px;text-shadow:none"></div>');
  48.   for (let level = 1; level <= itemsByLevels.length; ++level) {
  49.     const color = level < 5 ? 'DD0093' : level < 7 ? '882D9E' : level < 8 ? '294DDB' : '0093DD';
  50.     if (level > 1) {
  51.       $srsCounters.append(', ');
  52.     }
  53.     $srsCounters.append($('<span id="level' + level + '" style="color:#' + color + ';margin:0">' + itemsByLevels[level - 1] + '</span>'));
  54.   }
  55.   $('#srsCounters').remove();
  56.   $('div#stats').append($srsCounters);
  57. }
  58.  
  59. /* Event handlers */
  60.  
  61. const usedUIDs = [];
  62. let previousLevel = 0;
  63.  
  64. function reorderQuestionTypes() {
  65.   const item = $.jStorage.get('currentItem');
  66.   const newUID = (item.rad ? 'r' : item.kan ? 'k' : 'v') + item.id;
  67.   if (usedUIDs.includes(newUID)) {
  68.     return;
  69.   }
  70.   usedUIDs.push(newUID);
  71.   if (previousLevel > 0) {
  72.     $('#level' + previousLevel).text(--itemsByLevels[previousLevel - 1]);
  73.   }
  74.   previousLevel = item.srs;
  75.   const requestedType = ['reading', 'meaning'][item.rad ? 1 : questionTypeOrder - 1];
  76.   if ($.jStorage.get("questionType") !== requestedType) {
  77.     $.jStorage.set('questionType', requestedType);
  78.     $.jStorage.set('currentItem', item);
  79.   }
  80. }
  81.  
  82. function reorderBySrs() {
  83.   const items = $.jStorage.get('activeQueue').concat($.jStorage.get('reviewQueue'));
  84.   items.sort(itemComparator);
  85.   showCounters(items);
  86.   $.jStorage.set('activeQueue', items.slice(0, 10));
  87.   $.jStorage.set('reviewQueue', items.slice(10).reverse());
  88.   if (questionTypeOrder !== 3) {
  89.     $.jStorage.listenKeyChange('currentItem', reorderQuestionTypes);
  90.   }
  91.   $.jStorage.set('currentItem', items[0]);
  92.   if (force1x1) {
  93.     try {
  94.         unsafeWindow.Math.random = function() { return 0; };
  95.     } catch (e) {
  96.         Math.random = function() { return 0; };
  97.     }
  98.   }
  99. }
  100.  
  101. function updateCounter() {
  102.   const items = $.jStorage.get('activeQueue').concat($.jStorage.get('reviewQueue'));
  103.   showCounters(items);
  104. }
  105.  
  106. /* Initialization */
  107.  
  108. $(function() {
  109.   const $button = $('<div style="background-color: #A000f0; color: #FFFFFF; cursor: pointer; display: inline-block; font-size: 0.8125em; padding: 10px; vertical-align: bottom;">Sort by SRS</div>');
  110.   $('footer').prepend($button.click(reorderBySrs));
  111.   document.addEventListener('keydown', function(e) {
  112.     if (e.code === keybind) {
  113.       reorderBySrs();
  114.       e.preventDefault();
  115.     }
  116.   });
  117.   if (startupSort) {
  118.       $('div[id*="loading"]:visible').on('hide', reorderBySrs);
  119.   }
  120.   $.jStorage.listenKeyChange('currentItem', updateCounter);
  121. });
Advertisement
RAW Paste Data Copied
Advertisement