1 // Written by Mike Frysinger <vapier@gmail.com>. Released into the public domain. Suck it.
3 /* Globals to allow easy manipulation via javascript console */
8 function TcpClientSender(tcpclient) {
9 this.tcpclient = tcpclient;
11 TcpClientSender.prototype.send = function(data, cb) {
12 this.tcpclient.sendMessage(data, cb);
14 TcpClientSender.prototype.poll = function() {
15 this.tcpclient.poll();
18 function tramp_mpc_recv(data) {
22 function sync_storage(sync) {
23 return sync ? chrome.storage.sync : chrome.storage.local;
26 window.onload = function() {
31 'host', 'port', 'refresh',
34 'host': '192.168.0.2',
40 chrome.storage.local.get(local_keys, function(settings) {
41 local_keys.forEach(function(key) {
43 options[key] = settings[key]
46 var storage = sync_storage(options['sync']);
47 storage.get(sync_keys, function(settings) {
48 sync_keys.forEach(function(key) {
50 options[key] = settings[key];
53 init_ui(local_keys, sync_keys, options);
59 function mpc_refresh() {
64 function mpc_connect(host, port) {
65 if (typeof(host) != 'string') {
66 host = window['opts_host'].value;
67 port = parseInt(window['opts_port'].value);
70 if (mpc != undefined) {
71 console.log('disconnecting');
72 update_ui('disconnect');
74 tcpclient.disconnect();
79 tcpclient = new TcpClient(host, port);
80 tcpclient.connect(function() {
81 var mpc_sender = new TcpClientSender(tcpclient);
82 tcpclient.addResponseListener(tramp_mpc_recv);
83 mpc = new Mpc(mpc_sender, update_ui);
84 console.log('connected to ' + host + ':' + port);
85 console.log('protip: use the "mpc" object to poke mpd directly.\n' +
86 'you can also do mpc.set_debug(3) to see traffic');
88 update_refresh_timer();
92 function tramp_mpc_consume() {
93 var val = zo(!getToggleButton(this));
95 setToggleButton(this, val);
97 function tramp_mpc_next() { mpc.next(); }
98 function tramp_mpc_pause() { mpc.pause(); }
99 function tramp_mpc_play() { mpc.play(); }
100 function tramp_mpc_previous() { mpc.previous(); }
101 function tramp_mpc_random() {
102 var val = zo(!getToggleButton(this));
104 setToggleButton(this, val);
106 function tramp_mpc_repeat() {
107 var val = zo(!getToggleButton(this));
109 setToggleButton(this, val);
111 function tramp_mpc_seekcur() { mpc.seekcur(this.value); }
112 function tramp_mpc_setvol() { mpc.setvol(this.value); }
113 function tramp_mpc_single() {
114 var val = zo(!getToggleButton(this));
116 setToggleButton(this, val);
118 function tramp_mpc_stop() { mpc.stop(); }
124 return val == '0' ? 0 : 1;
126 function getToggleButton(btn) {
127 return btn.style.borderStyle == 'inset';
129 function setToggleButton(btn, val) {
130 if (val === undefined)
131 val = !getToggleButton(btn);
132 btn.style.borderStyle = val ? 'inset' : '';
135 function show_page(page) {
136 if (typeof(page) != 'string')
137 page = this.id.split('.')[1];
139 var eles = document.getElementsByClassName('main');
140 for (var i = 0; i < eles.length; ++i) {
144 if (ele.id == 'main.' + page) {
148 ele.style.display = dis;
149 document.getElementById('tab.' + ele.id.split('.')[1]).className = cls;
153 function do_refresh() {
155 refresh_id = window.setTimeout(do_refresh, window['opts_refresh'].value * 1000);
158 function update_refresh_timer() {
159 if (!isNaN(refresh_id))
160 window.clearTimeout(refresh_id);
161 var rate = window['opts_refresh'].value * 1000;
163 refresh_id = window.setTimeout(do_refresh, rate);
166 function update_local_settings() {
168 setting[this.id] = this.checked;
169 chrome.storage.local.set(setting);
172 function update_sync_settings() {
174 setting[this.id] = this.value;
175 var storage = sync_storage(window['opts_sync'].checked);
176 storage.set(setting);
180 update_refresh_timer();
185 function init_ui(local_keys, sync_keys, options) {
188 'controls', 'metadata', 'options',
189 ].forEach(function(id) {
190 document.getElementById('tab.' + id).onclick = show_page;
193 /* Setup control tab */
194 ui_mpc_status = document.getElementById('status');
196 'consume', 'next', 'pause', 'play', 'previous', 'random', 'repeat',
197 'seekcur', 'setvol', 'single', 'stop',
198 ].forEach(function(id) {
199 var ele = window['ui_mpc_' + id] = document.getElementById(id);
200 ele.onchange = ele.onclick = window['tramp_mpc_' + id];
204 /* Setup metadata tab */
206 'album', 'artist', 'date', 'file', 'title',
207 ].forEach(function(id) {
208 window['ui_mpc_metadata_' + id] = document.getElementById('metadata.' + id);
211 /* Setup options tab */
212 document.getElementById('connect').onclick = mpc_connect;
213 local_keys.forEach(function(id) {
214 var ele = window['opts_' + id] = document.getElementById(id);
215 ele.checked = options[id];
216 ele.onchange = update_local_settings;
218 sync_keys.forEach(function(id) {
219 var ele = window['opts_' + id] = document.getElementById(id);
220 ele.value = options[id];
221 ele.oninput = update_sync_settings;
225 function update_ui(state, cmd) {
226 if (typeof(state) == 'string') {
227 ui_mpc_status.innerText = ({
228 'disconnect': 'Disconnecting...',
229 'init': 'Connecting...',
234 if (Array.isArray(state)) {
247 // Hack: should be a real object.
248 ui_mpc_metadata_album.innerText = state.Album;
249 ui_mpc_metadata_artist.innerText = state.Artist;
250 ui_mpc_metadata_title.innerText = state.Title;
251 ui_mpc_metadata_date.innerText = state.Date;
252 ui_mpc_metadata_file.innerText = state.file;
256 // When stopped, there is no time field at all.
257 time = state.time.split(':');
260 window.ui_mpc_seekcur.max = time[1];
261 window.ui_mpc_seekcur.value = time[0];
262 percent = Math.floor((0.0 + time[0]) * 100 / (0.0 + time[1]));
263 window.ui_mpc_seekcur.title = 'seekcur (' + percent + '%)';
265 window.ui_mpc_setvol.value = state.volume;
266 window.ui_mpc_setvol.title = 'setvol (' + state.volume + '%)';
269 'consume', 'random', 'repeat', 'single',
270 ].forEach(function(id) {
271 setToggleButton(window['ui_mpc_' + id], szo(state[id]));
274 ui_mpc_status.innerText = ({