Wanikani: Level Duration

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

目前為 2018-04-26 提交的版本,檢視 最新版本

// ==UserScript==
// @name         Wanikani: Level Duration
// @namespace    Wanikani: Level Duration
// @version      0.2.0.
// @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 radicals 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}
						}
				};
				//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])) {break;}
						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: grey !important;}');
				$('li.dropdown.levels').append('<div class="level-duration">' + Math.round(10*days_passed)/10 + ' days on level</div>');
		}
})();