]>
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
);
15 function tramp_mpc_recv(data
) {
19 function sync_storage(sync
) {
20 return sync
? chrome
.storage
.sync
: chrome
.storage
.local
;
23 window
.onload = function() {
28 'host', 'port', 'refresh',
31 'host': '192.168.0.2',
37 chrome
.storage
.local
.get(local_keys
, function(settings
) {
38 local_keys
.forEach(function(key
) {
40 options
[key
] = settings
[key
]
43 var storage
= sync_storage(options
['sync']);
44 storage
.get(sync_keys
, function(settings
) {
45 sync_keys
.forEach(function(key
) {
47 options
[key
] = settings
[key
];
50 init_ui(local_keys
, sync_keys
, options
);
56 function mpc_refresh() {
61 function mpc_connect(host
, port
) {
62 if (typeof(host
) != 'string') {
63 host
= window
['opts_host'].value
;
64 port
= parseInt(window
['opts_port'].value
);
67 if (mpc
!= undefined) {
68 console
.log('disconnecting');
69 update_ui('disconnect');
71 tcpclient
.disconnect();
76 tcpclient
= new TcpClient(host
, port
);
77 tcpclient
.connect(function() {
78 var mpc_sender
= new TcpClientSender(tcpclient
);
79 tcpclient
.addResponseListener(tramp_mpc_recv
);
80 mpc
= new Mpc(mpc_sender
, update_ui
);
81 console
.log('connected to ' + host
+ ':' + port
);
83 update_refresh_timer();
87 function tramp_mpc_consume() {
88 var val
= zo(!getToggleButton(this));
90 setToggleButton(this, val
);
92 function tramp_mpc_next() { mpc
.next(); }
93 function tramp_mpc_pause() { mpc
.pause(); }
94 function tramp_mpc_play() { mpc
.play(); }
95 function tramp_mpc_previous() { mpc
.previous(); }
96 function tramp_mpc_random() {
97 var val
= zo(!getToggleButton(this));
99 setToggleButton(this, val
);
101 function tramp_mpc_repeat() {
102 var val
= zo(!getToggleButton(this));
104 setToggleButton(this, val
);
106 function tramp_mpc_seekcur() { mpc
.seekcur(this.value
); }
107 function tramp_mpc_setvol() { mpc
.setvol(this.value
); }
108 function tramp_mpc_single() {
109 var val
= zo(!getToggleButton(this));
111 setToggleButton(this, val
);
113 function tramp_mpc_stop() { mpc
.stop(); }
119 return val
== '0' ? 0 : 1;
121 function getToggleButton(btn
) {
122 return btn
.style
.borderStyle
== 'inset';
124 function setToggleButton(btn
, val
) {
125 if (val
=== undefined)
126 val
= !getToggleButton(btn
);
127 btn
.style
.borderStyle
= val
? 'inset' : '';
130 function show_page(page
) {
131 if (typeof(page
) != 'string')
132 page
= this.id
.split('.')[1];
134 var eles
= document
.getElementsByClassName('main');
135 for (var i
= 0; i
< eles
.length
; ++i
) {
139 if (ele
.id
== 'main.' + page
) {
143 ele
.style
.display
= dis
;
144 document
.getElementById('tab.' + ele
.id
.split('.')[1]).className
= cls
;
148 function do_refresh() {
150 refresh_id
= window
.setTimeout(do_refresh
, window
['opts_refresh'].value
* 1000);
153 function update_refresh_timer() {
154 if (refresh_id
!= NaN
)
155 window
.clearTimeout(refresh_id
);
156 var rate
= window
['opts_refresh'].value
* 1000;
158 refresh_id
= window
.setTimeout(do_refresh
, rate
);
161 function update_local_settings() {
163 setting
[this.id
] = this.checked
;
164 chrome
.storage
.local
.set(setting
);
167 function update_sync_settings() {
169 setting
[this.id
] = this.value
;
170 var storage
= sync_storage(window
['opts_sync'].checked
);
171 storage
.set(setting
);
175 update_refresh_timer();
180 function init_ui(local_keys
, sync_keys
, options
) {
183 'controls', 'metadata', 'options',
184 ].forEach(function(id
) {
185 document
.getElementById('tab.' + id
).onclick
= show_page
;
188 /* Setup control tab */
189 ui_mpc_status
= document
.getElementById('status');
190 ui_mpc_metadata
= document
.getElementById('metadata');
192 'consume', 'next', 'pause', 'play', 'previous', 'random', 'repeat',
193 'seekcur', 'setvol', 'single', 'stop',
194 ].forEach(function(id
) {
195 var ele
= window
['ui_mpc_' + id
] = document
.getElementById(id
);
196 ele
.onclick
= window
['tramp_mpc_' + id
];
200 /* Setup options tab */
201 document
.getElementById('connect').onclick
= mpc_connect
;
202 local_keys
.forEach(function(id
) {
203 var ele
= window
['opts_' + id
] = document
.getElementById(id
);
204 ele
.checked
= options
[id
];
205 ele
.onchange
= update_local_settings
;
207 sync_keys
.forEach(function(id
) {
208 var ele
= window
['opts_' + id
] = document
.getElementById(id
);
209 ele
.value
= options
[id
];
210 ele
.oninput
= update_sync_settings
;
214 function update_ui(state
, cmd
) {
215 if (typeof(state
) == 'string') {
216 ui_mpc_status
.innerText
= ({
217 'disconnect': 'Disconnecting...',
218 'init': 'Connecting...',
223 if (Array
.isArray(state
)) {
236 if ('file' in state
) {
237 // Hack: should be a real object.
238 ui_mpc_metadata
.innerText
= state
['file'];
244 // When stopped, there is no time field at all.
245 time
= state
.time
.split(':');
248 window
['ui_mpc_seekcur'].max
= time
[1];
249 window
['ui_mpc_seekcur'].value
= time
[0];
251 window
['ui_mpc_setvol'].value
= state
.volume
;
253 'consume', 'random', 'repeat', 'single',
254 ].forEach(function(id
) {
255 setToggleButton(window
['ui_mpc_' + id
], szo(state
[id
]));
258 ui_mpc_status
.innerText
= ({