]>
git.wh0rd.org - chrome-ext/web-power-switch.git/blob - popup.js
1 // Written by Mike Frysinger <vapier@gmail.com>. Released into the public domain.
3 var url_base
, user
, pass
;
5 function fetchpage(url
, callback
) {
6 url
= url_base
+ '/' + url
;
8 var xhr
= new XMLHttpRequest();
11 xhr
.onreadystatechange = function(state
) {
12 if (xhr
.readyState
== 4) {
13 if (xhr
.status
== 200) {
18 'Could not connect;<br>check your ' +
19 '<a id="open-settings" href="">settings</a>'
21 document
.getElementById('open-settings').onclick
= open_settings_page
;
22 console
.log('connect error', state
);
26 xhr
.onerror = function(error
) {
28 setstatus('onerror; see console');
29 console
.log('xhr error:', error
);
32 console
.log('fetching', url
)
33 xhr
.withCredentials
= true;
34 xhr
.open('GET', url
, true, user
, pass
);
35 xhr
.responseType
= 'document';
36 // The user/pass options above don't seem to work, so do it ourselves.
37 xhr
.setRequestHeader('Authorization', 'Basic ' + btoa(user
+ ':' + pass
));
40 setstatus('Exception; see console');
41 console
.log('exception:', e
);
46 const data
= o
.toUpperCase();
53 // For example, cycle uses CCL.
58 function toggleit(button
) {
59 const outlet_num
= button
.id
.split(':')[0];
60 var old_status
= button
.data
;
61 var new_status
= onoff(button
.data
);
62 var url
= 'outlet?' + outlet_num
+ '=' + new_status
;
64 if (!button
.id
.endsWith('cycle')) {
65 const cycler
= document
.getElementById(`${button.id}:cycle`);
66 cycler
.style
.display
= new_status
=== 'ON' ? 'block' : 'none';
69 fetchpage(url
, function(xhr
, state
) {
70 console
.log('switch ' + outlet_num
+ ': ' + old_status
+ ' -> ' + new_status
);
71 if (!button
.id
.endsWith('cycle')) {
72 button
.value
= 'Switch ' + old_status
;
73 button
.data
= new_status
;
81 function toggle_confirmed() {
82 clearTimeout(this.timeout
);
83 this.onclick
= toggle_confirm
;
87 function toggle_confirm() {
89 this.onclick
= toggle_confirmed
;
90 this.oldvalue
= this.value
;
91 this.value
= 'Confirm!?';
92 this.timeout
= setTimeout(function() {
93 button
.value
= button
.oldvalue
;
94 button
.onclick
= toggle_confirm
;
100 function initpopup(xhr
, state
) {
101 var tbl
= document
.getElementById('buttons');
102 var row
, cell
, button
;
104 console
.log(xhr
, state
);
106 // There is no clean API for extracting the current state.
110 <th bgcolor="#DDDDFF" align=left>
111 Controller: !!!Web Power Switch 6
116 var th
, ths
= state
.currentTarget
.responseXML
.querySelectorAll('th');
117 for (var i
= 0; th
= ths
[i
]; ++i
) {
118 if (th
.bgColor
!= '#DDDDFF')
121 var controller_name
= th
.innerText
.trim();
122 if (controller_name
.slice(0, 12) != 'Controller: ')
125 row
= tbl
.insertRow(-1);
126 cell
= row
.insertCell(-1);
128 cell
.align
= 'center';
129 cell
.innerText
= controller_name
.slice(12);
130 cell
.innerHTML
= '<a href="' + url_base
+ '" target="_blank">' + cell
.innerHTML
+ '</a>'
133 var tr
, trs
= state
.currentTarget
.responseXML
.querySelectorAll('tr');
134 for (var i
= 0; tr
= trs
[i
]; ++i
) {
135 if (tr
.bgColor
!= '#F4F4F4')
138 var outlet_num
= tr
.children
[0].innerText
.trim();
139 var outlet_name
= tr
.children
[1].innerText
.trim();
140 var current_status
= tr
.children
[2].innerText
.trim();
141 var new_status
= tr
.children
[3].innerText
.trim();
142 var confirmable
= tr
.children
[3].children
[0].hasAttribute('onclick');
144 row
= tbl
.insertRow(-1);
145 cell
= row
.insertCell(-1);
146 if (outlet_name
=== '')
147 cell
.innerHTML
= '<i>unnamed</i>';
149 cell
.innerText
= outlet_name
+ ':';
150 cell
= row
.insertCell(-1);
151 button
= document
.createElement('input');
152 button
.type
= 'button';
153 button
.id
= outlet_num
;
154 button
.value
= new_status
;
155 button
.data
= current_status
;
156 button
.onclick
= confirmable
? toggle_confirm
: toggle
;
157 cell
.appendChild(button
);
159 // The switch only allows cycling when it's on.
160 cell
= row
.insertCell(-1);
161 button
= document
.createElement('input');
162 button
.type
= 'button';
163 button
.id
= `${outlet_num}:cycle`;
164 button
.value
= '🗘';
166 button
.title
= 'Power cycle';
167 button
.onclick
= confirmable
? toggle_confirm
: toggle
;
168 if (current_status
.toUpperCase() === 'OFF') {
169 button
.style
.display
= 'none';
171 button
.style
.fontSize
= 'smaller';
172 cell
.appendChild(button
);
178 function setstatus(msg
) {
179 var status
= document
.getElementById('status');
180 status
.innerHTML
= msg
;
181 status
.style
.visibility
= msg
? '' : 'hidden';
182 status
.style
.float = msg
? '' : 'left';
183 status
.style
.position
= msg
? '' : 'absolute';
186 function open_settings_page() {
187 chrome
.runtime
.openOptionsPage();
190 document
.addEventListener('DOMContentLoaded', function() {
191 storage
.get(settings_keys
, function(settings
) {
192 url_base
= settings
['url'] || settings_defaults
['url'];
193 user
= settings
['user'] || settings_defaults
['user'];
194 pass
= settings
['pass'] || settings_defaults
['pass'];
195 chrome
.permissions
.contains({
196 origins
: [url_base
+ '/*']
197 }, function(granted
) {
199 fetchpage('index.htm', initpopup
);
202 'Missing permissions;<br>please visit the ' +
203 '<a id="open-settings" href="">settings page</a>' +
204 '<br>to grant access.<br>' +
205 '<center><input id=retry type=submit value=Retry></center>'
207 document
.getElementById('open-settings').onclick
= open_settings_page
;
208 // Work around http://crbug.com/125706.
209 document
.getElementById('retry').onclick = function() {
210 chrome
.permissions
.request({origins
: [url_base
+ '/*']});
211 fetchpage('index.htm', initpopup
);