Greasy Fork

Greasy Fork is available in English.

WaniKani Lattice Extension

Extends the WaniKani lattice and allows to choose what data to display

当前为 2016-04-09 提交的版本,查看 最新版本

// ==UserScript==
// @name			WaniKani Lattice Extension
// @description		Extends the WaniKani lattice and allows to choose what data to display
// @namespace		irx.wanikani.lattice_extension
// @include			https://www.wanikani.com/
// @include			https://www.wanikani.com/dashboard
// @include			https://www.wanikani.com/review/session*
// @include			https://www.wanikani.com/lattice/radicals/meaning
// @include			https://www.wanikani.com/lattice/kanji/combined
// @include			https://www.wanikani.com/lattice/kanji/meaning
// @include			https://www.wanikani.com/lattice/kanji/reading
// @include			https://www.wanikani.com/lattice/vocabulary/combined
// @include			https://www.wanikani.com/lattice/vocabulary/meaning
// @include			https://www.wanikani.com/lattice/vocabulary/reading
// @version			1.1
// @copyright		2016, Ingo Radax
// @license			MIT; http://opensource.org/licenses/MIT
// @grant			none
// ==/UserScript==

var WaniKani = (function() {
	var local_storage_prefix = 'wk_toolkit_';
	
	var api_key = null;
	
	var radical_data = null;
	var kanji_data = null;
	var vocabulary_data = null;

	function log(msg) {
		console.log(msg);
	}
	
	function is_on_wanikani() {
		return (window.location.host == 'www.wanikani.com');
	}
	
	function is_on_dashboard() {
		return is_on_wanikani() && ((window.location.pathname == '/dashboard') || (window.location.pathname == '/'));
	}
	
	function is_on_review_session_page() {
		return is_on_wanikani() && (window.location.pathname == '/review/session');
	}
	
	function is_on_review_page() {
		return is_on_wanikani() && (window.location.pathname == '/review');
	}
	
	function is_on_lesson_session_page() {
		return is_on_wanikani() && (window.location.pathname == '/lesson/session');
	}
	
	function is_on_lesson_page() {
		return is_on_wanikani() && (window.location.pathname == '/lesson');
	}
	
	function is_on_lattice_radicals_meaning() {
		return is_on_wanikani() && (window.location.pathname == '/lattice/radicals/meaning');
	}
	
	function is_on_lattice_radicals_progress() {
		return is_on_wanikani() && (window.location.pathname == '/lattice/radicals/progress');
	}
	
	function is_on_lattice_kanji_combined() {
		return is_on_wanikani() && (window.location.pathname == '/lattice/kanji/combined');
	}
	
	function is_on_lattice_kanji_meaning() {
		return is_on_wanikani() && (window.location.pathname == '/lattice/kanji/meaning');
	}
	
	function is_on_lattice_kanji_reading() {
		return is_on_wanikani() && (window.location.pathname == '/lattice/kanji/reading');
	}
	
	function is_on_lattice_kanji_progress() {
		return is_on_wanikani() && (window.location.pathname == '/lattice/kanji/status');
	}
	
	function is_on_lattice_vocabulary_combined() {
		return is_on_wanikani() && (window.location.pathname == '/lattice/vocabulary/combined');
	}
	
	function is_on_lattice_vocabulary_meaning() {
		return is_on_wanikani() && (window.location.pathname == '/lattice/vocabulary/meaning');
	}
	
	function is_on_lattice_vocabulary_reading() {
		return is_on_wanikani() && (window.location.pathname == '/lattice/vocabulary/reading');
	}
	
	function is_on_lattice_vocabulary_progress() {
		return is_on_wanikani() && (window.location.pathname == '/lattice/vocabulary/status');
	}
	
	//-------------------------------------------------------------------
	// Try to parse the url and detect if it belongs to a single item.
	// e.g.	'https://www.wanikani.com/level/1/radicals/construction'
	//		will be parsed as 'radicals' and 'construction'
	//-------------------------------------------------------------------
	function parse_item_url(url) {
		url = decodeURI(url);
		var parsed = /.*\/(radicals|kanji|vocabulary)\/(.+)/.exec(url);
		if (parsed) {
			return {type:parsed[1], name:parsed[2]};
		}
		else {
			return null;
		}
	}
	
	function clear_local_storage() {
		localStorage.removeItem(local_storage_prefix + 'last_review_time');
		localStorage.removeItem(local_storage_prefix + 'next_review_time');
		localStorage.removeItem(local_storage_prefix + 'last_unlock_time');
		localStorage.removeItem(local_storage_prefix + 'api_key');
		localStorage.removeItem(local_storage_prefix + 'api_user_radicals');
		localStorage.removeItem(local_storage_prefix + 'api_user_radicals_fetch_time');
		localStorage.removeItem(local_storage_prefix + 'api_user_kanji');
		localStorage.removeItem(local_storage_prefix + 'api_user_kanji_fetch_time');
		localStorage.removeItem(local_storage_prefix + 'api_user_vocabulary');
		localStorage.removeItem(local_storage_prefix + 'api_user_vocabulary_fetch_time');
	}
		
	function track_times() {
		if (is_on_review_session_page()) {
			localStorage.setItem(local_storage_prefix + 'last_review_time', now());
			
			var lastUnlockTime = new Date($('.recent-unlocks time:nth(0)').attr('datetime'))/1000;
			localStorage.setItem(local_storage_prefix + 'last_unlock_time', now());
		}
		
		if (is_on_dashboard()) {
			var next_review = Number($('.review-status .timeago').attr('datetime'));
			// Workaround for "WaniKani Real Times" script, which deletes the element we were looking for above.
			if (isNaN(next_review)) {
				next_review = Number($('.review-status time1').attr('datetime'));
				// Conditional divide-by-1000, in case someone fixed this error in Real Times script.
				if (next_review > 10000000000) next_review /= 1000;
			}
			localStorage.setItem(local_storage_prefix + 'next_review_time', next_review);
		}
	}
	
	function get_last_review_time() {
		return Number(localStorage.getItem(local_storage_prefix + 'last_review_time') || 0);
	}
	
	function get_next_review_time() {
		return Number(localStorage.getItem(local_storage_prefix + 'next_review_time') || 0);
	}
	
	function get_last_unlock_time() {
		return Number(localStorage.getItem(local_storage_prefix + 'last_unlock_time') || 0);
	}

	function now() {
		return Math.floor(new Date() / 1000);
	}
	
	function ajax_retry(url, retries, timeout) {
		retries = retries || 2;
		timeout = timeout || 3000;
		function action(resolve, reject) {
			$.ajax({
				url: url,
				timeout: timeout
			})
			.done(function(data, status){
				if (status === 'success')
					resolve(data);
				else
					reject();
			})
			.fail(function(xhr, status, error){
				if (status === 'error' && --retries > 0)
					action(resolve, reject);
				else
					reject();
			});
		}
		return new Promise(action);
	}

	function get_api_key() {
		return new Promise(function(resolve, reject) {
			api_key = localStorage.getItem(local_storage_prefix + 'api_key');
			if (typeof api_key === 'string' && api_key.length == 32) {
				log("Already having API key");
				return resolve();	
			}
			
			log("Loading API key");
			
			ajax_retry('/account').then(function(page) {
				
				log("Loading API key ... SUCCESS");
				
				// --[ SUCCESS ]----------------------
				// Make sure what we got is a web page.
				if (typeof page !== 'string') {return reject();}

				// Extract the user name.
				page = $(page);
				
				// Extract the API key.
				api_key = page.find('#api-button').parent().find('input').attr('value');
				if (typeof api_key !== 'string' || api_key.length !== 32)  {return reject();}

				localStorage.setItem(local_storage_prefix + 'api_key', api_key);
				resolve();

			},function(result) {
				
				log("Loading API key ... ERROR");
				
				// --[ FAIL ]-------------------------
				reject(new Error('Failed to fetch API key!'));
				
			});
		});
	}
	
	function call_api_user_radicals() {
		return new Promise(function(resolve, reject) {
			log("Calling API: User radicals");
			$.getJSON('/api/user/' + api_key + '/radicals/', function(json){
				if (json.error && json.error.code === 'user_not_found') {
					log("Calling API: User radicals ... ERROR")
					localStorage.removeItem(local_storage_prefix + 'api_user_radicals');
					localStorage.removeItem(local_storage_prefix + 'api_user_radicals_fetch_time');
					location.reload();
					reject();
					return;
				}

				log("Calling API: User radicals ... SUCCESS");
				
				localStorage.setItem(local_storage_prefix + 'api_user_radicals', JSON.stringify(json));
				localStorage.setItem(local_storage_prefix + 'api_user_radicals_fetch_time', now());
				
				radical_data = json;
				
				resolve();
			});
		});
	}
	
	function call_api_user_kanji() {
		return new Promise(function(resolve, reject) {
			log("Calling API: User kanji");
			$.getJSON('/api/user/' + api_key + '/kanji/', function(json){
				if (json.error && json.error.code === 'user_not_found') {
					log("Calling API: User kanji ... ERROR")
					localStorage.removeItem(local_storage_prefix + 'api_user_kanji');
					localStorage.removeItem(local_storage_prefix + 'api_user_kanji_fetch_time');
					location.reload();
					reject();
					return;
				}

				log("Calling API: User kanji ... SUCCESS");
				
				localStorage.setItem(local_storage_prefix + 'api_user_kanji', JSON.stringify(json));
				localStorage.setItem(local_storage_prefix + 'api_user_kanji_fetch_time', now());
				
				kanji_data = json;
				
				resolve();
			});
		});
	}
	
	function call_api_user_vocabulary() {
		return new Promise(function(resolve, reject) {
			log("Calling API: User vocabulary");
			$.getJSON('/api/user/' + api_key + '/vocabulary/', function(json){
				if (json.error && json.error.code === 'user_not_found') {
					log("Calling API: User vocabulary ... ERROR")
					localStorage.removeItem(local_storage_prefix + 'api_user_vocabulary');
					localStorage.removeItem(local_storage_prefix + 'api_user_vocabulary_fetch_time');
					location.reload();
					reject();
					return;
				}

				log("Calling API: User vocabulary ... SUCCESS");
				
				localStorage.setItem(local_storage_prefix + 'api_user_vocabulary', JSON.stringify(json));
				localStorage.setItem(local_storage_prefix + 'api_user_vocabulary_fetch_time', now());
				
				vocabulary_data = json;
				
				resolve();
			});
		});
	}
	
	function get_last_fetch_time_api_user_radicals() {
		return Number(localStorage.getItem(local_storage_prefix + 'api_user_radicals_fetch_time'));
	}
	
	function get_last_fetch_time_api_user_kanji() {
		return Number(localStorage.getItem(local_storage_prefix + 'api_user_kanji_fetch_time'));
	}
	
	function get_last_fetch_time_api_user_vocabulary() {
		return Number(localStorage.getItem(local_storage_prefix + 'api_user_vocabulary_fetch_time'));
	}
	
	function load_radical_data() {
        var next_review_time = get_next_review_time();
		var last_review_time = get_last_review_time();
		var last_unlock_time = get_last_unlock_time();
		var last_fetch_time = get_last_fetch_time_api_user_radicals();
        if ((last_fetch_time <= last_unlock_time) ||
			(last_fetch_time <= last_review_time) ||
			((next_review_time < now()) && (last_fetch_time <= (now() - 3600)))) {
			log("Clearing previous fetched radical data");
			radical_data = null;
			localStorage.removeItem(local_storage_prefix + 'api_user_radicals');
			localStorage.removeItem(local_storage_prefix + 'api_user_radicals_fetch_time');
		}
		
		if (radical_data == null) {
			var stringified = localStorage.getItem(local_storage_prefix + 'api_user_radicals');
			if (stringified != null) {
				log("Radical data loaded from local storage");
				radical_data = JSON.parse(stringified);
			}
		}
		
		if (radical_data != null) {
			log("Radical data already loaded");
			return Promise.resolve();
		}
		
		return new Promise(function(resolve, reject) {
			get_api_key()
				.then(call_api_user_radicals)
				.then(function() {
					resolve();
				});
		});
	}
	
	function load_kanji_data() {
        var next_review_time = get_next_review_time();
		var last_review_time = get_last_review_time();
		var last_unlock_time = get_last_unlock_time();
		var last_fetch_time = get_last_fetch_time_api_user_kanji();
        if ((last_fetch_time <= last_unlock_time) ||
			(last_fetch_time <= last_review_time) ||
			((next_review_time < now()) && (last_fetch_time <= (now() - 3600)))) {
			log("Clearing previous fetched kanji data");
			kanji_data = null;
			localStorage.removeItem(local_storage_prefix + 'api_user_kanji');
			localStorage.removeItem(local_storage_prefix + 'api_user_kanji_fetch_time');
		}
		
		if (kanji_data == null) {
			var stringified = localStorage.getItem(local_storage_prefix + 'api_user_kanji');
			if (stringified != null) {
				log("Kanji data loaded from local storage");
				kanji_data = JSON.parse(stringified);
			}
		}
		
		if (kanji_data != null) {
			log("Kanji data already loaded");
			return Promise.resolve();
		}
		
		return new Promise(function(resolve, reject) {
			get_api_key()
				.then(call_api_user_kanji)
				.then(function() {
					resolve();
				});
		});
	}
	
	function load_vocabulary_data() {
        var next_review_time = get_next_review_time();
		var last_review_time = get_last_review_time();
		var last_unlock_time = get_last_unlock_time();
		var last_fetch_time = get_last_fetch_time_api_user_vocabulary();
        if ((last_fetch_time <= last_unlock_time) ||
			(last_fetch_time <= last_review_time) ||
			((next_review_time < now()) && (last_fetch_time <= (now() - 3600)))) {
			log("Clearing previous fetched vocabulary data");
			vocabulary_data = null;
			localStorage.removeItem(local_storage_prefix + 'api_user_vocabulary');
			localStorage.removeItem(local_storage_prefix + 'api_user_vocabulary_fetch_time');
		}
		
		if (vocabulary_data == null) {
			var stringified = localStorage.getItem(local_storage_prefix + 'api_user_vocabulary');
			if (stringified != null) {
				log("Vocabulary data loaded from local storage");
				vocabulary_data = JSON.parse(stringified);
			}
		}
		
		if (vocabulary_data != null) {
			log("Vocabulary data already loaded");
			return Promise.resolve();
		}
		
		return new Promise(function(resolve, reject) {
			get_api_key()
				.then(call_api_user_vocabulary)
				.then(function() {
					resolve();
				});
		});
	}
	
	function get_radical_data() {
		return radical_data;
	}
	
	function get_kanji_data() {
		return kanji_data;
	}
	
	function get_vocabulary_data() {
		return vocabulary_data;
	}
	
	function find_radical(meaning) {
		if (radical_data ==  null) {
			return null;
		}
		
		var numRadicals = radical_data.requested_information.length;
		for (var i = 0; i < numRadicals; i++) {
			if (radical_data.requested_information[i].meaning == meaning) {
				return radical_data.requested_information[i];
			}
		}
		
		return null;
	}
	
	function find_kanji(character) {
		if (kanji_data ==  null) {
			return null;
		}
		
		var numKanji = kanji_data.requested_information.length;
		for (var i = 0; i < numKanji; i++) {
			if (kanji_data.requested_information[i].character == character) {
				return kanji_data.requested_information[i];
			}
		}
		
		return null;
	}
	
	function find_vocabulary(character) {
		if (vocabulary_data ==  null) {
			return null;
		}
		
		var numVocabulary = vocabulary_data.requested_information.general.length;
		for (var i = 0; i < numVocabulary; i++) {
			if (vocabulary_data.requested_information.general[i].character == character) {
				return vocabulary_data.requested_information.general[i];
			}
		}
		
		return null;
	}
	
	function find_item(type, name) {
		if (type == 'radicals') {
			return find_radical(name);
		}
		else if(type == 'kanji') {
			return find_kanji(name);
		}
		else if(type == 'vocabulary') {
			return find_vocabulary(name);
		}
		else {
			return null;
		}
	}
	
	return {
		is_on_wanikani: is_on_wanikani,
		is_on_dashboard: is_on_dashboard,
		is_on_review_session_page: is_on_review_session_page,
		is_on_review_page: is_on_review_page,
		is_on_lesson_session_page: is_on_lesson_session_page,
		is_on_lesson_page: is_on_lesson_page,
		is_on_lattice_radicals_meaning: is_on_lattice_radicals_meaning,
		is_on_lattice_radicals_progress: is_on_lattice_radicals_progress,
		is_on_lattice_kanji_combined: is_on_lattice_kanji_combined,
		is_on_lattice_kanji_meaning: is_on_lattice_kanji_meaning,
		is_on_lattice_kanji_reading: is_on_lattice_kanji_reading,
		is_on_lattice_kanji_progress: is_on_lattice_kanji_progress,
		is_on_lattice_vocabulary_combined: is_on_lattice_vocabulary_combined,
		is_on_lattice_vocabulary_meaning: is_on_lattice_vocabulary_meaning,
		is_on_lattice_vocabulary_reading: is_on_lattice_vocabulary_reading,
		is_on_lattice_vocabulary_progress: is_on_lattice_vocabulary_progress,
		parse_item_url: parse_item_url,
		clear_local_storage: clear_local_storage,
		track_times: track_times,
		get_last_review_time: get_last_review_time,
		get_next_review_time: get_next_review_time,
		load_radical_data: load_radical_data,
		get_radical_data: get_radical_data,
		find_radical: find_radical,
		load_kanji_data: load_kanji_data,
		get_kanji_data: get_kanji_data,
		find_kanji: find_kanji,
		load_vocabulary_data: load_vocabulary_data,
		get_vocabulary_data: get_vocabulary_data,
		find_vocabulary: find_vocabulary,
		find_item: find_item,
	};
})();

