Advertisement
Guest User

Cleverflow (with audio) 2.5

a guest
Dec 31st, 2013
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name Cleverflow (with audio)
  3. // @namespace http://waniiikaniii.com/
  4. // @include http://www.wanikani.com/review/session
  5. // @version 2.5
  6. // @grant GM_addStyle
  7. // @grant GM_getResourceText
  8. // @run-at document-end
  9. // @require http://code.jquery.com/jquery-1.9.1.min.js
  10. // ==/UserScript==
  11.  
  12. //This is the timer that dictates how quickly to check for a new item to update the 'last item' button
  13. var timer_showNextItem = 350;
  14.  
  15. //AUDIO SETTINGS
  16. //This toggles the audio on or off: ON = 1, OFF = 0
  17. var playAudioWhenAvailable = 1;
  18. //This is the timer that dictates how long it gives the audio to play before moving on to the next item
  19. var timer_audioPlayTime = 1500;
  20.  
  21.  
  22. var lastItem = "";
  23. var lastType = "";
  24. var saved_lastType = "";
  25. var saved_lastItem = "";
  26. var saved_currentItem="";
  27. var currentItem = "";
  28. var currentType = "";
  29. var previousItem = "";
  30. var previousType = "";
  31. var previousItemURL = "";
  32. var submittedAnswer = "";
  33. var previousCorrect = false;
  34.  
  35. function reviewLoaded()
  36. {
  37.     if ($('#loading').is(':visible'))
  38.     {
  39.         console.log('Initial load in progress...');
  40.         setTimeout(reviewLoaded, 250);
  41.     }
  42.     else
  43.     {  
  44.         $('<li id="option-show-previous"><span title="Check previous item"><i class="icon-question-sign"></i></span></li>').insertBefore('#option-last-items').addClass('disabled');
  45.         $('#additional-content ul').css('text-align','center');
  46.         $('#additional-content ul li').css('width','16%');
  47.         console.log('Initial load finished');
  48.     }
  49. }
  50.  
  51. function checkAnswer()
  52. {  
  53.     previousItem = $.trim($('#character span').html());
  54.     previousType = $('#character').attr('class');
  55.  
  56.     console.log('previousItem: ' + previousItem);
  57.     console.log('previousType: ' + previousType);
  58.    
  59.     if($('#answer-form form fieldset').hasClass('correct'))
  60.     {
  61.         previousCorrect = true;
  62.         console.log('Correct answer. Moving to the next item.');
  63.         $('#option-show-previous span').css('background-color', '#FBFBFB');
  64.     }
  65.     else if($('#answer-form form fieldset').hasClass('incorrect'))
  66.     {
  67.         previousCorrect = false;
  68.         console.log('Wrong answer. We will move to the next item and change the button color to red');
  69.         $('#option-show-previous span').css('background-color', '#FF8E8E');
  70.     }
  71.  
  72.     if (playAudioWhenAvailable == 1)
  73.     {
  74.         playAudioThenMove();
  75.     }
  76.     else
  77.     {
  78.         moveToNextItem();
  79.     }
  80. }
  81.  
  82. function playAudioThenMove()
  83. {
  84.     console.log('Attempting to play audio clip before advancing');
  85.     if ($("#option-audio").attr('class') != "disabled")
  86.     {
  87.         $("#option-audio").click();
  88.         setTimeout(moveToNextItem, timer_audioPlayTime);
  89.     }
  90.     else
  91.     {
  92.         moveToNextItem();
  93.     }
  94. }
  95.        
  96. function moveToNextItem()
  97. {            
  98.     $('#answer-form button').click();
  99.    
  100.     currentItem = $.trim($('#character span').html());
  101.     currentType = $('#character').attr('class');
  102.    
  103.     console.log('currentItem: ' + currentItem);
  104.     console.log('currentType: ' + currentType);      
  105.    
  106.     if(previousItem != currentItem)
  107.     {
  108.         $('#option-show-previous').off("click");
  109.        
  110.         if(previousItem == '')
  111.         {
  112.             $('#option-show-previous').addClass('disabled');
  113.             $('#option-show-previous span').attr('title', 'Previous item unavailable').html('<i class="icon-question-sign"></i> ' + previousItem);
  114.             console.log('lastItem: Previous item unavailable');
  115.         }
  116.         else
  117.         {
  118.             $('#option-show-previous').removeClass('disabled');
  119.            
  120.             if(previousCorrect === true)
  121.                 $('#option-show-previous span').attr('title', 'Check the previous ' + previousType).html('<i class="icon-question-sign"></i> ' + previousItem);
  122.             else
  123.                 $('#option-show-previous span').attr('title', 'You answered ' + submittedAnswer).html('<i class="icon-question-sign"></i> ' + previousItem);
  124.                
  125.             previousItemURL = 'http://www.wanikani.com/';
  126.            
  127.             if (previousType == 'kanji')
  128.             {
  129.                     previousItemURL += 'kanji/' + previousItem + '/';
  130.             }
  131.             else if (previousType == 'vocabulary')
  132.             {
  133.                     previousItemURL += 'vocabulary/' + previousItem + '/';
  134.             }
  135.             else
  136.             {
  137.                     previousItemURL += 'radicals/' + submittedAnswer.toLowerCase().replace(' ', '-') + '/';
  138.             }
  139.             console.log('previousItemURL: ' + previousItemURL);
  140.    
  141.             $('#option-show-previous').on("click", function(event){ window.open(previousItemURL); });
  142.         }
  143.     }
  144. }
  145.  
  146. $(document).keyup(function(e) {
  147.     code = (e.keyCode ? e.keyCode : e.which);
  148.     if(code == 13)
  149.     {
  150.         submittedAnswer = $('#user-response').val();
  151.         console.log('submittedAnswer: ' + submittedAnswer);
  152.            
  153.         setTimeout(checkAnswer, timer_showNextItem);      
  154.     }
  155. });
  156.  
  157. reviewLoaded();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement