]>
git.wh0rd.org - chrome-ext/music-player-client.git/blob - main.js
aa4c7bd961f329b5506724635ae7e4c7b6b72908
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_next() { mpc
.next(); }
114 function tramp_mpc_pause() { mpc
.pause(); }
115 function tramp_mpc_play() { mpc
.play(); }
116 function tramp_mpc_previous() { mpc
.previous(); }
117 function tramp_mpc_random() {
118 var val
= zo(!getToggleButton(this));
120 setToggleButton(this, val
);
122 function tramp_mpc_repeat() {
123 var val
= zo(!getToggleButton(this));
125 setToggleButton(this, val
);
127 function tramp_mpc_seekcur() { mpc
.seekcur(this.value
); }
128 function tramp_mpc_setvol() { mpc
.setvol(this.value
); }
129 function tramp_mpc_single() {
130 var val
= zo(!getToggleButton(this));
132 setToggleButton(this, val
);
134 function tramp_mpc_stop() { mpc
.stop(); }
140 return val
== '0' ? 0 : 1;
142 function getToggleButton(btn
) {
143 return btn
.style
.borderStyle
== 'inset';
145 function setToggleButton(btn
, val
) {
146 if (val
=== undefined)
147 val
= !getToggleButton(btn
);
148 btn
.style
.borderStyle
= val
? 'inset' : '';
151 function show_page(page
) {
152 if (typeof(page
) != 'string')
153 page
= this.id
.split('.')[1];
164 var eles
= document
.getElementsByClassName('main');
165 for (var i
= 0; i
< eles
.length
; ++i
) {
169 if (ele
.id
== 'main.' + page
) {
173 ele
.style
.display
= dis
;
174 document
.getElementById('tab.' + ele
.id
.split('.')[1]).className
= cls
;
178 function do_refresh() {
180 refresh_id
= window
.setTimeout(do_refresh
, window
['opts_refresh'].value
* 1000);
183 function update_refresh_timer() {
184 if (!isNaN(refresh_id
))
185 window
.clearTimeout(refresh_id
);
186 var rate
= window
['opts_refresh'].value
* 1000;
188 refresh_id
= window
.setTimeout(do_refresh
, rate
);
191 function update_local_settings() {
193 setting
[this.id
] = this.checked
;
194 chrome
.storage
.local
.set(setting
);
197 function update_sync_settings() {
199 setting
[this.id
] = this.value
;
200 var storage
= sync_storage(window
['opts_sync'].checked
);
201 storage
.set(setting
);
205 update_refresh_timer();
210 function init_ui(local_keys
, sync_keys
, options
) {
216 'controls', 'metadata', 'playlist', 'options',
217 ].forEach(function(id
) {
218 var ele
= document
.getElementById('tab.' + id
);
219 ele
.onclick
= show_page
;
220 ele
.title
= id
+ ' [' + i
+ ']';
224 /* Setup control tab */
225 ui_mpc_status
= document
.getElementById('status');
227 'consume', 'next', 'pause', 'play', 'previous', 'random', 'repeat',
228 'seekcur', 'setvol', 'single', 'stop',
229 ].forEach(function(id
) {
230 var ele
= window
['ui_mpc_' + id
] = document
.getElementById(id
);
231 ele
.onchange
= ele
.onclick
= window
['tramp_mpc_' + id
];
234 ele
.title
+= ' [' + ele
.accessKey
+ ']'
236 window
['ui_mpc_currtime'] = document
.getElementById('currtime');
238 /* Setup metadata tab */
240 'album', 'artist', 'date', 'file', 'title',
241 ].forEach(function(id
) {
242 window
['ui_mpc_metadata_' + id
] = document
.getElementById('metadata.' + id
);
245 /* Setup playlist tab */
246 window
['ui_mpc_playlist'] = document
.getElementById('playlist');
248 /* Setup options tab */
249 document
.getElementById('connect').onclick
= mpc_connect
;
250 local_keys
.forEach(function(id
) {
251 var ele
= window
['opts_' + id
] = document
.getElementById(id
);
252 ele
.checked
= options
[id
];
253 ele
.onchange
= update_local_settings
;
255 sync_keys
.forEach(function(id
) {
256 var ele
= window
['opts_' + id
] = document
.getElementById(id
);
257 ele
.value
= options
[id
];
258 ele
.oninput
= update_sync_settings
;
262 function pretty_time(time
) {
263 var sec
, min
, hrs
, ret
= '';
264 time
= parseInt(time
);
266 min
= parseInt((time
/ 60) % 60);
267 hrs
= parseInt((time
/ 3600) % 3600);
269 ret
= hrs
+ ':' + ("00" + min
).substr(-2) + ':';
272 return ret
+ ("00" + sec
).substr(-2);
275 function update_ui(state
, cmd
) {
276 if (typeof(state
) == 'string') {
277 ui_mpc_status
.innerText
= ({
278 'disconnect': 'Disconnecting...',
279 'init': 'Connecting...',
284 if (Array
.isArray(state
)) {
297 /* Update the metadata tab. */
298 var currentsong
= {};
299 if ('Currentsong' in state
)
300 currentsong
= state
.Currentsong
;
301 ui_mpc_metadata_album
.innerText
= currentsong
.Album
;
302 ui_mpc_metadata_artist
.innerText
= currentsong
.Artist
;
303 ui_mpc_metadata_title
.innerText
= currentsong
.Title
;
304 ui_mpc_metadata_date
.innerText
= currentsong
.Date
;
305 ui_mpc_metadata_file
.innerText
= currentsong
.file
;
307 /* Update the playlist tab. */
309 if ('Playlist' in state
)
310 playlist
= state
.Playlist
;
311 ui_mpc_playlist
.innerHTML
= '';
312 playlist
.forEach(function(song
) {
313 var cell
, row
= ui_mpc_playlist
.insertRow(-1);
314 if (song
.Pos
== currentsong
.Pos
)
315 row
.style
.fontWeight
= 'bold';
317 cell
= row
.insertCell(-1);
318 cell
.innerText
= song
.Pos
;
319 cell
.style
.textAlign
= 'right';
321 if ('Artist' in song
) {
322 row
.insertCell(-1).innerText
= song
.Artist
;
323 row
.insertCell(-1).innerText
= song
.Album
;
324 row
.insertCell(-1).innerText
= song
.Title
;
326 cell
= row
.insertCell(-1);
327 cell
.innerText
= song
.file
;
330 row
.insertCell(-1).innerText
= pretty_time(song
.Time
);
333 /* Update the status tab. */
336 // When stopped, there is no time field at all.
337 time
= state
.time
.split(':');
340 ui_mpc_seekcur
.max
= time
[1];
341 ui_mpc_seekcur
.value
= time
[0];
342 percent
= Math
.floor((0.0 + time
[0]) * 100 / (0.0 + time
[1]));
343 ui_mpc_seekcur
.title
= 'seekcur (' + percent
+ '%)';
344 ui_mpc_currtime
.innerText
= [pretty_time(time
[0]), pretty_time(time
[1]), percent
+ '%'].join(' / ');
346 ui_mpc_setvol
.value
= state
.volume
;
347 ui_mpc_setvol
.title
= 'setvol (' + state
.volume
+ '%)';
350 'consume', 'random', 'repeat', 'single',
351 ].forEach(function(id
) {
352 setToggleButton(window
['ui_mpc_' + id
], szo(state
[id
]));
355 ui_mpc_status
.innerText
= ({