/
background.js
52 lines (42 loc) · 1.31 KB
/
background.js
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
const port = browser.runtime.connectNative("alfredtabswitch");
function sendTabList({ requestId, port }) {
chrome.tabs.query({}, tabs => {
const relevantData = tabs.map(e => {
return {
id: e.index,
windowId: e.windowId,
url: e.url,
title: e.title,
};
});
port.postMessage({ requestId, data: relevantData });
});
}
function switchToTab({ port, requestId, windowId, tabId }) {
chrome.tabs.highlight({
windowId,
tabs: [ tabId ]
});
port.postMessage({ requestId, data: null });
}
function dispatcher({ cmd, requestId, port }) {
switch (cmd) {
case 'getTabList':
sendTabList({ requestId, port });
break;
default:
if (cmd.indexOf('switchTo') === 0) {
const args = cmd.split(':');
const tabId = Number(args[1]);
const windowId = Number(args[2]);
switchToTab({ port, requestId, windowId, tabId });
} else {
throw new Error(`Unknown command: ${msg.cmd}`);
}
}
return;
}
port.onMessage.addListener(response => {
// console.log(`Got message from NM: ${JSON.stringify(response)}`);
dispatcher({ ...response, port });
});