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);
15 function tramp_mpc_recv(data) {
19 function sync_storage(sync) {
20 return sync ? chrome.storage.sync : chrome.storage.local;
23 window.onload = function() {
28 'host', 'port', 'refresh',
31 'host': '192.168.0.2',
37 chrome.storage.local.get(local_keys, function(settings) {
38 local_keys.forEach(function(key) {
40 options[key] = settings[key]
43 var storage = sync_storage(options['sync']);
44 storage.get(sync_keys, function(settings) {
45 sync_keys.forEach(function(key) {
47 options[key] = settings[key];
50 init_ui(local_keys, sync_keys, options);
56 function mpc_refresh() {
61 function mpc_connect(host, port) {
62 if (typeof(host) != 'string') {
63 host = window['opts_host'].value;
64 port = parseInt(window['opts_port'].value);
67 if (mpc != undefined) {
68 console.log('disconnecting');
69 update_ui('disconnect');
71 tcpclient.disconnect();
76 tcpclient = new TcpClient(host, port);
77 tcpclient.connect(function() {
78 var mpc_sender = new TcpClientSender(tcpclient);
79 tcpclient.addResponseListener(tramp_mpc_recv);
80 mpc = new Mpc(mpc_sender, update_ui);
81 console.log('connected to ' + host + ':' + port);
82 console.log('protip: use the "mpc" object to poke mpd directly.\n' +
83 'you can also do mpc.set_debug(3) to see traffic');
85 update_refresh_timer();
89 function tramp_mpc_consume() {
90 var val = zo(!getToggleButton(this));
92 setToggleButton(this, val);
94 function tramp_mpc_next() { mpc.next(); }
95 function tramp_mpc_pause() { mpc.pause(); }
96 function tramp_mpc_play() { mpc.play(); }
97 function tramp_mpc_previous() { mpc.previous(); }
98 function tramp_mpc_random() {
99 var val = zo(!getToggleButton(this));
101 setToggleButton(this, val);
103 function tramp_mpc_repeat() {
104 var val = zo(!getToggleButton(this));
106 setToggleButton(this, val);
108 function tramp_mpc_seekcur() { mpc.seekcur(this.value); }
109 function tramp_mpc_setvol() { mpc.setvol(this.value); }
110 function tramp_mpc_single() {
111 var val = zo(!getToggleButton(this));
113 setToggleButton(this, val);
115 function tramp_mpc_stop() { mpc.stop(); }
121 return val == '0' ? 0 : 1;
123 function getToggleButton(btn) {
124 return btn.style.borderStyle == 'inset';
126 function setToggleButton(btn, val) {
127 if (val === undefined)
128 val = !getToggleButton(btn);
129 btn.style.borderStyle = val ? 'inset' : '';
132 function show_page(page) {
133 if (typeof(page) != 'string')
134 page = this.id.split('.')[1];
136 var eles = document.getElementsByClassName('main');
137 for (var i = 0; i < eles.length; ++i) {
141 if (ele.id == 'main.' + page) {
145 ele.style.display = dis;
146 document.getElementById('tab.' + ele.id.split('.')[1]).className = cls;
150 function do_refresh() {
152 refresh_id = window.setTimeout(do_refresh, window['opts_refresh'].value * 1000);
155 function update_refresh_timer() {
156 if (!isNaN(refresh_id))
157 window.clearTimeout(refresh_id);
158 var rate = window['opts_refresh'].value * 1000;
160 refresh_id = window.setTimeout(do_refresh, rate);
163 function update_local_settings() {
165 setting[this.id] = this.checked;
166 chrome.storage.local.set(setting);
169 function update_sync_settings() {
171 setting[this.id] = this.value;
172 var storage = sync_storage(window['opts_sync'].checked);
173 storage.set(setting);
177 update_refresh_timer();
182 function init_ui(local_keys, sync_keys, options) {
185 'controls', 'metadata', 'options',
186 ].forEach(function(id) {
187 document.getElementById('tab.' + id).onclick = show_page;
190 /* Setup control tab */
191 ui_mpc_status = document.getElementById('status');
192 ui_mpc_metadata = document.getElementById('metadata');
194 'consume', 'next', 'pause', 'play', 'previous', 'random', 'repeat',
195 'seekcur', 'setvol', 'single', 'stop',
196 ].forEach(function(id) {
197 var ele = window['ui_mpc_' + id] = document.getElementById(id);
198 ele.onchange = ele.onclick = window['tramp_mpc_' + id];
202 /* Setup options tab */
203 document.getElementById('connect').onclick = mpc_connect;
204 local_keys.forEach(function(id) {
205 var ele = window['opts_' + id] = document.getElementById(id);
206 ele.checked = options[id];
207 ele.onchange = update_local_settings;
209 sync_keys.forEach(function(id) {
210 var ele = window['opts_' + id] = document.getElementById(id);
211 ele.value = options[id];
212 ele.oninput = update_sync_settings;
216 function update_ui(state, cmd) {
217 if (typeof(state) == 'string') {
218 ui_mpc_status.innerText = ({
219 'disconnect': 'Disconnecting...',
220 'init': 'Connecting...',
225 if (Array.isArray(state)) {
238 if ('file' in state) {
239 // Hack: should be a real object.
240 ui_mpc_metadata.innerText = state['file'];
245 // When stopped, there is no time field at all.
246 time = state.time.split(':');
249 window['ui_mpc_seekcur'].max = time[1];
250 window['ui_mpc_seekcur'].value = time[0];
252 window['ui_mpc_setvol'].value = state.volume;
254 'consume', 'random', 'repeat', 'single',
255 ].forEach(function(id) {
256 setToggleButton(window['ui_mpc_' + id], szo(state[id]));
259 ui_mpc_status.innerText = ({