]>
git.wh0rd.org - chrome-ext/music-player-client.git/blob - main.js
b83e0a219739a85ff931f8129635815c3eb3b533
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 function mpc_refresh() {
64 function mpc_connect(host
, port
) {
65 if (typeof(host
) != 'string') {
66 host
= window
['opts_host'].value
;
67 port
= parseInt(window
['opts_port'].value
);
70 if (mpc
!= undefined) {
71 console
.log('disconnecting');
72 update_ui('disconnect');
74 tcpclient
.disconnect();
79 tcpclient
= new TcpClient(host
, port
);
80 tcpclient
.connect(function() {
81 var mpc_sender
= new TcpClientSender(tcpclient
);
82 tcpclient
.addResponseListener(tramp_mpc_recv
);
83 mpc
= new Mpc(mpc_sender
, update_ui
);
84 console
.log('connected to ' + host
+ ':' + port
);
85 console
.log('protip: use the "mpc" object to poke mpd directly.\n' +
86 'you can also do mpc.set_debug(3) to see traffic');
88 update_refresh_timer();
92 function tramp_mpc_consume() {
93 var val
= zo(!getToggleButton(this));
95 setToggleButton(this, val
);
97 function tramp_mpc_next() { mpc
.next(); }
98 function tramp_mpc_pause() { mpc
.pause(); }
99 function tramp_mpc_play() { mpc
.play(); }
100 function tramp_mpc_previous() { mpc
.previous(); }
101 function tramp_mpc_random() {
102 var val
= zo(!getToggleButton(this));
104 setToggleButton(this, val
);
106 function tramp_mpc_repeat() {
107 var val
= zo(!getToggleButton(this));
109 setToggleButton(this, val
);
111 function tramp_mpc_seekcur() { mpc
.seekcur(this.value
); }
112 function tramp_mpc_setvol() { mpc
.setvol(this.value
); }
113 function tramp_mpc_single() {
114 var val
= zo(!getToggleButton(this));
116 setToggleButton(this, val
);
118 function tramp_mpc_stop() { mpc
.stop(); }
124 return val
== '0' ? 0 : 1;
126 function getToggleButton(btn
) {
127 return btn
.style
.borderStyle
== 'inset';
129 function setToggleButton(btn
, val
) {
130 if (val
=== undefined)
131 val
= !getToggleButton(btn
);
132 btn
.style
.borderStyle
= val
? 'inset' : '';
135 function show_page(page
) {
136 if (typeof(page
) != 'string')
137 page
= this.id
.split('.')[1];
139 var eles
= document
.getElementsByClassName('main');
140 for (var i
= 0; i
< eles
.length
; ++i
) {
144 if (ele
.id
== 'main.' + page
) {
148 ele
.style
.display
= dis
;
149 document
.getElementById('tab.' + ele
.id
.split('.')[1]).className
= cls
;
153 function do_refresh() {
155 refresh_id
= window
.setTimeout(do_refresh
, window
['opts_refresh'].value
* 1000);
158 function update_refresh_timer() {
159 if (!isNaN(refresh_id
))
160 window
.clearTimeout(refresh_id
);
161 var rate
= window
['opts_refresh'].value
* 1000;
163 refresh_id
= window
.setTimeout(do_refresh
, rate
);
166 function update_local_settings() {
168 setting
[this.id
] = this.checked
;
169 chrome
.storage
.local
.set(setting
);
172 function update_sync_settings() {
174 setting
[this.id
] = this.value
;
175 var storage
= sync_storage(window
['opts_sync'].checked
);
176 storage
.set(setting
);
180 update_refresh_timer();
185 function init_ui(local_keys
, sync_keys
, options
) {
188 'controls', 'metadata', 'options',
189 ].forEach(function(id
) {
190 document
.getElementById('tab.' + id
).onclick
= show_page
;
193 /* Setup control tab */
194 ui_mpc_status
= document
.getElementById('status');
195 ui_mpc_metadata
= document
.getElementById('metadata');
197 'consume', 'next', 'pause', 'play', 'previous', 'random', 'repeat',
198 'seekcur', 'setvol', 'single', 'stop',
199 ].forEach(function(id
) {
200 var ele
= window
['ui_mpc_' + id
] = document
.getElementById(id
);
201 ele
.onchange
= ele
.onclick
= window
['tramp_mpc_' + id
];
205 /* Setup options tab */
206 document
.getElementById('connect').onclick
= mpc_connect
;
207 local_keys
.forEach(function(id
) {
208 var ele
= window
['opts_' + id
] = document
.getElementById(id
);
209 ele
.checked
= options
[id
];
210 ele
.onchange
= update_local_settings
;
212 sync_keys
.forEach(function(id
) {
213 var ele
= window
['opts_' + id
] = document
.getElementById(id
);
214 ele
.value
= options
[id
];
215 ele
.oninput
= update_sync_settings
;
219 function update_ui(state
, cmd
) {
220 if (typeof(state
) == 'string') {
221 ui_mpc_status
.innerText
= ({
222 'disconnect': 'Disconnecting...',
223 'init': 'Connecting...',
228 if (Array
.isArray(state
)) {
241 if ('file' in state
) {
242 // Hack: should be a real object.
243 ui_mpc_metadata
.innerText
= state
.file
;
248 // When stopped, there is no time field at all.
249 time
= state
.time
.split(':');
252 window
.ui_mpc_seekcur
.max
= time
[1];
253 window
.ui_mpc_seekcur
.value
= time
[0];
254 percent
= Math
.floor((0.0 + time
[0]) * 100 / (0.0 + time
[1]));
255 window
.ui_mpc_seekcur
.title
= 'seekcur (' + percent
+ '%)';
257 window
.ui_mpc_setvol
.value
= state
.volume
;
258 window
.ui_mpc_setvol
.title
= 'setvol (' + state
.volume
+ '%)';
261 'consume', 'random', 'repeat', 'single',
262 ].forEach(function(id
) {
263 setToggleButton(window
['ui_mpc_' + id
], szo(state
[id
]));
266 ui_mpc_status
.innerText
= ({