WaniKani Context Sentences in Reviews

Shows the context sentence in the question during vocab reviews.

As of 2017-02-10. See the latest version.

// ==UserScript==
// @name        WaniKani Context Sentences in Reviews
// @namespace   nelemnaru
// @description Shows the context sentence in the question during vocab reviews.
// @include     *://www.wanikani.com/review/session*
// @version     1.8
// @license     Please improve and repost!
// @grant       none
// ==/UserScript==

//Here you can change the scaling factor of the brightly-colored (purple, pink, blue) question area. Set to 1 for 100%, to 0.75 for 75%, etc.
var rescale = 1;// Default is 1

//Here you can set whether the vocab word is highlighted in the sentence. Set to 0 for no, or to 1 for yes
var highlightword = 0;// Default is 0

//Here you can set whether the context sentence is always shown, or only appears after a correct answer. Set to 0 to always show, or to 1 to show after correct answer
var showaftercorrect = 0;// Default is 0



$("#character").wrap("<div style='font-size:" + rescale + "em'></div>");
$("#question-type").prepend("<div id='sentwrap' style='font-size:1.5em'><div id='sentj'></div><div id='sente'></div></div>");
$("#question-type").click(function() {
  if ($.jStorage.get("questionType") === "meaning") {
    $("#sente").toggle();
  } 
});

var japanese, english, jvoc, previous_item;

//Show sentence after correct answer
if (showaftercorrect == 1) {
    $("#answer-form form button").on("click",function() {
        setTimeout(function(){
            if ($("#answer-form form fieldset").hasClass("correct")){$("#sentwrap").show();}
        }, 100); 
    });
}


$.jStorage.listenKeyChange('currentItem', function() {

  $("#sente").hide();
  if (showaftercorrect == 1) {$("#sentwrap").hide();}
    
  var current_item = $.jStorage.get('currentItem');
    
  if (previous_item !== current_item.voc) {
      $("#sentj").html(" ");//blank sentence area while fetching json
  } else if (highlightword == 1) {//immediately change highlight color when sentence stays same (smooth transition in Single Mode)
      if ($.jStorage.get('questionType') == "reading") {$(".highlighted").css({"color":"#2e2e2e","background-color":"#fff"});}
      if ($.jStorage.get('questionType') == "meaning") {$(".highlighted").css({"color":"#e9e9e9","background-color":"#555"});}
  }
  
  previous_item = current_item.voc;
    
/*Following is code courtesy of rfindley to fetch sentences*/
  // Only vocab has context sentences.
  if (current_item.voc !== undefined) {
      var url = '/json/vocabulary/' + current_item.id;

      // Grab the item info from WK server.  Process result when it arrives.
      $.getJSON(url, function(json) {
          // Extract the sentences from the item info.
          var context_sentences = json.sentences;
          //console.log('Sentences for ' + current_item.voc + ':');

          // Output each sentence to the console.
          $.each(context_sentences, function(idx, sentence) {
              japanese = sentence[0];
              english = sentence[1];
              //console.log('  J: ' + japanese);
              //console.log('  E: ' + english);
          });
/*End of code by rfindley*/
       
      $("#sentj").html(japanese);
      $("#sente").html(english);
          
      if (highlightword == 1) {
          jvoc = current_item.voc;
          while (japanese.indexOf(jvoc) === -1) {
              if (jvoc.length == 1) {console.log("Search failed");break}
              jvoc = jvoc.slice(0,jvoc.length - 1);
          }
          $("#sentj").html(japanese.replace(new RegExp(jvoc,'g'),"<span class='highlighted' style=text-shadow:none>"+jvoc+"</span>"));
          if ($.jStorage.get('questionType') == "reading") {$(".highlighted").css({"color":"#2e2e2e","background-color":"#fff"});}
          if ($.jStorage.get('questionType') == "meaning") {$(".highlighted").css({"color":"#e9e9e9","background-color":"#555"});}
      }

      });
  } else {
      $("#sentj").html("");
      $("#sente").html("");
  }

});