Wanikani Procrastination Annihilation

Hides the forums until you've completed some reviews/lessons

目前為 2015-12-16 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name        Wanikani Procrastination Annihilation
// @namespace   mempo
// @description Hides the forums until you've completed some reviews/lessons
// @include     https://www.wanikani.com/*
// @version     1.0
// @grant       GM_getValue
// @grant       GM_setValue
// @require     http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js
// ==/UserScript==

function main() {
  console.log('started Wanikani Procrastination Annihilation BETA');
  
  var apiKey = GM_getValue('apiKey'),
   lastUnlock = GM_getValue('lastUnlock'), //INT: last unlock started
   lastReviewAmount = GM_getValue('lastReviewAmount'), //INT: last known amount of reviews
   unlockPercentage = 0.5, //INT: percentage of reviews that needs to be done before forums are unlocked
   onLockDown = GM_getValue('onLockDown'), //BOOL: are we on lockdown?
   resetTime = 1800000, //Half hour: 30min * 60sec * 1000ms
   isInitialized = GM_getValue('isInitialized'),
   currentTime = new Date().getTime();
  
  if (!isInitialized) {
      if (window.location.href.indexOf('account') != - 1) {
          apiKey = retrieveAPIkey();
      } else {
          var okcancel = confirm('Wanikani Procrastination Annihilation 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/account';
              return;
          }
      }  
    //REMAINING GM_VALUES INIT
    GM_setValue('lastUnlock', 0); 
    GM_setValue('lastReviewAmount', 0);
    GM_setValue('onLockDown', false);
    GM_setValue('isInitialized', true);
    
    //GO BACK TO DASHBOARD
    window.location = 'https://www.wanikani.com/dashboard';
    return;
  }
  
  console.log('apiKey is ' + apiKey);
  console.log('lastUnlock is ' + lastUnlock);
  console.log('lastReviewAmount is ' + lastReviewAmount);
  console.log('onLockDown is ' + onLockDown);
  console.log('isInitialized is ' + isInitialized);

  
/*********************************************************************************
*                                                                                *
*                                    checkPageRedirection                        *
*                                                                                *
**********************************************************************************/
  
  if (window.location.href.indexOf('chat') != -1 || window.location.href.indexOf('community') != -1) {
      if(onLockDown === true){
          alert("I'm very sorry, but the forums are on lockdown. Complete some reviews to unlock them!");
          window.location = 'https://www.wanikani.com/review';
      }else {
          console.log("well go on, the forums are unlocked!");
      }
  }
      
    if (window.location.href.indexOf('dashboard') != -1) {
      displaySettings(); 
      console.log('already on dashboard, mate');
    }

  
/*********************************************************************************
*                                                                                *
*                                    checkLockDown                               *
*                                                                                *
**********************************************************************************/
  console.log('inside checklockdown function');
  console.log('resettime is ' + resetTime);
  console.log('difference in time is '+ (currentTime - lastUnlock) );
  
  var currentReviewAmount = 0;
  
  $.getJSON('https://www.wanikani.com/api/user/'+ apiKey +'/study-queue', function(data){
      setTimeout(function() {
      if(data.error){
          alert("API Error: "+data.error.message);
      }else{
          currentReviewAmount = data.requested_information.reviews_available;
          console.log("current amount of reviews is " + currentReviewAmount);
          console.log("last amount of reviews is " + lastReviewAmount);
          console.log("time to reset is " + ((lastUnlock + resetTime) < currentTime));
                
                
               if(!onLockDown){ //Unlocked
                   if((lastUnlock + resetTime) < currentTime){
                        if(currentReviewAmount>0){
                            lockForums(currentReviewAmount);
                        }else{
                            showForums();
                        }
                    }else {
                        showForums();
                        if(currentReviewAmount<lastReviewAmount){
                            GM_setValue('lastReviewAmount', currentReviewAmount);
                        }
                    }
                 
                } else { //Locked
                  if(currentReviewAmount <= (lastReviewAmount * (1-unlockPercentage))) { //Locked && Reviews DONE
                    unlockForums(currentTime,lastReviewAmount);
                  }else{ //Locked && Reviews TODO
                    hideForums();
                  }


                }

              } 
	    }, 0);
	});
}

/////////////////////////

function retrieveAPIkey() {
  var apiKey;
  for(var i=0;i<document.getElementsByClassName('span6').length;i++){
    if(document.getElementsByClassName('span6')[i].getAttribute('placeholder')=="Key has not been generated") {
        apiKey = document.getElementsByClassName('span6') [i].getAttribute('value');

    }
  }
  alert('API key was set to: ' + apiKey);
  if (apiKey) {
    GM_setValue('apiKey', apiKey);
    return apiKey;
  }
}

function displaySettings() {
    
$('section.forum-topics-list').after(
                '<section id="proc">'+
                '  <style id="proc_style"></style>'+
                '  <p class="showOnLockDown">Sorry, the forums are on lockdown. Complete some reviews before you can socialize again.</p> ' +
//                '  <a id="proc_settings-lnk" class="link">[settings]</a>'+
                '</section>'
);
}

function lockForums(currentReviewAmount){
  GM_setValue('lastReviewAmount',currentReviewAmount);
  GM_setValue('onLockDown', true);
  console.log('locked forums');
  console.log('currentReviews are ' + currentReviewAmount);
  hideForums();
}

function unlockForums(currentTime,lastReviewAmount){
  GM_setValue('onLockDown', false);
  GM_setValue('lastUnlock',currentTime);
  GM_setValue('lastReviewAmount',lastReviewAmount);
  console.log('unlocked forums');
  console.log('last review amount is ' + lastReviewAmount);
  console.log('last unlock is ' + currentTime);

  
  showForums();
}

function hideForums(){
  document.getElementsByClassName("forum-topics-list")[0].style.display = "none";
  document.getElementsByClassName("showOnLockDown")[0].style.display = "block";
}

function showForums(){
  document.getElementsByClassName("forum-topics-list")[0].style.display = "block";
  document.getElementsByClassName("showOnLockDown")[0].style.display = "none";

}

/////////////////////////

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