-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathdispatch.html
56 lines (52 loc) · 1.57 KB
/
dispatch.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<!--
Copyright (c) 2011 The Chromium Authors. All rights reserved. Use of this
source code is governed by a BSD-style license that can be found in the
LICENSE file.
Dispatcher connects key logging in key.js to music player control in t61.js
key.js sends request.command
t61.js sends request.register to register tabID
-->
<script src="jquery.js"></script>
<script>
var SITE_URL = 'http://www.thesixtyone.com';
// Open channel for music player control
var port = null;
chrome.extension.onRequest.addListener(
function(request, sender, sendResponse) {
switch(request.type) {
case 'command':
console.log('dispatch command: ' + request.command);
try {
port.postMessage(request.command);
} catch(error) {
console.log('error: no music tab open');
// If preference is set, launch music tab here
if (localStorage['autoload']) {
chrome.tabs.create({url: SITE_URL});
}
}
sendResponse({});
break;
case 'register':
console.log('dispatch got register');
chrome.tabs.getSelected(null, function(tab) {
port = chrome.tabs.connect(tab.id);
sendResponse({});
});
break;
case 'key':
console.log('dispatch got keycode request');
sendResponse(getCommandShortcuts());
break;
}
}
);
function getCommandShortcuts(command) {
var keyIds = ['previous', 'playpause', 'next'];
var out = {};
$.each(keyIds, function(index, item) {
out[item] = JSON.parse(localStorage[item]);
});
return out;
}
</script>