Wanikani: Level Duration 2

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

目前為 2019-05-24 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Wanikani: Level Duration 2
// @namespace    Wanikani: Level Duration
// @version      2.5.3.
// @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
		var script_name = 'Level Duration';
		if (!window.wkof) {
				if (confirm(script_name+' requires Wanikani Open Framework.\nDo you want to be forwarded to the installation instructions?'))
						window.location.href = 'https://community.wanikani.com/t/instructions-installing-wanikani-open-framework/28549';
				return;
		}
		//if it's installed then do the stuffs
		else {
				var date;

				// Include the Menu module, and wait for it to be ready.
				wkof.include('Menu,Settings');
				wkof.ready('Menu,Settings')
						.then(load_settings)
						.then(install_menu)
				wkof.include('Apiv2');
				wkof.ready('Apiv2').then(fetch_data);
		}

		//fetches and processes the level's unlock date
		function fetch_data(data) {
				wkof.Apiv2.fetch_endpoint('/level_progressions').then(function(data) {
						date = data.data[data.total_count-1].data.unlocked_at;
						var days_passed = (Date.now() - Date.parse(date))/(1000*60*60*24);

						$('head').append('<style>' +
										 '    .global-header {padding-bottom: 3px;}'+
										 '    .level-duration {' +
										 '        color: #999 !important;' +
										 '        text-shadow: 0 1px 0 #ffffff;' +
										 '        margin-top: -7px;' +
										 '        font-size: 14px;' +
										 '    }' +
										 '    .level-duration span {'+
										 '        display: inline-block;'+
										 '        margin: 0 50%;'+
										 '        transform: translateX(-50%);'+
										 '        white-space: nowrap;'+
										 '    }'+
										 '    .dropdown.levels > a, .nav li.reviews a {padding-bottom: 0 !important;}' +
										 '    .level-duration-old {' +
										 '        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>');
						update_display();
				});
		}

		// This function is called when the Menu module is ready to use.
		function install_menu() {
				var config = {
						name: 'level_duration_settings',
						submenu: 'Settings',
						title: 'Level Duration',
						on_click: open_settings
				};
				wkof.Menu.insert_script_link(config);
		}

		function load_settings() {
				var defaults = {
						displayHours: false
				};
				wkof.Settings.load('level_duration', defaults);
		}

		function open_settings(items) {
				var config = {
						script_id: 'level_duration',
						title: 'Level Duration',
						on_save: update_display,
						content: {
								displayHours: {
										type: 'checkbox',
										label: 'Display Hours',
										default: false,
										hover_tip: 'Include hours in the level-duration display.',
								},
						}
				}
				var dialog = new wkof.Settings(config);
				dialog.open();
		}

		function update_display() {
				var displayHours = wkof.settings.level_duration.displayHours;
				var days_passed = (Date.now() - Date.parse(date))/(1000*60*60*24);
				var level_text = " days on level";
				if (displayHours && days_passed < 100) {
						level_text = " days " + Math.floor((days_passed-Math.floor(days_passed))*24) + " hours";
						days_passed = Math.floor(days_passed);
				}
				else {
						if (days_passed > 10) days_passed = Math.round(days_passed);
						days_passed = Math.round(days_passed*10)/10;
						if (days_passed == 1 && level_text == " days on level") level_text = " day on level";
				}
				if (!$('.level-duration').length) {
						var elem = document.createElement('div');
						elem.className = "level-duration";
						if ($('.sitemap__section.sitemap--divider > h2')[0]) var target = ".sitemap .sitemap__section:first-child"; // New header
						else {// Old header
								target = ".dropdown.levels";
								elem.className = "level-duration-old";
						}
						elem.innerHTML = '<span>'+ days_passed + level_text +'</span>';
						$(target)[0].append(elem);
				}
				else {
						$('.level-duration')[0].innerText = days_passed + level_text;
				}
		}
})();