WaniKani Real Numbers

Replaces 42+ with the real number using WaniKani API

目前為 2018-04-14 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name        WaniKani Real Numbers
// @namespace   Mempo.scripts
// @author      Mempo
// @description Replaces 42+ with the real number using WaniKani API
// @include     http://www.wanikani.com/*
// @include     https://www.wanikani.com/*
// @version     5.3
// @grant       none
// @run-at    document-end
// ==/UserScript==

function main() {
  console.log('START OF WRN');
  var apiKey = localStorage.getItem('apiKey');
  if (!apiKey) {
    if (window.location.href.indexOf('account') != - 1) {
      retrieveAPIkey();
      apiKey = localStorage.getItem('apiKey');
    } else {
      var okcancel = confirm('WaniKani Real Numbers has no API key entered!\nPress OK to go to your settings page and retrieve your API key!');
      if (okcancel == true) {
        window.location = 'https://www.wanikani.com/settings/account';
      }
    }
  }

  var doneReviews = Boolean(localStorage.getItem('WRN_doneReviews') === null || localStorage.getItem('WRN_doneReviews'));
  var lastUpdate = Number(localStorage.getItem('WRN_lastUpdate') || 0);
  var currentTime = new Date().getTime();
  if ((currentTime - lastUpdate) > 120000) {
    localStorage.setItem('WRN_lastUpdate', currentTime.toString());
    doneReviews = true;
  }
  if (window.location.href.indexOf('review') != - 1 || window.location.href.indexOf('lesson') != - 1) {
    localStorage.setItem('WRN_doneReviews', "true");
  } else {
    var numberReviews = document.getElementsByClassName('reviews') [0].getElementsByTagName('span') [0];
    var numberLessons = document.getElementsByClassName('lessons') [0].getElementsByTagName('span') [0];
    if (numberReviews.innerHTML == '42+' || numberLessons.innerHTML == '42+') {
      if (apiKey) {
        if (doneReviews) {
          $.getJSON('https://www.wanikani.com/api/user/' + apiKey + '/study-queue', function (data) {
            setTimeout(function () {
              if (data.error) {
                alert('API Error: ' + data.error.message);
              } else {
                localStorage.setItem('WRN_numberReviews', data.requested_information.reviews_available);
                localStorage.setItem('WRN_numberLessons', data.requested_information.lessons_available);
                localStorage.setItem('WRN_doneReviews', "");
                displayReal(numberReviews, numberLessons);
              }
            }, 0);
          });
        } else {
          displayReal(numberReviews, numberLessons);
        }
      }
    }
  }

}
window.addEventListener('load', main, false);

function retrieveAPIkey() {
  apiKey = document.getElementById('user_api_key').value;

  if (apiKey) {
    localStorage.setItem('apiKey', apiKey);
    localStorage.setItem('WRN_doneReviews', 'true');
    var okcancel = confirm('WaniKani Real Numbers API key set to: ' + apiKey + '\nPress OK to go back to the dashboard!');
      if (okcancel == true) {
        window.location = 'https://www.wanikani.com/dashboard';
      }
  }else{
    alert('Something went wrong! Are you sure you have already generated an API key? If you have, try generating a new one. Hopefully that will solve the problem!');
  }
}

function displayReal(numberReviews, numberLessons) {
  numberReviews.innerHTML = localStorage.getItem('WRN_numberReviews');
  numberLessons.innerHTML = localStorage.getItem('WRN_numberLessons');
}