-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
148 lines (136 loc) · 3.75 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
var port = browser.runtime.connectNative("boxes_ext_native_shell");
var self = undefined;
var connected = false;
var cb = 0;
function makeSelfActive() {
browser.windows.getAll().then(windows => {
browser.windows.update(windows[0].id, {
focused: true
});
});
}
port.onMessage.addListener(res => {
if (!connected) {
self = res.msg;
makeSelfActive();
}
if (connected && cb !== 0) {
cb(res.msg);
// `getBoxAttrs` depends on this function, and we use it later, so to
// avoid an infinite recursion loop, we return out here.
return;
}
connected = true;
// This goes after everything because we don't want to have an infinite loop
if (self !== "<nobody>") {
getBoxAttrs(self).then(attrs => {
browser.browserAction.setIcon({
path: "popup/icons/" + attrs.icon
});
});
}
});
async function getBoxes() {
return new Promise((resolve, reject) => {
cb = reply => {
resolve(reply.split("\n").slice(0, -1).map(v => {
let [id, icon, name, exec] = v.split("|");
return {
id, icon, name, exec
};
}));
};
port.postMessage("box:ls");
});
}
async function getInstallations() {
return new Promise((resolve, reject) => {
cb = reply => {
resolve(reply.split("\n"));
};
port.postMessage("i:ls");
});
}
async function startBox(id) {
return new Promise((resolve, reject) => {
cb = resolve;
port.postMessage("exec " + id);
});
}
async function newBox(icon, name, exec) {
return new Promise((resolve, reject) => {
cb = reply => {
resolve(reply);
};
port.postMessage("box:new " + icon + "|" + name + "|" + exec);
});
}
async function getBoxAttrs(id) {
return new Promise((resolve, reject) => {
cb = reply => {
let [icon, name, exec] = reply.split("|");
resolve({
icon, name, exec
});
};
port.postMessage("box:attrs get " + id);
});
}
async function setBoxAttrs(id, icon, name, exec) {
return new Promise((resolve, reject) => {
cb = reply => {
resolve(reply);
};
port.postMessage("box:attrs set " + [id, icon, name, exec].join("|"));
});
}
async function delBox(id) {
return new Promise((resolve, reject) => {
cb = reply => {
resolve(reply);
};
port.postMessage("box:del " + id);
});
}
async function getDefault() {
return new Promise((resolve, reject) => {
cb = reply => {
resolve(reply);
};
port.postMessage("default:get");
});
}
async function setDefault(data) {
return new Promise((resolve, reject) => {
cb = reply => {
resolve(reply);
};
port.postMessage("default:set " + data);
});
}
browser.runtime.onMessage.addListener(msg => {
switch (msg.type) {
case "i:ls":
return getInstallations();
case "default:get":
return getDefault();
case "default:set":
return setDefault(...msg.args);
case "box:ls":
return getBoxes();
case "box:new":
return newBox(...msg.args);
case "box:attrs get":
return getBoxAttrs(...msg.args);
case "box:attrs set":
return setBoxAttrs(...msg.args);
case "box:del":
return delBox(...msg.args);
case "exec":
return startBox(...msg.args);
case "whoami":
return Promise.resolve(self);
default:
return new Error("no such type exists");
}
});