X-Git-Url: https://git.wh0rd.org/?a=blobdiff_plain;f=popup.js;h=2f4bea59a758ca2896c0548277bc86404647103a;hb=00a6ceea845df36c7596b1922c1989c509226886;hp=7fca1a2de2935bad13f4c021ee75f444bcc3fe78;hpb=dc7033a88058ecf87b57c8f660d5149a6726a24e;p=chrome-ext%2Fweb-power-switch.git diff --git a/popup.js b/popup.js index 7fca1a2..2f4bea5 100644 --- a/popup.js +++ b/popup.js @@ -42,20 +42,38 @@ function fetchpage(url, callback) { } } +function get_css_var(key) { return getComputedStyle(document.documentElement).getPropertyValue(`--${key}`); } + function onoff(o) { - return o.toUpperCase() === 'ON' ? 'OFF' : 'ON'; + const data = o.toUpperCase(); + switch (data) { + case 'OFF': + return 'ON'; + case 'ON': + return 'OFF'; + default: + // For example, cycle uses CCL. + return data; + } } function toggleit(button) { - var outlet_num = button.id; + const outlet_num = button.id.split(':')[0]; var old_status = button.data; var new_status = onoff(button.data); var url = 'outlet?' + outlet_num + '=' + new_status; + if (!button.id.endsWith('cycle')) { + const cycler = document.getElementById(`${button.id}:cycle`); + cycler.style.display = new_status === 'ON' ? 'block' : 'none'; + } + fetchpage(url, function(xhr, state) { console.log('switch ' + outlet_num + ': ' + old_status + ' -> ' + new_status); - button.value = 'Switch ' + old_status; - button.data = new_status; + if (!button.id.endsWith('cycle')) { + button.value = 'Switch ' + old_status; + button.data = new_status; + } }); } function toggle() { @@ -79,8 +97,13 @@ function toggle_confirm() { }, 5000); } -function trim(str) { - return str.replace(/^\s+|\s+$/, ''); +/* 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) { @@ -104,37 +127,36 @@ function initpopup(xhr, state) { if (th.bgColor != '#DDDDFF') continue; - var controller_name = trim(th.innerText); + var controller_name = th.innerText.trim(); if (controller_name.slice(0, 12) != 'Controller: ') continue; row = tbl.insertRow(-1); cell = row.insertCell(-1); - cell.colSpan = 2; + cell.colSpan = 10; cell.align = 'center'; - cell.innerText = controller_name.slice(12); - cell.innerHTML = '' + cell.innerHTML + '' - } - /* -