WaniKani Settings

try to take over the world!

Questo script non dovrebbe essere installato direttamente. È una libreria per altri script da includere con la chiave // @require https://update.greatest.deepsurf.us/scripts/22751/230720/WaniKani%20Settings.js

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

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

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

function noSpaces(value){
    return value.replace(/ /g,'');
}

function makeSettings(Name, data){
    if($('.dropdown.account .dropdown-menu li.nav-header:contains("Scripts")').length === 0){
        $('.dropdown.account .dropdown-menu').append('<li id="scriptsMenu" class="nav-header">Scripts</li>');
    }
    $('#scriptsMenu').after("<li><a id='div" + noSpaces(Name) + "Link' href='#' onclick='$(\"#div" + noSpaces(Name) + "Settings\").dialog(\"open\");return false;'>" + Name + "</a></li>");
    $('head').append('<link rel="stylesheet" href="https://code.jquery.com/ui/1.12.0/themes/base/jquery-ui.css" type="text/css" />');
	if (typeof jQuery.ui == 'undefined') {
		jQuery.getScript("https://code.jquery.com/ui/1.12.0/jquery-ui.js", function(data2, status, jqxhr) {
		makeSettings2(Name, data);
	});
	} else {
		makeSettings2(Name, data);
	}
}

function makeSettings2(Name, data){
	var divSettings = "<div id='div" + noSpaces(Name) + "Settings'><table>";
	$.each(data,function(item,value){
		divSettings = divSettings + '<tr><td><span>' + value.Display + '</span></td>';
		switch(value.Type) {
			case "textbox":
				divSettings = divSettings + '<td><input type="textbox" id="txt' + value.Name + '"></input></td></tr>';
				break;
			case "checkbox":
				divSettings = divSettings + '<td><input type="checkbox" id="chk' + value.Name + '"></input></td></tr>';
				break;
			case "select":
				divSettings = divSettings + '<td><select id="ddl' + value.Name + '">';
				$.each(value.Options,function(item2,value2){
					divSettings = divSettings + '<option value="' + value2.Value + '">' + value2.Text + '</option>';
				});
				divSettings = divSettings + '</td></tr>';
				break;
			default:
				break;
		}
	});
    divSettings = divSettings + '</table>';
    $('section.progression').after(divSettings);
	$.each(data,function(item,value){
			switch(value.Type) {
                case "textbox":
                    $('#txt' + value.Name).val(getSetting(value.Name) === null ? value.Default === null ? "" : value.Default : getSetting(value.Name));
                    break;
                case "checkbox":
                    $('#chk' + value.Name).val();
                    if((getSetting(value.Name) === null ? value.Default === null ? "" : value.Default : getSetting(value.Name)) === "1"){
                        $('#chk' + value.Name).prop('checked','checked');
                    }
                    break;
                case "select":
                    $('#ddl' + value.Name).val(getSetting(value.Name) === null ? value.Default === null ? "" : value.Default : getSetting(value.Name));
                    break;
                default:
                    return;
            }
	});
    $('#div' + noSpaces(Name) + "Settings").dialog({
				autoOpen: false,
				height: 350,
				width: 'auto',
				modal: true,
				buttons: {
					"Save": function () {
                        $.each(data,function(item,value){
                            switch(value.Type) {
                                case "textbox":
                                    localStorage.setItem(value.Name,$('#txt' + value.Name).val());
                                    break;
                                case "checkbox":
                                    if($('#chk' + value.Name).prop('checked') == true){
                                        localStorage.setItem(value.Name,"1");
                                    } else {
                                        localStorage.setItem(value.Name,"0");
                                    }
                                    break;
                                case "select":
                                    localStorage.setItem(value.Name,$('#ddl' + value.Name).val());
                                    break;
                                default:
                                    return;
                            }
                        });
						$(this).dialog("close");
						doReload();
					},
					Cancel: function () {
						$(this).dialog("close");
					}
				}
			});
}

function getSetting(setting){
	return localStorage.getItem(setting);
}

function doReload(){
    $('<div class="yesNoDialog"></div>').appendTo('body')
    .html('<div><h6>WaniKani needs to reload for changes to take effect.\nReload now?</h6></div>')
    .dialog({
        modal: true,
        title: "Refresh?",
        zIndex: 10000,
        autoOpen: true,
        width: 'auto',
        resizable: false,
        buttons: {
            Yes: function () {
                location.reload();
            },
            No: function () {
                $(this).remove();
            }
        },
        close: function (event, ui) {
            $(this).remove();
        }
    });
}

//-------------------------------------------------------------------
// Add a <style> section to the document.
//-------------------------------------------------------------------
function addStyle(aCss) {
    var head, style;
    head = document.getElementsByTagName('head')[0];
    if (head) {
        style = document.createElement('style');
        style.setAttribute('type', 'text/css');
        style.textContent = aCss;
        head.appendChild(style);
        return style;
    }
    return null;
}