-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
56 lines (53 loc) · 1.55 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
53
54
55
56
var port = browser.runtime.connectNative("pprefox_rs");
var themes = [];
browser.management.getAll().then((extensions) => {
for (let extension of extensions) {
if (extension.type !== 'theme') {
continue;
}
themes.push({ "name": extension.name, "id": extension.id });
}
});
function tabs_filter(tabs) {
// filter out extra info we don't need, like cookie info
return tabs.map(tab => {
return Object.keys(tab)
.filter(key => ["active", "id", "index", "pinned", "title", "url", "windowId"].includes(key))
.reduce((acc, key) => {
acc[key] = tab[key];
return acc;
}, {});
});
}
port.onMessage.addListener((msg) => {
if (msg['command'] === "list_themes") {
if (msg.hasOwnProperty("uuid")) {
var outgoing = { "uuid": msg['uuid'], "themes": themes };
port.postMessage(outgoing);
}
}
if (msg['command'] === "set_theme") {
if (msg.hasOwnProperty("theme_id")) {
browser.management.setEnabled(msg['theme_id'], true);
}
}
if (msg['command'] === "list_tabs") {
if (msg.hasOwnProperty("uuid")) {
var query = { "windowType": "normal" };
if (msg.hasOwnProperty("query")) {
query = msg['query'];
}
browser.tabs.query(query).then((tabs) => {
var outgoing = {
"uuid": msg['uuid'], "success": true, "tabs": tabs_filter(tabs)
};
port.postMessage(outgoing);
});
}
}
if (msg['command'] === "select_tab") {
if (msg.hasOwnProperty("index")) {
browser.tabs.update(msg['index'], {"active": true});
}
}
});