X-Git-Url: https://git.wh0rd.org/?p=chrome-ext%2Fweb-power-switch.git;a=blobdiff_plain;f=options.js;h=e1c9702e1ac7b932672cf5b2a5dccf1a7ce1f0ed;hp=b017cd8087c240d7aaf1d1f4e7f0d21f66fca40e;hb=HEAD;hpb=3a003566115fa1f778c77f3e3812494ad5ba6688 diff --git a/options.js b/options.js index b017cd8..4df24aa 100644 --- a/options.js +++ b/options.js @@ -1,4 +1,4 @@ -// Written by Mike Frysinger . Released into the public domain. Suck it. +// Written by Mike Frysinger . Released into the public domain. function update_settings() { var url = document.getElementById('url').value + '/*'; @@ -37,18 +37,60 @@ function update_settings() { }); msg.timeout = setTimeout(function() { - msg.innerText = ''; + // Can't leave this blank or Chrome will resize the options page. + msg.innerHTML = ' '; }, 5000); } +function keydown(e) { + if (e.key == 'Enter') { + update_settings(); + } +} + +function toggle_visible_pass() { + const ele = document.getElementById('pass'); + ele.type = (ele.type == 'password') ? 'text' : 'password'; + // Disable form submission. + return false; +} + +function theme_select(theme, init) { + const theme_system = $('#theme-system'); + const theme_light = $('#theme-light'); + const theme_dark = $('#theme-dark'); + + theme_system.className = theme == 'system' ? 'selected' : ''; + theme_light.className = theme == 'light' ? 'selected' : ''; + theme_dark.className = theme == 'dark' ? 'selected' : ''; + + if (init) { + theme_system.onclick = theme_click; + theme_light.onclick = theme_click; + theme_dark.onclick = theme_click; + } +} + +function theme_click() { + const theme = this.textContent.toLowerCase(); + theme_select(theme); + storage.set({theme}); +} + window.onload = function() { - storage.get(settings_keys, function(settings) { + storage.get(settings_keys, function(settings_storage) { + const settings = Object.assign({}, settings_defaults, settings_storage); + + theme_select(settings['theme'], true); + var field = document.getElementById('save'); field.onclick = update_settings; settings_keys.forEach(function(key) { var field = document.getElementById(key); - field.value = settings[key] || settings_defaults[key]; + field.value = settings[key]; + field.onkeydown = keydown; }); }); + document.getElementById('show-pass').onclick = toggle_visible_pass; };