]>
git.wh0rd.org - chrome-ext/music-player-client.git/blob - main.js
5b05cee5e2c101192829762c3b542f40b49f9d86
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];
169 var eles
= document
.getElementsByClassName('main');
170 for (var i
= 0; i
< eles
.length
; ++i
) {
174 if (ele
.id
== 'main.' + page
) {
178 ele
.style
.display
= dis
;
179 document
.getElementById('tab.' + ele
.id
.split('.')[1]).className
= cls
;
183 function do_refresh() {
185 refresh_id
= window
.setTimeout(do_refresh
, window
['opts_refresh'].value
* 1000);
188 function update_refresh_timer() {
189 if (!isNaN(refresh_id
))
190 window
.clearTimeout(refresh_id
);
191 var rate
= window
['opts_refresh'].value
* 1000;
193 refresh_id
= window
.setTimeout(do_refresh
, rate
);
196 function update_local_settings() {
198 setting
[this.id
] = this.checked
;
199 chrome
.storage
.local
.set(setting
);
202 function update_sync_settings() {
204 setting
[this.id
] = this.value
;
205 var storage
= sync_storage(window
['opts_sync'].checked
);
206 storage
.set(setting
);
210 update_refresh_timer();
215 function init_ui(local_keys
, sync_keys
, options
) {
221 'controls', 'metadata', 'playlist', 'options',
222 ].forEach(function(id
) {
223 var ele
= document
.getElementById('tab.' + id
);
224 ele
.onclick
= show_page
;
225 ele
.title
= id
+ ' [' + i
+ ']';
229 /* Setup control tab */
230 ui_mpc_status
= document
.getElementById('status');
232 'consume', 'next', 'pause', 'play', 'previous', 'random', 'repeat',
233 'seekcur', 'setvol', 'single', 'stop',
234 ].forEach(function(id
) {
235 var ele
= window
['ui_mpc_' + id
] = document
.getElementById(id
);
236 ele
.onchange
= ele
.onclick
= window
['tramp_mpc_' + id
];
239 ele
.title
+= ' [' + ele
.accessKey
+ ']'
241 window
['ui_mpc_currtime'] = document
.getElementById('currtime');
243 /* Setup metadata tab */
245 'album', 'artist', 'date', 'file', 'title',
246 ].forEach(function(id
) {
247 window
['ui_mpc_metadata_' + id
] = document
.getElementById('metadata.' + id
);
250 /* Setup playlist tab */
251 window
['ui_mpc_playlist'] = document
.getElementById('playlist');
253 /* Setup options tab */
254 document
.getElementById('connect').onclick
= mpc_connect
;
255 local_keys
.forEach(function(id
) {
256 var ele
= window
['opts_' + id
] = document
.getElementById(id
);
257 ele
.checked
= options
[id
];
258 ele
.onchange
= update_local_settings
;
260 sync_keys
.forEach(function(id
) {
261 var ele
= window
['opts_' + id
] = document
.getElementById(id
);
262 ele
.value
= options
[id
];
263 ele
.oninput
= update_sync_settings
;
267 function pretty_time(time
) {
268 var sec
, min
, hrs
, ret
= '';
269 time
= parseInt(time
);
271 min
= parseInt((time
/ 60) % 60);
272 hrs
= parseInt((time
/ 3600) % 3600);
274 ret
= hrs
+ ':' + ("00" + min
).substr(-2) + ':';
277 return ret
+ ("00" + sec
).substr(-2);
280 function playlist_del() {
281 mpc
.deleteid(this.song_id
);
282 this.parentNode
.remove();
285 function playlist_play() {
286 mpc
.playid(this.song_id
);
287 this.parentNode
.style
.fontWeight
= 'bold';
290 function update_ui(state
, cmd
) {
291 if (typeof(state
) == 'string') {
292 ui_mpc_status
.innerText
= ({
293 'disconnect': 'Disconnecting...',
294 'init': 'Connecting...',
299 if (Array
.isArray(state
)) {
312 /* Update the metadata tab. */
313 var currentsong
= {};
314 if ('Currentsong' in state
)
315 currentsong
= state
.Currentsong
;
316 ui_mpc_metadata_album
.innerText
= currentsong
.Album
;
317 ui_mpc_metadata_artist
.innerText
= currentsong
.Artist
;
318 ui_mpc_metadata_title
.innerText
= currentsong
.Title
;
319 ui_mpc_metadata_date
.innerText
= currentsong
.Date
;
320 ui_mpc_metadata_file
.innerText
= currentsong
.file
;
322 /* Update the playlist tab. */
324 if ('Playlist' in state
)
325 playlist
= state
.Playlist
;
326 ui_mpc_playlist
.innerHTML
= '';
327 playlist
.forEach(function(song
) {
328 var cell
, row
= ui_mpc_playlist
.insertRow(-1);
329 if (song
.Pos
== currentsong
.Pos
)
330 row
.style
.fontWeight
= 'bold';
332 cell
= row
.insertCell(-1);
333 cell
.id
= 'playlist_del';
334 cell
.innerHTML
= '¤';
335 cell
.song_id
= song
.Id
;
336 cell
.title
= 'delete';
337 cell
.onclick
= playlist_del
;
339 cell
= row
.insertCell(-1);
340 cell
.innerText
= song
.Pos
;
341 cell
.style
.textAlign
= 'right';
342 cell
.song_id
= song
.Id
;
344 cell
.onclick
= playlist_play
;
346 if ('Artist' in song
) {
347 row
.insertCell(-1).innerText
= song
.Artist
;
348 row
.insertCell(-1).innerText
= song
.Album
;
349 row
.insertCell(-1).innerText
= song
.Title
;
351 cell
= row
.insertCell(-1);
352 cell
.innerText
= song
.file
;
355 row
.insertCell(-1).innerText
= pretty_time(song
.Time
);
358 /* Update the status tab. */
361 // When stopped, there is no time field at all.
362 time
= state
.time
.split(':');
365 ui_mpc_seekcur
.max
= time
[1];
366 ui_mpc_seekcur
.value
= time
[0];
367 percent
= Math
.floor((0.0 + time
[0]) * 100 / (0.0 + time
[1]));
368 ui_mpc_seekcur
.title
= 'seekcur (' + percent
+ '%)';
369 ui_mpc_currtime
.innerText
= [pretty_time(time
[0]), pretty_time(time
[1]), percent
+ '%'].join(' / ');
371 ui_mpc_setvol
.value
= state
.volume
;
372 ui_mpc_setvol
.title
= 'setvol (' + state
.volume
+ '%)';
375 'consume', 'random', 'repeat', 'single',
376 ].forEach(function(id
) {
377 setToggleButton(window
['ui_mpc_' + id
], szo(state
[id
]));
380 ui_mpc_status
.innerText
= ({