]>
git.wh0rd.org - chrome-ext/music-player-client.git/blob - main.js
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();
17 TcpClientSender
.prototype.reconnect = function() {
18 this.tcpclient
.disconnect();
19 this.tcpclient
.connect();
22 function tramp_mpc_recv(data
) {
26 function sync_storage(sync
) {
27 return sync
? chrome
.storage
.sync
: chrome
.storage
.local
;
30 window
.onload = function() {
35 'host', 'port', 'refresh',
38 'host': '192.168.0.2',
44 chrome
.storage
.local
.get(local_keys
, function(settings
) {
45 local_keys
.forEach(function(key
) {
47 options
[key
] = settings
[key
]
50 var storage
= sync_storage(options
['sync']);
51 storage
.get(sync_keys
, function(settings
) {
52 sync_keys
.forEach(function(key
) {
54 options
[key
] = settings
[key
];
57 init_ui(local_keys
, sync_keys
, options
);
63 window
.onkeypress = function(e
) {
64 if (e
.target
!= document
.body
) {
65 /* Only allow the shortcuts when the focus is on the body.
66 Otherwise you can't type these numbers into text fields. */
72 show_page('controls');
75 show_page('metadata');
78 show_page('playlist');
86 function mpc_refresh() {
90 function mpc_connect(host
, port
) {
91 if (typeof(host
) != 'string') {
92 host
= window
['opts_host'].value
;
93 port
= parseInt(window
['opts_port'].value
);
96 if (mpc
!= undefined) {
97 console
.log('disconnecting');
98 update_ui('disconnect');
100 tcpclient
.disconnect();
105 tcpclient
= new TcpClient(host
, port
);
106 tcpclient
.connect(function() {
107 var mpc_sender
= new TcpClientSender(tcpclient
);
108 tcpclient
.addResponseListener(tramp_mpc_recv
);
109 mpc
= new Mpc(mpc_sender
, update_ui
);
110 console
.log('connected to ' + host
+ ':' + port
);
111 console
.log('protip: use the "mpc" object to poke mpd directly.\n' +
112 'you can also do mpc.set_debug(3) to see traffic');
114 update_refresh_timer();
118 function tramp_mpc_consume() {
119 var val
= zo(!getToggleButton(this));
121 setToggleButton(this, val
);
123 function tramp_mpc_deleteid() { mpc
.deleteid(this.title
); }
124 function tramp_mpc_next() { mpc
.next(); }
125 function tramp_mpc_pause() { mpc
.pause(); }
126 function tramp_mpc_play() { mpc
.play(); }
127 function tramp_mpc_previous() { mpc
.previous(); }
128 function tramp_mpc_random() {
129 var val
= zo(!getToggleButton(this));
131 setToggleButton(this, val
);
133 function tramp_mpc_repeat() {
134 var val
= zo(!getToggleButton(this));
136 setToggleButton(this, val
);
138 function tramp_mpc_seekcur() { mpc
.seekcur(this.value
); }
139 function tramp_mpc_setvol() { mpc
.setvol(this.value
); }
140 function tramp_mpc_single() {
141 var val
= zo(!getToggleButton(this));
143 setToggleButton(this, val
);
145 function tramp_mpc_stop() { mpc
.stop(); }
151 return val
== '0' ? 0 : 1;
153 function getToggleButton(btn
) {
154 return btn
.style
.borderStyle
== 'inset';
156 function setToggleButton(btn
, val
) {
157 if (val
=== undefined)
158 val
= !getToggleButton(btn
);
159 btn
.style
.borderStyle
= val
? 'inset' : '';
162 function show_page(page
) {
163 if (typeof(page
) != 'string')
164 page
= this.id
.split('.')[1];
166 // We might not be connected in which case 'mpc' will be undefined.
178 var eles
= document
.getElementsByClassName('main');
179 for (var i
= 0; i
< eles
.length
; ++i
) {
183 if (ele
.id
== 'main.' + page
) {
187 ele
.style
.display
= dis
;
188 document
.getElementById('tab.' + ele
.id
.split('.')[1]).className
= cls
;
192 function do_refresh() {
194 refresh_id
= window
.setTimeout(do_refresh
, window
['opts_refresh'].value
* 1000);
197 function update_refresh_timer() {
198 if (!isNaN(refresh_id
))
199 window
.clearTimeout(refresh_id
);
200 var rate
= window
['opts_refresh'].value
* 1000;
202 refresh_id
= window
.setTimeout(do_refresh
, rate
);
205 function update_local_settings() {
207 setting
[this.id
] = this.checked
;
208 chrome
.storage
.local
.set(setting
);
211 function update_sync_settings() {
213 setting
[this.id
] = this.value
;
214 var storage
= sync_storage(window
['opts_sync'].checked
);
215 storage
.set(setting
);
219 update_refresh_timer();
224 function init_ui(local_keys
, sync_keys
, options
) {
230 'controls', 'metadata', 'playlist', 'options',
231 ].forEach(function(id
) {
232 var ele
= document
.getElementById('tab.' + id
);
233 ele
.onclick
= show_page
;
234 ele
.title
= id
+ ' [' + i
+ ']';
238 /* Setup control tab */
239 ui_mpc_status
= document
.getElementById('status');
241 'consume', 'next', 'pause', 'play', 'previous', 'random', 'repeat',
242 'seekcur', 'setvol', 'single', 'stop',
243 ].forEach(function(id
) {
244 var ele
= window
['ui_mpc_' + id
] = document
.getElementById(id
);
245 ele
.onchange
= ele
.onclick
= window
['tramp_mpc_' + id
];
248 ele
.title
+= ' [' + ele
.accessKey
+ ']'
250 window
['ui_mpc_currtime'] = document
.getElementById('currtime');
252 /* Setup metadata tab */
254 'album', 'artist', 'date', 'file', 'title',
255 ].forEach(function(id
) {
256 window
['ui_mpc_metadata_' + id
] = document
.getElementById('metadata.' + id
);
259 /* Setup playlist tab */
260 window
['ui_mpc_playlist'] = document
.getElementById('playlist');
262 /* Setup options tab */
263 document
.getElementById('connect').onclick
= mpc_connect
;
264 local_keys
.forEach(function(id
) {
265 var ele
= window
['opts_' + id
] = document
.getElementById(id
);
266 ele
.checked
= options
[id
];
267 ele
.onchange
= update_local_settings
;
269 sync_keys
.forEach(function(id
) {
270 var ele
= window
['opts_' + id
] = document
.getElementById(id
);
271 ele
.value
= options
[id
];
272 ele
.oninput
= update_sync_settings
;
276 function pretty_time(time
) {
277 var sec
, min
, hrs
, ret
= '';
278 time
= parseInt(time
);
280 min
= parseInt((time
/ 60) % 60);
281 hrs
= parseInt((time
/ 3600) % 3600);
283 ret
= hrs
+ ':' + ("00" + min
).substr(-2) + ':';
286 return ret
+ ("00" + sec
).substr(-2);
289 function playlist_del() {
290 mpc
.deleteid(this.song_id
);
291 this.parentNode
.remove();
294 function playlist_play() {
295 mpc
.playid(this.song_id
);
296 this.parentNode
.style
.fontWeight
= 'bold';
299 function update_ui(state
, cmd
) {
300 if (typeof(state
) == 'string') {
301 ui_mpc_status
.innerText
= ({
302 'disconnect': 'Disconnecting...',
303 'init': 'Connecting...',
308 if (Array
.isArray(state
)) {
321 /* Update the metadata tab only when things have changed. */
323 if ('Currentsong' in state
) {
324 currentsong
= state
.Currentsong
;
325 if (ui_mpc_metadata_file
.lastUpdate
!= state
.Currentsong
.lastUpdate
) {
326 ui_mpc_metadata_album
.innerText
= currentsong
.Album
;
327 ui_mpc_metadata_artist
.innerText
= currentsong
.Artist
;
328 ui_mpc_metadata_title
.innerText
= currentsong
.Title
;
329 ui_mpc_metadata_date
.innerText
= currentsong
.Date
;
330 ui_mpc_metadata_file
.innerText
= currentsong
.file
;
334 /* Update the playlist tab only when things have changed. */
335 if ('Playlist' in state
&& ui_mpc_playlist
.lastUpdate
!= state
.Playlist
.lastUpdate
) {
336 var playlist
= state
.Playlist
;
338 ui_mpc_playlist
.innerHTML
= '';
339 playlist
.forEach(function(song
) {
340 var cell
, row
= ui_mpc_playlist
.insertRow(-1);
341 if (currentsong
&& song
.Pos
== currentsong
.Pos
)
342 row
.style
.fontWeight
= 'bold';
344 cell
= row
.insertCell(-1);
345 cell
.id
= 'playlist_del';
346 cell
.innerHTML
= '¤';
347 cell
.song_id
= song
.Id
;
348 cell
.title
= 'delete';
349 cell
.onclick
= playlist_del
;
351 cell
= row
.insertCell(-1);
352 cell
.innerText
= song
.Pos
;
353 cell
.style
.textAlign
= 'right';
354 cell
.song_id
= song
.Id
;
356 cell
.onclick
= playlist_play
;
358 if ('Artist' in song
) {
359 row
.insertCell(-1).innerText
= song
.Artist
;
360 row
.insertCell(-1).innerText
= song
.Album
;
361 row
.insertCell(-1).innerText
= song
.Title
;
363 cell
= row
.insertCell(-1);
364 cell
.innerText
= song
.file
;
367 row
.insertCell(-1).innerText
= pretty_time(song
.Time
);
370 ui_mpc_playlist
.lastUpdate
= playlist
.lastUpdate
;
373 /* Update the status tab. */
375 if ('time' in state
) {
376 // When stopped, there is no time field at all.
377 time
= state
.time
.split(':');
378 percent
= Math
.floor((0.0 + time
[0]) * 100 / (0.0 + time
[1]));
383 ui_mpc_seekcur
.max
= time
[1];
384 ui_mpc_seekcur
.value
= time
[0];
385 ui_mpc_seekcur
.title
= 'seekcur (' + percent
+ '%)';
386 ui_mpc_currtime
.innerText
= [pretty_time(time
[0]), pretty_time(time
[1]), percent
+ '%'].join(' / ');
388 ui_mpc_setvol
.title
= 'setvol';
389 if ('volume' in state
) {
390 ui_mpc_setvol
.value
= state
.volume
;
391 ui_mpc_setvol
.title
+= ' (' + state
.volume
+ '%)';
395 'consume', 'random', 'repeat', 'single',
396 ].forEach(function(id
) {
397 setToggleButton(window
['ui_mpc_' + id
], szo(state
[id
]));
400 ui_mpc_status
.innerText
= ({