(function(gobj) {
	
	var statistics = {
		combined_percent_answered_correct: {min: 10000, max:0},
		meaning_percent_answered_correct: {min: 10000, max:0},
		meaning_correct: {min: 10000, max:0},
		meaning_incorrect: {min: 10000, max:0},
		meaning_max_streak: {min: 10000, max:0},
		meaning_current_streak: {min: 10000, max:0},
		reading_percent_answered_correct: {min: 10000, max:0},
		reading_correct: {min: 10000, max:0},
		reading_incorrect: {min: 10000, max:0},
		reading_max_streak: {min: 10000, max:0},
		reading_current_streak: {min: 10000, max:0},};
	
	var classification_thresholds = {};
	
	var radicals_data_labels = {
		meaning_percent_answered_correct: "Meaning (% correct)",
		meaning_correct: "Meaning (correct)",
		meaning_incorrect: "Meaning (incorrect)",
		meaning_current_streak: "Meaning (current streak)",
		meaning_max_streak: "Meaning (max streak)",
	};
	
	var kanji_data_labels = {
		combined_percent_answered_correct: "Combined (% correct)",
		meaning_percent_answered_correct: "Meaning (% correct)",
		reading_percent_answered_correct: "Reading (% correct)",
		meaning_correct: "Meaning (correct)",
		meaning_incorrect: "Meaning (incorrect)",
		meaning_current_streak: "Meaning (current streak)",
		meaning_max_streak: "Meaning (max streak)",
		reading_correct: "Reading (correct)",
		reading_incorrect: "Reading (incorrect)",
		reading_current_streak: "Reading (current streak)",
		reading_max_streak: "Reading (max streak)",
	};
	
	var vocabulary_data_labels = {
		combined_percent_answered_correct: "Combined (% correct)",
		meaning_percent_answered_correct: "Meaning (% correct)",
		reading_percent_answered_correct: "Reading (% correct)",
		meaning_correct: "Meaning (correct)",
		meaning_incorrect: "Meaning (incorrect)",
		meaning_current_streak: "Meaning (current streak)",
		meaning_max_streak: "Meaning (max streak)",
		reading_correct: "Reading (correct)",
		reading_incorrect: "Reading (incorrect)",
		reading_current_streak: "Reading (current streak)",
		reading_max_streak: "Reading (max streak)",
	};
	
	var used_labels = null;
	
	function removeText(jq_expression) {
		$(jq_expression).contents().filter(function () {
			return this.nodeType === 3; 
		}).remove();
	}
	
	function calculate_classification_thresholds(min, max) {
		if (max - min < 5) {
			if (max >= 4) {
				classification_thresholds = {
					t0: max - 4,
					t1: max - 4,
					t2: max - 3,
					t3: max - 2,
					t4: max - 1,
					t5: max,
				};
			}
			else {
				classification_thresholds = {
					t0: 0,
					t1: 0,
					t2: 1,
					t3: 2,
					t4: 3,
					t5: 4,
				};
			}
		}
		
		var len = (max - min) / 5;
		classification_thresholds = {
			t0: min,
			t1: Math.floor(min + len),
			t2: Math.floor(min + 2 * len),
			t3: Math.floor(min + 3 * len),
			t4: Math.floor(min + 4 * len),
			t5: max,
		};
	}
	
	function classify_item(value) {
		var t = classification_thresholds;
		if (value <= t.t1)
			return 'percentage-0-20';
		else if ((value >= t.t1 + 1) && (value <= t.t2))
			return 'percentage-21-40';
		else if ((value >= t.t2 + 1) && (value <= t.t3))
			return 'percentage-41-60';
		else if ((value >= t.t3 + 1) && (value <= t.t4))
			return 'percentage-61-80';
		else
			return 'percentage-81-100';
	}
	
	function updateLegend() {
		var dataToDisplay = $('#data_to_display').val();
		var unit = dataToDisplay.includes('percent') ? '%' : '';;
		var t = classification_thresholds;
		$('aside.additional-info ul li:first div span:nth(0)').attr('data-original-title', t.t0 + unit + '-' + t.t1 + unit);
		$('aside.additional-info ul li:first div span:nth(1)').attr('data-original-title', (t.t1 + 1) + unit + '-' + t.t2 + unit);
		$('aside.additional-info ul li:first div span:nth(2)').attr('data-original-title', (t.t2 + 1) + unit + '-' + t.t3 + unit);
		$('aside.additional-info ul li:first div span:nth(3)').attr('data-original-title', (t.t3 + 1) + unit + '-' + t.t4 + unit);
		$('aside.additional-info ul li:first div span:nth(4)').attr('data-original-title', (t.t4 + 1) + unit + '-' + t.t5 + unit);
	}
	
	function updateKanji() {
		var dataToDisplay = $('#data_to_display').val();
	
		var zeroAsMinimum = $('#zero_as_minimum').is(':checked');
		if (zeroAsMinimum) {
			var max = statistics[dataToDisplay].max;
			calculate_classification_thresholds(0, max);
		}
		else {
			var min = statistics[dataToDisplay].min;
			var max = statistics[dataToDisplay].max;
			calculate_classification_thresholds(min, max);
		}
		
		updateLegend();
		
		var kanji = $('section.lattice-single-character a');
		kanji.each(function(i, item) {
			var isLocked = $(this).parent().attr('is_locked');
			if (isLocked == 'true') {
				return;
			}

			$(this).removeClass('percentage-0-20 percentage-21-40 percentage-41-60 percentage-61-80 percentage-81-100');
			
			var value = $(this).parent().attr('data_' + dataToDisplay);
			$(this).addClass(classify_item(value));
		});
	}
	
	function updateHtmlItems() {
		var dataToDisplay = $('#data_to_display').val();
	
		var zeroAsMinimum = $('#zero_as_minimum').is(':checked');
		if (zeroAsMinimum) {
			var max = statistics[dataToDisplay].max;
			calculate_classification_thresholds(0, max);
		}
		else {
			var min = statistics[dataToDisplay].min;
			var max = statistics[dataToDisplay].max;
			calculate_classification_thresholds(min, max);
		}
		
		updateLegend();
		
		var htmlItems = $('section.lattice-single-character a, section.lattice-multi-character a');
		htmlItems.each(function(i, item) {
			var isLocked = $(this).parent().attr('is_locked');
			if (isLocked != 'false') {
				return;
			}

			$(this).removeClass('percentage-0-20 percentage-21-40 percentage-41-60 percentage-61-80 percentage-81-100');
			
			var value = $(this).parent().attr('data_' + dataToDisplay);
			$(this).addClass(classify_item(value));
		});
	}
	
	function buildSelectBox() {
		var isOnLatticeCombined = WaniKani.is_on_lattice_kanji_combined() || WaniKani.is_on_lattice_vocabulary_combined();
		var isOnLatticeMeaning = WaniKani.is_on_lattice_kanji_meaning() || WaniKani.is_on_lattice_vocabulary_meaning();
		var isOnLatticeReading = WaniKani.is_on_lattice_kanji_reading() || WaniKani.is_on_lattice_vocabulary_reading();
		
		var selectBox = '<select id="data_to_display" class="input" name="data_to_display" title="Select what data to display.">';
		for(var key in used_labels){
			var selected = '';
			
			if (isOnLatticeCombined) {
				if (key == 'combined_percent_answered_correct') {
					selected = 'selected';
				}
			}
			else if (isOnLatticeMeaning) {
				if (key == 'meaning_percent_answered_correct') {
					selected = 'selected';
				}
			}
			else if (isOnLatticeReading) {
				if (key == 'reading_percent_answered_correct') {
					selected = 'selected';
				}
			}
			
			selectBox = selectBox + '<option value="' + key + '" ' + selected + '>' + used_labels[key] + '</option>';
		}
		selectBox = selectBox + '</select>'
		return selectBox;
	}
	
	function addToStatistics(attribute, value) {
		var currentMin = statistics[attribute].min;
		var currentMax = statistics[attribute].max;
		statistics[attribute].min = (currentMin < value) ? currentMin : value;
		statistics[attribute].max = (currentMax < value) ? value : currentMax;
	}
	
	function extendItem(value, item, dataAttr) {
		var newTitle = item.attr('data-original-title');
		newTitle += '<br />' + used_labels[dataAttr] + ': ' + value;
		item.attr('data-original-title', newTitle);
		
		item.parent().attr('data_' + dataAttr, value);
		
		addToStatistics(dataAttr, value);
	}
	
	function calcPercentAnsweredCorrect(correct, incorrect) {
		if (correct + incorrect > 0)
			return Math.round(100 * correct / (correct + incorrect));
		else
			return 100;
	}
	
	function extendLatticeRadicals() {
		used_labels = radicals_data_labels;
		
		removeText('aside.additional-info ul li:first');
		
		$('aside.additional-info ul li:first').append(buildSelectBox());
		$('#data_to_display').on('change', function() { updateHtmlItems(); });
		
		$('aside.additional-info ul li:first').append(
			'<br /><input id="zero_as_minimum" type="checkbox" name="zero_as_minimum" checked>Use 0 as minimum<br>');
		$('#zero_as_minimum').on('change', function() { updateHtmlItems(); });
		
		var htmlItems = $('section.lattice-single-character a');
		htmlItems.each(function(i, item) {
			var uri = WaniKani.parse_item_url($(this).attr('href'));
			var itemInfo = WaniKani.find_item(uri.type, uri.name);
			if ((itemInfo == null) || (itemInfo.user_specific == null)) {
				$(this).parent().attr('is_locked', 'true');
				return;
			}
			
			$(this).parent().attr('is_locked', 'false');
			
			{
				var correct = itemInfo.user_specific.meaning_correct;
				var incorrect = itemInfo.user_specific.meaning_incorrect;
				var percentAnsweredCorrect = calcPercentAnsweredCorrect(correct, incorrect);
				extendItem(percentAnsweredCorrect, $(this), 'meaning_percent_answered_correct');
			}
			
			extendItem(itemInfo.user_specific.meaning_correct,			$(this), 'meaning_correct');
			extendItem(itemInfo.user_specific.meaning_incorrect,		$(this), 'meaning_incorrect');
			extendItem(itemInfo.user_specific.meaning_current_streak,	$(this), 'meaning_current_streak');
			extendItem(itemInfo.user_specific.meaning_max_streak,		$(this), 'meaning_max_streak');
		});
	}
	
	function extendLatticeKanji() {
		used_labels = kanji_data_labels;
		
		removeText('aside.additional-info ul li:first');
		
		$('aside.additional-info ul li:first').append(buildSelectBox());
		$('#data_to_display').on('change', function() { updateHtmlItems(); });
		
		$('aside.additional-info ul li:first').append(
			'<br /><input id="zero_as_minimum" type="checkbox" name="zero_as_minimum" checked>Use 0 as minimum<br>');
		$('#zero_as_minimum').on('change', function() { updateHtmlItems(); });
		
		var htmlItems = $('section.lattice-single-character a');
		htmlItems.each(function(i, item) {
			var uri = WaniKani.parse_item_url($(this).attr('href'));
			var itemInfo = WaniKani.find_item(uri.type, uri.name);
			if ((itemInfo == null) || (itemInfo.user_specific == null)) {
				$(this).parent().attr('is_locked', 'true');
				return;
			}
			
			$(this).parent().attr('is_locked', 'false');
			
			{
				var correct = itemInfo.user_specific.meaning_correct + itemInfo.user_specific.reading_correct;
				var incorrect = itemInfo.user_specific.meaning_incorrect + itemInfo.user_specific.reading_incorrect;
				var percentAnsweredCorrect = calcPercentAnsweredCorrect(correct, incorrect);
				extendItem(percentAnsweredCorrect, $(this), 'combined_percent_answered_correct');
			}
			
			{
				var correct = itemInfo.user_specific.meaning_correct;
				var incorrect = itemInfo.user_specific.meaning_incorrect;
				var percentAnsweredCorrect = calcPercentAnsweredCorrect(correct, incorrect);
				extendItem(percentAnsweredCorrect, $(this), 'meaning_percent_answered_correct');
			}
			
			{
				var correct = itemInfo.user_specific.reading_correct;
				var incorrect = itemInfo.user_specific.reading_incorrect;
				var percentAnsweredCorrect = calcPercentAnsweredCorrect(correct, incorrect);
				extendItem(percentAnsweredCorrect, $(this), 'reading_percent_answered_correct');
			}
			
			extendItem(itemInfo.user_specific.meaning_correct,			$(this), 'meaning_correct');
			extendItem(itemInfo.user_specific.meaning_incorrect,		$(this), 'meaning_incorrect');
			extendItem(itemInfo.user_specific.meaning_current_streak,	$(this), 'meaning_current_streak');
			extendItem(itemInfo.user_specific.meaning_max_streak,		$(this), 'meaning_max_streak');
			extendItem(itemInfo.user_specific.reading_correct,			$(this), 'reading_correct');
			extendItem(itemInfo.user_specific.reading_incorrect,		$(this), 'reading_incorrect');
			extendItem(itemInfo.user_specific.reading_max_streak,		$(this), 'reading_max_streak');
			extendItem(itemInfo.user_specific.reading_current_streak,	$(this), 'reading_current_streak');
		});
	}
	
	function extendLatticeVocabulary() {
		used_labels = vocabulary_data_labels;
		
		removeText('aside.additional-info ul li:first');
		
		$('aside.additional-info ul li:first').append(buildSelectBox());
		$('#data_to_display').on('change', function() { updateHtmlItems(); });
		
		$('aside.additional-info ul li:first').append(
			'<br /><input id="zero_as_minimum" type="checkbox" name="zero_as_minimum" checked>Use 0 as minimum<br>');
		$('#zero_as_minimum').on('change', function() { updateHtmlItems(); });
		
		var vocabulary = $('section.lattice-multi-character a');
		vocabulary.each(function(i, item) {
			var uri = WaniKani.parse_item_url($(this).attr('href'));
			var itemInfo = WaniKani.find_item(uri.type, uri.name);
			if ((itemInfo == null) || (itemInfo.user_specific == null)) {
				$(this).parent().attr('is_locked', 'true');
				return;
			}
			
			$(this).parent().attr('is_locked', 'false');
			
			{
				var correct = itemInfo.user_specific.meaning_correct + itemInfo.user_specific.reading_correct;
				var incorrect = itemInfo.user_specific.meaning_incorrect + itemInfo.user_specific.reading_incorrect;
				var percentAnsweredCorrect = calcPercentAnsweredCorrect(correct, incorrect);
				extendItem(percentAnsweredCorrect, $(this), 'combined_percent_answered_correct');
			}
			
			{
				var correct = itemInfo.user_specific.meaning_correct;
				var incorrect = itemInfo.user_specific.meaning_incorrect;
				var percentAnsweredCorrect = calcPercentAnsweredCorrect(correct, incorrect);
				extendItem(percentAnsweredCorrect, $(this), 'meaning_percent_answered_correct');
			}
			
			{
				var correct = itemInfo.user_specific.reading_correct;
				var incorrect = itemInfo.user_specific.reading_incorrect;
				var percentAnsweredCorrect = calcPercentAnsweredCorrect(correct, incorrect);
				extendItem(percentAnsweredCorrect, $(this), 'reading_percent_answered_correct');
			}
			
			extendItem(itemInfo.user_specific.meaning_correct,			$(this), 'meaning_correct');
			extendItem(itemInfo.user_specific.meaning_incorrect,		$(this), 'meaning_incorrect');
			extendItem(itemInfo.user_specific.meaning_current_streak,	$(this), 'meaning_current_streak');
			extendItem(itemInfo.user_specific.meaning_max_streak,		$(this), 'meaning_max_streak');
			extendItem(itemInfo.user_specific.reading_correct,			$(this), 'reading_correct');
			extendItem(itemInfo.user_specific.reading_incorrect,		$(this), 'reading_incorrect');
			extendItem(itemInfo.user_specific.reading_max_streak,		$(this), 'reading_max_streak');
			extendItem(itemInfo.user_specific.reading_current_streak,	$(this), 'reading_current_streak');
		});
	}
	
	//-------------------------------------------------------------------
	// Main function
	//-------------------------------------------------------------------
	function main() {
		console.log('START - WaniKani Lattice Extension');
		
		WaniKani.track_times();
	
		if (WaniKani.is_on_lattice_radicals_meaning()) {
			WaniKani.load_radical_data().then(extendLatticeRadicals);
		}
		else if (WaniKani.is_on_lattice_kanji_combined() || WaniKani.is_on_lattice_kanji_meaning() || WaniKani.is_on_lattice_kanji_reading()) {
			WaniKani.load_kanji_data().then(extendLatticeKanji);
		}
		else if (WaniKani.is_on_lattice_vocabulary_combined() || WaniKani.is_on_lattice_vocabulary_meaning() || WaniKani.is_on_lattice_vocabulary_reading()) {
			WaniKani.load_vocabulary_data().then(extendLatticeVocabulary);
		}
		
		console.log('END - WaniKani Lattice Extension');
	}
	window.addEventListener('load', main, false);

}());