]>
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();
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 window
.onkeypress = function(e
) {
62 show_page('controls');
65 show_page('metadata');
68 show_page('playlist');
76 function mpc_refresh() {
80 function mpc_connect(host
, port
) {
81 if (typeof(host
) != 'string') {
82 host
= window
['opts_host'].value
;
83 port
= parseInt(window
['opts_port'].value
);
86 if (mpc
!= undefined) {
87 console
.log('disconnecting');
88 update_ui('disconnect');
90 tcpclient
.disconnect();
95 tcpclient
= new TcpClient(host
, port
);
96 tcpclient
.connect(function() {
97 var mpc_sender
= new TcpClientSender(tcpclient
);
98 tcpclient
.addResponseListener(tramp_mpc_recv
);
99 mpc
= new Mpc(mpc_sender
, update_ui
);
100 console
.log('connected to ' + host
+ ':' + port
);
101 console
.log('protip: use the "mpc" object to poke mpd directly.\n' +
102 'you can also do mpc.set_debug(3) to see traffic');
104 update_refresh_timer();
108 function tramp_mpc_consume() {
109 var val
= zo(!getToggleButton(this));
111 setToggleButton(this, val
);
113 function tramp_mpc_deleteid() { mpc
.deleteid(this.title
); }
114 function tramp_mpc_next() { mpc
.next(); }
115 function tramp_mpc_pause() { mpc
.pause(); }
116 function tramp_mpc_play() { mpc
.play(); }
117 function tramp_mpc_previous() { mpc
.previous(); }
118 function tramp_mpc_random() {
119 var val
= zo(!getToggleButton(this));
121 setToggleButton(this, val
);
123 function tramp_mpc_repeat() {
124 var val
= zo(!getToggleButton(this));
126 setToggleButton(this, val
);
128 function tramp_mpc_seekcur() { mpc
.seekcur(this.value
); }
129 function tramp_mpc_setvol() { mpc
.setvol(this.value
); }
130 function tramp_mpc_single() {
131 var val
= zo(!getToggleButton(this));
133 setToggleButton(this, val
);
135 function tramp_mpc_stop() { mpc
.stop(); }
141 return val
== '0' ? 0 : 1;
143 function getToggleButton(btn
) {
144 return btn
.style
.borderStyle
== 'inset';
146 function setToggleButton(btn
, val
) {
147 if (val
=== undefined)
148 val
= !getToggleButton(btn
);
149 btn
.style
.borderStyle
= val
? 'inset' : '';
152 function show_page(page
) {
153 if (typeof(page
) != 'string')
154 page
= this.id
.split('.')[1];
165 var eles
= document
.getElementsByClassName('main');
166 for (var i
= 0; i
< eles
.length
; ++i
) {
170 if (ele
.id
== 'main.' + page
) {
174 ele
.style
.display
= dis
;
175 document
.getElementById('tab.' + ele
.id
.split('.')[1]).className
= cls
;
179 function do_refresh() {
181 refresh_id
= window
.setTimeout(do_refresh
, window
['opts_refresh'].value
* 1000);
184 function update_refresh_timer() {
185 if (!isNaN(refresh_id
))
186 window
.clearTimeout(refresh_id
);
187 var rate
= window
['opts_refresh'].value
* 1000;
189 refresh_id
= window
.setTimeout(do_refresh
, rate
);
192 function update_local_settings() {
194 setting
[this.id
] = this.checked
;
195 chrome
.storage
.local
.set(setting
);
198 function update_sync_settings() {
200 setting
[this.id
] = this.value
;
201 var storage
= sync_storage(window
['opts_sync'].checked
);
202 storage
.set(setting
);
206 update_refresh_timer();
211 function init_ui(local_keys
, sync_keys
, options
) {
217 'controls', 'metadata', 'playlist', 'options',
218 ].forEach(function(id
) {
219 var ele
= document
.getElementById('tab.' + id
);
220 ele
.onclick
= show_page
;
221 ele
.title
= id
+ ' [' + i
+ ']';
225 /* Setup control tab */
226 ui_mpc_status
= document
.getElementById('status');
228 'consume', 'next', 'pause', 'play', 'previous', 'random', 'repeat',
229 'seekcur', 'setvol', 'single', 'stop',
230 ].forEach(function(id
) {
231 var ele
= window
['ui_mpc_' + id
] = document
.getElementById(id
);
232 ele
.onchange
= ele
.onclick
= window
['tramp_mpc_' + id
];
235 ele
.title
+= ' [' + ele
.accessKey
+ ']'
237 window
['ui_mpc_currtime'] = document
.getElementById('currtime');
239 /* Setup metadata tab */
241 'album', 'artist', 'date', 'file', 'title',
242 ].forEach(function(id
) {
243 window
['ui_mpc_metadata_' + id
] = document
.getElementById('metadata.' + id
);
246 /* Setup playlist tab */
247 window
['ui_mpc_playlist'] = document
.getElementById('playlist');
249 /* Setup options tab */
250 document
.getElementById('connect').onclick
= mpc_connect
;
251 local_keys
.forEach(function(id
) {
252 var ele
= window
['opts_' + id
] = document
.getElementById(id
);
253 ele
.checked
= options
[id
];
254 ele
.onchange
= update_local_settings
;
256 sync_keys
.forEach(function(id
) {
257 var ele
= window
['opts_' + id
] = document
.getElementById(id
);
258 ele
.value
= options
[id
];
259 ele
.oninput
= update_sync_settings
;
263 function pretty_time(time
) {
264 var sec
, min
, hrs
, ret
= '';
265 time
= parseInt(time
);
267 min
= parseInt((time
/ 60) % 60);
268 hrs
= parseInt((time
/ 3600) % 3600);
270 ret
= hrs
+ ':' + ("00" + min
).substr(-2) + ':';
273 return ret
+ ("00" + sec
).substr(-2);
276 function playlist_del() {
277 mpc
.deleteid(this.title
);
278 this.parentNode
.remove();
281 function update_ui(state
, cmd
) {
282 if (typeof(state
) == 'string') {
283 ui_mpc_status
.innerText
= ({
284 'disconnect': 'Disconnecting...',
285 'init': 'Connecting...',
290 if (Array
.isArray(state
)) {
303 /* Update the metadata tab. */
304 var currentsong
= {};
305 if ('Currentsong' in state
)
306 currentsong
= state
.Currentsong
;
307 ui_mpc_metadata_album
.innerText
= currentsong
.Album
;
308 ui_mpc_metadata_artist
.innerText
= currentsong
.Artist
;
309 ui_mpc_metadata_title
.innerText
= currentsong
.Title
;
310 ui_mpc_metadata_date
.innerText
= currentsong
.Date
;
311 ui_mpc_metadata_file
.innerText
= currentsong
.file
;
313 /* Update the playlist tab. */
315 if ('Playlist' in state
)
316 playlist
= state
.Playlist
;
317 ui_mpc_playlist
.innerHTML
= '';
318 playlist
.forEach(function(song
) {
319 var cell
, row
= ui_mpc_playlist
.insertRow(-1);
320 if (song
.Pos
== currentsong
.Pos
)
321 row
.style
.fontWeight
= 'bold';
323 cell
= row
.insertCell(-1);
324 cell
.id
= 'playlist_del';
325 cell
.innerHTML
= '¤';
326 cell
.title
= song
.Id
;
327 cell
.onclick
= playlist_del
;
329 cell
= row
.insertCell(-1);
330 cell
.innerText
= song
.Pos
;
331 cell
.style
.textAlign
= 'right';
333 if ('Artist' in song
) {
334 row
.insertCell(-1).innerText
= song
.Artist
;
335 row
.insertCell(-1).innerText
= song
.Album
;
336 row
.insertCell(-1).innerText
= song
.Title
;
338 cell
= row
.insertCell(-1);
339 cell
.innerText
= song
.file
;
342 row
.insertCell(-1).innerText
= pretty_time(song
.Time
);
345 /* Update the status tab. */
348 // When stopped, there is no time field at all.
349 time
= state
.time
.split(':');
352 ui_mpc_seekcur
.max
= time
[1];
353 ui_mpc_seekcur
.value
= time
[0];
354 percent
= Math
.floor((0.0 + time
[0]) * 100 / (0.0 + time
[1]));
355 ui_mpc_seekcur
.title
= 'seekcur (' + percent
+ '%)';
356 ui_mpc_currtime
.innerText
= [pretty_time(time
[0]), pretty_time(time
[1]), percent
+ '%'].join(' / ');
358 ui_mpc_setvol
.value
= state
.volume
;
359 ui_mpc_setvol
.title
= 'setvol (' + state
.volume
+ '%)';
362 'consume', 'random', 'repeat', 'single',
363 ].forEach(function(id
) {
364 setToggleButton(window
['ui_mpc_' + id
], szo(state
[id
]));
367 ui_mpc_status
.innerText
= ({