]> git.wh0rd.org - chrome-ext/web-power-switch.git/commitdiff
add dark/light themes
authorMike Frysinger <vapier@gentoo.org>
Wed, 1 Apr 2020 03:26:57 +0000 (23:26 -0400)
committerMike Frysinger <vapier@gentoo.org>
Wed, 1 Apr 2020 03:26:57 +0000 (23:26 -0400)
common.js
css/dark.css [new file with mode: 0644]
css/light.css [new file with mode: 0644]
options.html
options.js
popup.html
popup.js
theme-init.js [new file with mode: 0644]

index 0912905a8ea3554e1a08ddc23209736a460518c7..9abdd95fdfd65b5e652cdbe4c341d25e47ac4c4a 100644 (file)
--- a/common.js
+++ b/common.js
@@ -1,15 +1,19 @@
 // Written by Mike Frysinger <vapier@gmail.com>.  Released into the public domain.
 
+function $(s) { return document.querySelector(s); }
+
 var storage = chrome.storage.sync;
 
 var settings_keys = [
        'url',
        'user',
        'pass',
+       'theme',
 ];
 
 var settings_defaults = {
        'url': 'http://192.168.0.100',
        'user': 'admin',
        'pass': '1234',
+       'theme': 'system',
 };
diff --git a/css/dark.css b/css/dark.css
new file mode 100644 (file)
index 0000000..0650976
--- /dev/null
@@ -0,0 +1,11 @@
+@charset "utf-8";
+
+/* Written by Mike Frysinger <vapier@gmail.com>.  Released into the public domain. */
+
+:root {
+       /* NB: No space after colon. */
+       --theme:dark;
+       --body-color: black;
+       --body-filter: invert(1);
+       --theme-text: '☼';
+}
diff --git a/css/light.css b/css/light.css
new file mode 100644 (file)
index 0000000..9399b81
--- /dev/null
@@ -0,0 +1,11 @@
+@charset "utf-8";
+
+/* Written by Mike Frysinger <vapier@gmail.com>.  Released into the public domain. */
+
+:root {
+       /* NB: No space after colon. */
+       --theme:light;
+       --body-color: white;
+       --body-filter:;
+       --theme-text: '☀';
+}
index 1ea430acf656b5ed1f17f65976aa15cbb2414e40..e672b76726a230d0171bb1bd7768b4f29e34319c 100644 (file)
@@ -7,7 +7,10 @@
 <title>Web Power Switch Manager Options</title>
 <script src='common.js'></script>
 <script src='options.js'></script>
-<style>body { padding: 0px; }</style>
+<style>
+body { padding: 0px; }
+button.selected { filter: invert(1); }
+</style>
 </head>
 
 <body>
  <td><input type='password' id='pass' size=30
             autocapitalize='none' autocorrect='off' spellcheck='false'>&nbsp;<button id='show-pass'>👁</button></td>
 </tr>
+<tr>
+ <td>Theme:</td>
+ <td>
+  <input type='text' id='theme' hidden>
+  <button id='theme-light'>Light</button>
+  <button id='theme-system'>System</button>
+  <button id='theme-dark'>Dark</button>
+ </td>
+</tr>
 </table>
 
 <p style='text-align: center'>
index 540d7cc128fc9121aac2d6ecf43d339a0d083b14..4df24aab976c29ef1d0c0dbbc8510cada2ea3528 100644 (file)
@@ -55,14 +55,40 @@ function toggle_visible_pass() {
        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;
                });
        });
index 7ea22fe9daaf8d34b2d72ed3d567d64b08835d11..b8abe51010e0121290b8d49fdfd1afbb171e3865 100644 (file)
@@ -5,15 +5,32 @@
 <head>
 <meta charset='utf-8'/>
 <title>Web Power Switch Manager</title>
+
+<link rel='stylesheet' href='css/dark.css' media='(prefers-color-scheme: dark)'/>
+<link rel='stylesheet' href='css/light.css' media='(prefers-color-scheme: light), (prefers-color-scheme: no-preference)'/>
+<!-- Load theme override asap to help with initial loading/flashing. -->
+<link rel='stylesheet' href='' id='theme-override'/>
+<script src='theme-init.js'></script>
+
 <script src='common.js'></script>
 <script src='popup.js'></script>
 <style>
+button[name="theme"]::before {
+       content: var(--theme-text);
+       font-size: smaller;
+       font-weight: bold;
+}
+button[name="theme"] {
+       float: right;
+}
 table#buttons {
        margin: 0px;
        padding: 0px;
        border-collapse: collapse;
 }
 body {
+       background-color: var(--body-color);
+       filter: var(--body-filter);
        margin: 1px;
        padding: 0px;
        font-family: Arial, sans-serif;
index be7fe61b445df9253d9b38c3677ff8b7a531a60e..2f4bea59a758ca2896c0548277bc86404647103a 100644 (file)
--- a/popup.js
+++ b/popup.js
@@ -42,6 +42,8 @@ function fetchpage(url, callback) {
        }
 }
 
+function get_css_var(key) { return getComputedStyle(document.documentElement).getPropertyValue(`--${key}`); }
+
 function onoff(o) {
        const data = o.toUpperCase();
        switch (data) {
@@ -95,6 +97,13 @@ function toggle_confirm() {
        }, 5000);
 }
 
+/* Toggle the selected theme. */
+function toggle_theme() {
+       const theme = get_css_var('theme') == 'light' ? 'dark' : 'light';
+       const css = $('link#theme-override');
+       css.href = `css/${theme}.css`;
+       storage.set({theme});
+       return false;
 }
 
 function initpopup(xhr, state) {
@@ -126,8 +135,17 @@ function initpopup(xhr, state) {
                cell = row.insertCell(-1);
                cell.colSpan = 10;
                cell.align = 'center';
-               cell.innerText = controller_name.slice(12);
-               cell.innerHTML = '<a href="' + url_base + '" target="_blank">' + cell.innerHTML + '</a>'
+
+               const a = document.createElement('a');
+               a.href = url_base;
+               a.target = '_blank';
+               a.text = controller_name.slice(12).trim();
+               cell.appendChild(a);
+
+               const button = document.createElement('button');
+               button.name = 'theme';
+               button.onclick = toggle_theme;
+               cell.appendChild(button);
        }
 
        var tr, trs = state.currentTarget.responseXML.querySelectorAll('tr');
@@ -188,10 +206,11 @@ function open_settings_page() {
 }
 
 document.addEventListener('DOMContentLoaded', function() {
-       storage.get(settings_keys, function(settings) {
-               url_base = settings['url'] || settings_defaults['url'];
-               user = settings['user'] || settings_defaults['user'];
-               pass = settings['pass'] || settings_defaults['pass'];
+       storage.get(settings_keys, function(settings_storage) {
+               const settings = Object.assign({}, settings_defaults, settings_storage);
+               url_base = settings['url'];
+               user = settings['user'];
+               pass = settings['pass'];
                chrome.permissions.contains({
                        origins: [url_base + '/*']
                }, function(granted) {
diff --git a/theme-init.js b/theme-init.js
new file mode 100644 (file)
index 0000000..8e80673
--- /dev/null
@@ -0,0 +1,9 @@
+// Written by Mike Frysinger <vapier@gmail.com>.  Released into the public domain.
+
+// Load the theme override asap to help with initial loading/flashing.
+chrome.storage.sync.get(['theme'], ({theme}) => {
+       if (theme == 'light' || theme == 'dark') {
+               const css = document.querySelector('link#theme-override');
+               css.href = `css/${theme}.css`;
+       }
+});