]>
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
) {
66 show_page('controls');
69 show_page('metadata');
72 show_page('playlist');
80 function mpc_refresh() {
84 function mpc_connect(host
, port
) {
85 if (typeof(host
) != 'string') {
86 host
= window
['opts_host'].value
;
87 port
= parseInt(window
['opts_port'].value
);
90 if (mpc
!= undefined) {
91 console
.log('disconnecting');
92 update_ui('disconnect');
94 tcpclient
.disconnect();
99 tcpclient
= new TcpClient(host
, port
);
100 tcpclient
.connect(function() {
101 var mpc_sender
= new TcpClientSender(tcpclient
);
102 tcpclient
.addResponseListener(tramp_mpc_recv
);
103 mpc
= new Mpc(mpc_sender
, update_ui
);
104 console
.log('connected to ' + host
+ ':' + port
);
105 console
.log('protip: use the "mpc" object to poke mpd directly.\n' +
106 'you can also do mpc.set_debug(3) to see traffic');
108 update_refresh_timer();
112 function tramp_mpc_consume() {
113 var val
= zo(!getToggleButton(this));
115 setToggleButton(this, val
);
117 function tramp_mpc_deleteid() { mpc
.deleteid(this.title
); }
118 function tramp_mpc_next() { mpc
.next(); }
119 function tramp_mpc_pause() { mpc
.pause(); }
120 function tramp_mpc_play() { mpc
.play(); }
121 function tramp_mpc_previous() { mpc
.previous(); }
122 function tramp_mpc_random() {
123 var val
= zo(!getToggleButton(this));
125 setToggleButton(this, val
);
127 function tramp_mpc_repeat() {
128 var val
= zo(!getToggleButton(this));
130 setToggleButton(this, val
);
132 function tramp_mpc_seekcur() { mpc
.seekcur(this.value
); }
133 function tramp_mpc_setvol() { mpc
.setvol(this.value
); }
134 function tramp_mpc_single() {
135 var val
= zo(!getToggleButton(this));
137 setToggleButton(this, val
);
139 function tramp_mpc_stop() { mpc
.stop(); }
145 return val
== '0' ? 0 : 1;
147 function getToggleButton(btn
) {
148 return btn
.style
.borderStyle
== 'inset';
150 function setToggleButton(btn
, val
) {
151 if (val
=== undefined)
152 val
= !getToggleButton(btn
);
153 btn
.style
.borderStyle
= val
? 'inset' : '';
156 function show_page(page
) {
157 if (typeof(page
) != 'string')
158 page
= this.id
.split('.')[1];
160 // We might not be connected in which case 'mpc' will be undefined.
172 var eles
= document
.getElementsByClassName('main');
173 for (var i
= 0; i
< eles
.length
; ++i
) {
177 if (ele
.id
== 'main.' + page
) {
181 ele
.style
.display
= dis
;
182 document
.getElementById('tab.' + ele
.id
.split('.')[1]).className
= cls
;
186 function do_refresh() {
188 refresh_id
= window
.setTimeout(do_refresh
, window
['opts_refresh'].value
* 1000);
191 function update_refresh_timer() {
192 if (!isNaN(refresh_id
))
193 window
.clearTimeout(refresh_id
);
194 var rate
= window
['opts_refresh'].value
* 1000;
196 refresh_id
= window
.setTimeout(do_refresh
, rate
);
199 function update_local_settings() {
201 setting
[this.id
] = this.checked
;
202 chrome
.storage
.local
.set(setting
);
205 function update_sync_settings() {
207 setting
[this.id
] = this.value
;
208 var storage
= sync_storage(window
['opts_sync'].checked
);
209 storage
.set(setting
);
213 update_refresh_timer();
218 function init_ui(local_keys
, sync_keys
, options
) {
224 'controls', 'metadata', 'playlist', 'options',
225 ].forEach(function(id
) {
226 var ele
= document
.getElementById('tab.' + id
);
227 ele
.onclick
= show_page
;
228 ele
.title
= id
+ ' [' + i
+ ']';
232 /* Setup control tab */
233 ui_mpc_status
= document
.getElementById('status');
235 'consume', 'next', 'pause', 'play', 'previous', 'random', 'repeat',
236 'seekcur', 'setvol', 'single', 'stop',
237 ].forEach(function(id
) {
238 var ele
= window
['ui_mpc_' + id
] = document
.getElementById(id
);
239 ele
.onchange
= ele
.onclick
= window
['tramp_mpc_' + id
];
242 ele
.title
+= ' [' + ele
.accessKey
+ ']'
244 window
['ui_mpc_currtime'] = document
.getElementById('currtime');
246 /* Setup metadata tab */
248 'album', 'artist', 'date', 'file', 'title',
249 ].forEach(function(id
) {
250 window
['ui_mpc_metadata_' + id
] = document
.getElementById('metadata.' + id
);
253 /* Setup playlist tab */
254 window
['ui_mpc_playlist'] = document
.getElementById('playlist');
256 /* Setup options tab */
257 document
.getElementById('connect').onclick
= mpc_connect
;
258 local_keys
.forEach(function(id
) {
259 var ele
= window
['opts_' + id
] = document
.getElementById(id
);
260 ele
.checked
= options
[id
];
261 ele
.onchange
= update_local_settings
;
263 sync_keys
.forEach(function(id
) {
264 var ele
= window
['opts_' + id
] = document
.getElementById(id
);
265 ele
.value
= options
[id
];
266 ele
.oninput
= update_sync_settings
;
270 function pretty_time(time
) {
271 var sec
, min
, hrs
, ret
= '';
272 time
= parseInt(time
);
274 min
= parseInt((time
/ 60) % 60);
275 hrs
= parseInt((time
/ 3600) % 3600);
277 ret
= hrs
+ ':' + ("00" + min
).substr(-2) + ':';
280 return ret
+ ("00" + sec
).substr(-2);
283 function playlist_del() {
284 mpc
.deleteid(this.song_id
);
285 this.parentNode
.remove();
288 function playlist_play() {
289 mpc
.playid(this.song_id
);
290 this.parentNode
.style
.fontWeight
= 'bold';
293 function update_ui(state
, cmd
) {
294 if (typeof(state
) == 'string') {
295 ui_mpc_status
.innerText
= ({
296 'disconnect': 'Disconnecting...',
297 'init': 'Connecting...',
302 if (Array
.isArray(state
)) {
315 /* Update the metadata tab only when things have changed. */
317 if ('Currentsong' in state
) {
318 currentsong
= state
.Currentsong
;
319 if (ui_mpc_metadata_file
.lastUpdate
!= state
.Currentsong
.lastUpdate
) {
320 ui_mpc_metadata_album
.innerText
= currentsong
.Album
;
321 ui_mpc_metadata_artist
.innerText
= currentsong
.Artist
;
322 ui_mpc_metadata_title
.innerText
= currentsong
.Title
;
323 ui_mpc_metadata_date
.innerText
= currentsong
.Date
;
324 ui_mpc_metadata_file
.innerText
= currentsong
.file
;
328 /* Update the playlist tab only when things have changed. */
329 if ('Playlist' in state
&& ui_mpc_playlist
.lastUpdate
!= state
.Playlist
.lastUpdate
) {
330 var playlist
= state
.Playlist
;
332 ui_mpc_playlist
.innerHTML
= '';
333 playlist
.forEach(function(song
) {
334 var cell
, row
= ui_mpc_playlist
.insertRow(-1);
335 if (currentsong
&& song
.Pos
== currentsong
.Pos
)
336 row
.style
.fontWeight
= 'bold';
338 cell
= row
.insertCell(-1);
339 cell
.id
= 'playlist_del';
340 cell
.innerHTML
= '¤';
341 cell
.song_id
= song
.Id
;
342 cell
.title
= 'delete';
343 cell
.onclick
= playlist_del
;
345 cell
= row
.insertCell(-1);
346 cell
.innerText
= song
.Pos
;
347 cell
.style
.textAlign
= 'right';
348 cell
.song_id
= song
.Id
;
350 cell
.onclick
= playlist_play
;
352 if ('Artist' in song
) {
353 row
.insertCell(-1).innerText
= song
.Artist
;
354 row
.insertCell(-1).innerText
= song
.Album
;
355 row
.insertCell(-1).innerText
= song
.Title
;
357 cell
= row
.insertCell(-1);
358 cell
.innerText
= song
.file
;
361 row
.insertCell(-1).innerText
= pretty_time(song
.Time
);
364 ui_mpc_playlist
.lastUpdate
= playlist
.lastUpdate
;
367 /* Update the status tab. */
369 if ('time' in state
) {
370 // When stopped, there is no time field at all.
371 time
= state
.time
.split(':');
372 percent
= Math
.floor((0.0 + time
[0]) * 100 / (0.0 + time
[1]));
377 ui_mpc_seekcur
.max
= time
[1];
378 ui_mpc_seekcur
.value
= time
[0];
379 ui_mpc_seekcur
.title
= 'seekcur (' + percent
+ '%)';
380 ui_mpc_currtime
.innerText
= [pretty_time(time
[0]), pretty_time(time
[1]), percent
+ '%'].join(' / ');
382 ui_mpc_setvol
.title
= 'setvol';
383 if ('volume' in state
) {
384 ui_mpc_setvol
.value
= state
.volume
;
385 ui_mpc_setvol
.title
+= ' (' + state
.volume
+ '%)';
389 'consume', 'random', 'repeat', 'single',
390 ].forEach(function(id
) {
391 setToggleButton(window
['ui_mpc_' + id
], szo(state
[id
]));
394 ui_mpc_status
.innerText
= ({