Wanikani Procrastination Annihilation

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

当前为 2015-12-16 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 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);