srfacanha

WaniKani Real Numbers By penx Fix by srfacanha

Jul 23rd, 2015
428
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name        WaniKani Real Numbers
  3. // @namespace   penx.scripts
  4. // @description Replaces 42+ with the real number using WaniKani API v1.0
  5. // @include     *www.wanikani.com/*
  6. // @version     2.9
  7. // @grant     metadata
  8. // @grant GM_getValue
  9. // @grant GM_setValue
  10. // @run-at      document-end
  11. // @updateURL   https://userscripts.org/scripts/source/153756.meta.js
  12. // @require     http://code.jquery.com/jquery-1.8.3.min.js
  13. // ==/UserScript==
  14. function main() {
  15.   var apikey = GM_getValue('apikey');
  16.   if (!apikey) {
  17.     if (window.location.href.indexOf('account') != - 1) {
  18.       retrieveAPIkey();
  19.       apikey = GM_getValue('apikey');
  20.     } else {
  21.       var okcancel = confirm('WaniKani Real Numbers has no API key entered!\nPress OK to go to your settings page and retrieve your API key!');
  22.       if (okcancel == true) {
  23.         window.location = 'https://www.wanikani.com/account';
  24.       }
  25.     }
  26.   }
  27.   var doneReviews = GM_getValue('doneReviews', true);
  28.   var lastUpdate = GM_getValue('lastUpdate', '0');
  29.   var currentTime = new Date().getTime();
  30.   if ((currentTime - lastUpdate) > 120000) {
  31.     GM_setValue('lastUpdate', currentTime.toString());
  32.     doneReviews = true;
  33.   }
  34.   if (window.location.href.indexOf('review') != - 1 || window.location.href.indexOf('lesson') != - 1) {
  35.     GM_setValue('doneReviews', true);
  36.   } else {
  37.     var numberReviews = document.getElementsByClassName('reviews') [0].getElementsByTagName('span') [0];
  38.     var numberLessons = document.getElementsByClassName('lessons') [0].getElementsByTagName('span') [0];
  39.     if (numberReviews.innerHTML == '42+' || numberLessons.innerHTML == '42+') {
  40.       if (apikey) {
  41.         if (doneReviews) {
  42.           $.getJSON('https://www.wanikani.com/api/v1/user/' + apikey + '/study-queue', function (data) {
  43.             setTimeout(function () {
  44.               if (data.error) {
  45.                 alert('API Error: ' + data.error.message);
  46.               } else {
  47.                 GM_setValue('numberReviews', data.requested_information.reviews_available);
  48.                 GM_setValue('numberLessons', data.requested_information.lessons_available);
  49.                 GM_setValue('doneReviews', false);
  50.                 displayReal(numberReviews, numberLessons);
  51.               }
  52.             }, 0);
  53.           });
  54.         } else {
  55.           displayReal(numberReviews, numberLessons);
  56.         }
  57.       }
  58.     }
  59.   }
  60. }
  61. window.addEventListener('load', main, false);
  62. GM_registerMenuCommand('WaniKani Real Numbers: Manually enter API key', setAPIkey, null, null, 'R');
  63. GM_registerMenuCommand('WaniKani Real Numbers: Reset API key', resetAPIkey, null, null, 'e');
  64. function resetAPIkey() {
  65.   GM_deleteValue('apikey');
  66.   alert('WaniKani Real Numbers API key reset!');
  67. }
  68. function retrieveAPIkey() {
  69.   var apikey;
  70.   for(var i=0;i<document.getElementsByClassName('span6').length;i++){
  71.     if(document.getElementsByClassName('span6')[i].getAttribute('placeholder')=="Key has not been generated")
  72.       apikey = document.getElementsByClassName('span6') [i].getAttribute('value');
  73.   }
  74.   alert('WaniKani Real Numbers API key set to: ' + apikey);
  75.   if (apikey) {
  76.     GM_setValue('apikey', apikey);
  77.     GM_setValue('doneReviews', true);
  78.   }
  79. }
  80. function setAPIkey() {
  81.   var apikey = prompt('Enter API key for WaniKani Real Numbers:');
  82.   if (apikey) {
  83.     GM_setValue('apikey', apikey);
  84.     GM_setValue('doneReviews', true);
  85.   }
  86. }
  87. function displayReal(numberReviews, numberLessons) {
  88.   numberReviews.innerHTML = GM_getValue('numberReviews');
  89.   numberLessons.innerHTML = GM_getValue('numberLessons');
  90. }
RAW Paste Data