Advertisement
Guest User

Cleverflow V.2.0

a guest
Jul 26th, 2013
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name Cleverflow
  3. // @namespace http://waniiikaniii.com/
  4. // @include http://www.wanikani.com/review
  5. // @version 2.0
  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. // @require http://cdnjs.cloudflare.com/ajax/libs/qtip2/2.1.1/jquery.qtip.min.js
  11. // @resource qtipCSS http://www.michaelfrank.com.br/wanikani_improve/jquery.qtip.min.css
  12. // ==/UserScript==
  13.  
  14.  
  15.  
  16. //This is the timer that dictates how long before it switches to next item
  17. var timer = 200;
  18. //This is the timer that dictates how quickly to check for a new item to update the 'last item' button
  19. var timer_showNextItem = 350;
  20.  
  21.  
  22. var qtipCSS = GM_getResourceText("qtipCSS");
  23. GM_addStyle(qtipCSS);
  24.  
  25. var playAudio = 0;
  26. var lastItem = "";
  27. var lastType = "";
  28. var isAudioPlaying = 0;
  29. var saved_lastType = "";
  30. var saved_lastItem = "";
  31. var saved_currentItem="";
  32. var currentItem = "";
  33. var currentType = "";
  34. var previousItem = "";
  35. var previousType = "";
  36. var previousItemURL = "";
  37. var submittedAnswer = "";
  38. var previousCorrect = false;
  39.  
  40. function reviewLoaded()
  41. {
  42.  
  43.     if ($('#loading').is(':visible'))
  44.     {
  45.         console.log('Initial load in progress...');
  46.         setTimeout(reviewLoaded, 250);
  47.     }
  48.     else
  49.     {  
  50.         $('<li id="option-show-previous"><span title="Check previous item"><i class="icon-question-sign"></i></span></li>').insertAfter('#option-home').addClass('disabled');
  51.         $('<style type="text/css"> .qtip{ max-width: 380px !important; } #additional-content ul li { width: 16% !important; } #additional-content {text-align: center;} #option-show-previous img { max-width: 12px; background-color: #00A2F3; padding: 2px 3px; }</style>').appendTo('head');
  52.        
  53.         console.log('Initial load finished');
  54.     }
  55. }
  56.  
  57. function checkAnswer()
  58. {  
  59.     previousItem = $.trim($('#character span').html());
  60.     previousType = $('#character').attr('class');
  61.  
  62.     console.log('previousItem: ' + previousItem);
  63.     console.log('previousType: ' + previousType);
  64.    
  65.     if($('#answer-form form fieldset').hasClass('correct'))
  66.     {
  67.         previousCorrect = true;
  68.         console.log('Correct answer. Moving to the next item.');
  69.         $('#option-show-previous span').css('background-color', '#FBFBFB');
  70.     }
  71.     else if($('#answer-form form fieldset').hasClass('incorrect'))
  72.     {
  73.         previousCorrect = false;
  74.         console.log('Wrong answer. We will move to the next item and change the button color to red');
  75.         $('#option-show-previous span').css('background-color', '#FF8E8E');
  76.     }
  77.  
  78.        
  79.            
  80.     $('#answer-form button').click();
  81.    
  82.    
  83.     currentItem = $.trim($('#character span').html());
  84.     currentType = $('#character').attr('class');
  85.    
  86.     console.log('currentItem: ' + currentItem);
  87.     console.log('currentType: ' + currentType);      
  88.    
  89.     if(previousItem != currentItem)
  90.     {
  91.         if(previousItem == '')
  92.         {
  93.             $('#option-show-previous').addClass('disabled');
  94.             $('#option-show-previous span').attr('title', 'Previous item unavailable').html('<i class="icon-question-sign"></i> ' + previousItem);
  95.             console.log('lastItem: Previous item unavailable');
  96.         }
  97.         else
  98.         {
  99.             $('#option-show-previous').removeClass('disabled');
  100.            
  101.             if(previousCorrect === true)
  102.                 $('#option-show-previous span').attr('title', 'Check the previous ' + previousType).html('<i class="icon-question-sign"></i> ' + previousItem);
  103.             else
  104.                 $('#option-show-previous span').attr('title', 'You answered ' + submittedAnswer).html('<i class="icon-question-sign"></i> ' + previousItem);
  105.                
  106.             previousItemURL = 'http://www.wanikani.com/quickview/';
  107.            
  108.             if (previousType == 'kanji')
  109.             {
  110.                     previousItemURL += 'kanji/' + previousItem + '/';
  111.             }
  112.             else if (previousType == 'vocabulary')
  113.             {
  114.                     previousItemURL += 'vocabulary/' + previousItem + '/';
  115.             }
  116.             else
  117.             {
  118.                     previousItemURL += 'radicals/' + submittedAnswer.toLowerCase().replace(' ', '-') + '/';
  119.             }
  120.             console.log('previousItemURL: ' + previousItemURL);
  121.         }
  122.     }
  123.    
  124.     $('#option-show-previous').not('.disabled').on('click', function(event) {
  125.          $(this).qtip({
  126.             show: 'click',
  127.             hide: 'unfocus',
  128.             overwrite: true,
  129.             content: {
  130.                 title: 'Previous ' + previousType,
  131.                 text: '<iframe src="' + previousItemURL + '" frameborder="0" marginheight="0" style="width:350px; height: 250px; overflow-x: hidden; overflow-y: scroll"></iframe>'
  132.             },
  133.             position: {
  134.                 my: 'Top Center',
  135.                 at: 'Bottom Center',
  136.                 viewport: $(window)
  137.             },
  138.             show: {
  139.                 event: event.type, // Use the same event type as above
  140.                 ready: true // Show immediately - important!
  141.             },
  142.             events: {
  143.                 hide: function(event, api) {
  144.                     $(this).qtip('destroy');
  145.                 }
  146.             },
  147.             style: 'qtip-bootstrap'
  148.         });
  149.     });
  150. }
  151.  
  152. $(document).keypress(function(e) {
  153.     if(e.which == 13)
  154.     {
  155.         submittedAnswer = $('#user-response').val();
  156.         console.log('submittedAnswer: ' + submittedAnswer);
  157.            
  158.         setTimeout(checkAnswer, timer_showNextItem);      
  159.     }
  160. });
  161.  
  162. reviewLoaded();
  163. setTimeout(WaitForCorrectAnswer, timer);
  164.  
  165.  
  166. function MoveToNextQuestion()
  167. {
  168.     checkAnswer();
  169.    
  170.     isAudioPlaying = 0;
  171.     setTimeout(WaitForCorrectAnswer, timer);
  172.    
  173.  
  174. }
  175.  
  176. function PlayAudioClipThenMove()
  177. {
  178.     if ($(".play")[0] != undefined)
  179.     {
  180.         $(".play")[0].click();
  181.         setTimeout(MoveToNextQuestion, 2000);
  182.     }
  183.     else
  184.     {
  185.         setTimeout(MoveToNextQuestion,timer);
  186.     }
  187. }
  188.  
  189. function WaitForCorrectAnswer()
  190. {
  191.         if (($('fieldset').attr("class") == "correct") && (isAudioPlaying == 0))
  192.         {
  193.             if ((playAudio == 1) && ($("#option-audio").attr("class") != "disabled"))
  194.             {
  195.                 isAudioPlaying = 1;
  196.                 setTimeout(PlayAudioClipThenMove, timer);
  197.             }
  198.             else {
  199.                  setTimeout(MoveToNextQuestion,timer);
  200.             }
  201.         }
  202.         else
  203.         {
  204.             setTimeout(WaitForCorrectAnswer, timer);
  205.         }
  206. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement