]>
Commit | Line | Data |
---|---|---|
a98f3ef6 MF |
1 | // Written by Mike Frysinger <vapier@gmail.com>. Released into the public domain. Suck it. |
2 | ||
3 | var url_base, user, pass; | |
4 | ||
5 | function fetchpage(url, callback) { | |
6 | url = url_base + '/' + url; | |
7 | ||
8 | var xhr = new XMLHttpRequest(); | |
9 | xhr.setstatus = false; | |
10 | try { | |
11 | xhr.onreadystatechange = function(state) { | |
12 | if (xhr.readyState == 4) { | |
13 | if (xhr.status == 200) { | |
14 | callback(xhr, state); | |
15 | } else { | |
16 | xhr.setstatus = true; | |
e981c837 MF |
17 | setstatus('Could not connect;<br>check your ' + |
18 | '<a href="' + chrome.extension.getURL('options.html') + '" target=_blank>settings</a>'); | |
a98f3ef6 MF |
19 | console.log('connect error', state); |
20 | } | |
21 | } | |
22 | } | |
23 | xhr.onerror = function(error) { | |
24 | if (!xhr.setstatus) | |
25 | setstatus('onerror; see console'); | |
26 | console.log('xhr error:', error); | |
27 | } | |
28 | ||
29 | console.log('fetching', url) | |
30 | xhr.withCredentials = true; | |
31 | xhr.open('GET', url, true, user, pass); | |
32 | xhr.responseType = 'document'; | |
33 | // The user/pass options above don't seem to work, so do it ourselves. | |
34 | xhr.setRequestHeader('Authorization', 'Basic ' + btoa(user + ':' + pass)); | |
35 | xhr.send(); | |
36 | } catch(e) { | |
37 | setstatus('Exception; see console'); | |
38 | console.log('exception:', e); | |
39 | } | |
40 | } | |
41 | ||
42 | function onoff(o) { | |
43 | return o.toUpperCase() === 'ON' ? 'OFF' : 'ON'; | |
44 | } | |
45 | ||
46 | function toggleit(button) { | |
47 | var outlet_num = button.id; | |
48 | var old_status = button.data; | |
49 | var new_status = onoff(button.data); | |
50 | var url = 'outlet?' + outlet_num + '=' + new_status; | |
51 | ||
52 | fetchpage(url, function(xhr, state) { | |
53 | console.log('switch ' + outlet_num + ': ' + old_status + ' -> ' + new_status); | |
54 | button.value = 'Switch ' + old_status; | |
55 | button.data = new_status; | |
56 | }); | |
57 | } | |
58 | function toggle() { | |
59 | toggleit(this); | |
60 | } | |
61 | ||
62 | function toggle_confirmed() { | |
63 | clearTimeout(this.timeout); | |
64 | this.onclick = toggle_confirm; | |
65 | toggleit(this); | |
66 | } | |
67 | ||
68 | function toggle_confirm() { | |
69 | var button = this; | |
70 | this.onclick = toggle_confirmed; | |
71 | this.oldvalue = this.value; | |
72 | this.value = 'Confirm!?'; | |
73 | this.timeout = setTimeout(function() { | |
74 | button.value = button.oldvalue; | |
75 | button.onclick = toggle_confirm; | |
76 | }, 5000); | |
77 | } | |
78 | ||
79 | function trim(str) { | |
80 | return str.replace(/^\s+|\s+$/, ''); | |
81 | } | |
82 | ||
83 | function initpopup(xhr, state) { | |
84 | var tbl = document.getElementById('buttons'); | |
85 | var row, cell, button; | |
86 | ||
87 | console.log(xhr, state); | |
88 | ||
89 | // There is no clean API for extracting the current state. | |
90 | // Example result: | |
91 | /* | |
92 | <tr bgcolor="#F4F4F4"><td align=center>1</td> | |
93 | <td>Outlet 1</td><td> | |
94 | <b><font color=red>OFF</font></b></td><td> | |
95 | <a href=outlet?1=ON>Switch ON</a> | |
96 | </td><td> | |
97 | <!-- <a href=outlet?1=CCL>Cycle</a> --> | |
98 | </td></tr> | |
99 | */ | |
100 | ||
101 | var tr, trs = state.currentTarget.responseXML.querySelectorAll('tr'); | |
102 | for (var i = 0; tr = trs[i]; ++i) { | |
103 | if (tr.bgColor != '#F4F4F4') | |
104 | continue; | |
105 | ||
106 | var outlet_num = trim(tr.children[0].innerText); | |
107 | var outlet_name = trim(tr.children[1].innerText); | |
108 | var current_status = trim(tr.children[2].innerText); | |
109 | var new_status = trim(tr.children[3].innerText); | |
110 | var confirmable = tr.children[3].children[0].hasAttribute('onclick'); | |
111 | ||
112 | row = tbl.insertRow(-1); | |
113 | cell = row.insertCell(-1); | |
114 | cell.innerText = outlet_name + ':'; | |
115 | cell = row.insertCell(-1); | |
116 | button = document.createElement('input'); | |
117 | button.type = 'button'; | |
118 | button.id = outlet_num; | |
119 | button.value = new_status; | |
120 | button.data = current_status; | |
121 | button.onclick = confirmable ? toggle_confirm : toggle; | |
122 | cell.appendChild(button); | |
123 | } | |
124 | ||
125 | setstatus(); | |
126 | } | |
127 | ||
128 | function setstatus(msg) { | |
129 | var status = document.getElementById('status'); | |
e981c837 | 130 | status.innerHTML = msg; |
a98f3ef6 MF |
131 | status.style.visibility = msg ? '' : 'hidden'; |
132 | status.style.float = msg ? '' : 'left'; | |
133 | status.style.position = msg ? '' : 'absolute'; | |
134 | } | |
135 | ||
136 | document.addEventListener('DOMContentLoaded', function() { | |
137 | storage.get(settings_keys, function(settings) { | |
138 | url_base = settings['url'] || settings_defaults['url']; | |
139 | user = settings['user'] || settings_defaults['user']; | |
140 | pass = settings['pass'] || settings_defaults['pass']; | |
141 | fetchpage('index.htm', initpopup); | |
142 | }); | |
143 | }); |