Wanikani: Level Duration

Displays the number of days you have spent on the current level.

Per 26-04-2018. Zie de nieuwste versie.

// ==UserScript==
// @name         Wanikani: Level Duration
// @namespace    Wanikani: Level Duration
// @version      0.2.2.
// @description  Displays the number of days you have spent on the current level.
// @author       Kumirei
// @include      https://www.wanikani.com*
// @grant        none
// ==/UserScript==

(function() {
		//check that the Wanikani Framework is installed
		if (!window.wkof) {
				alert('Wanikani: Level Duration requires Wanikani Open Framework.\nYou will now be forwarded to installation instructions.');
				window.location.href = 'https://community.wanikani.com/t/instructions-installing-wanikani-open-framework/28549';
				return;
		}
		else {
				//if it's installed then do the stuffs
				wkof.include('ItemData');
				wkof.ready('ItemData').then(fetch_items);
		}

		//get the current level's items through WKOF
		function fetch_items() {
				//config the request to return the current level's items
				var level = $('.dropdown.levels > a > span')[0].innerText;
				var config = {
						wk_items: {
								options: {assignments: true},
								filters: {level: level, item_type: 'rad, kan'}
						}
				};
				//request
				wkof.ItemData.get_items(config).then(process_items);
		}

		//find number of days since level up and attach info to header
		function process_items(items) {
				//find the earliest unlocked item on level
				var date = Date.now();
				for (var i = 0; i < items.length; i++) {
						if (!('assignments' in items[i])) {continue;}
						var unlock_date = Date.parse(items[i].assignments.unlocked_at);
						if (unlock_date < date) {
								date = unlock_date;
						}
				}
				//use data
				var days_passed = (Date.now() - date)/(1000*60*60*24);
				$('head').append('<style>' +
								 '    .dropdown.levels > a {padding-bottom: 0 !important;}' +
								 '    .level-duration {' +
								 '        text-align: center;' +
								 '        color: #999 !important;' +
								 '        font-family: "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;' +
								 '        text-shadow: 0 1px 0 #ffffff;' +
								 '        margin-top: -2px;' +
								 '    }' +
								 '</style>');
				$('li.dropdown.levels').append('<div class="level-duration">' + Math.round(10*days_passed)/10 + ' days on level</div>');
		}
})();