@webextkits/messages-center 0.1.0 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/background.js +31 -31
- package/package.json +1 -1
package/dist/background.js
CHANGED
|
@@ -1,56 +1,56 @@
|
|
|
1
1
|
var l = Object.defineProperty;
|
|
2
|
-
var b = (
|
|
3
|
-
var
|
|
2
|
+
var b = (n, e, t) => e in n ? l(n, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : n[e] = t;
|
|
3
|
+
var a = (n, e, t) => (b(n, typeof e != "symbol" ? e + "" : e, t), t);
|
|
4
4
|
import { tabs as d, runtime as h } from "webextension-polyfill";
|
|
5
5
|
class u {
|
|
6
|
-
constructor(
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
this.slug =
|
|
6
|
+
constructor(e, t) {
|
|
7
|
+
a(this, "listeners", {});
|
|
8
|
+
a(this, "tabs", []);
|
|
9
|
+
a(this, "slug");
|
|
10
|
+
a(this, "isDebugger");
|
|
11
|
+
this.slug = e, this.isDebugger = !!t, this.bindMessage(), this.bindEvent();
|
|
12
12
|
}
|
|
13
|
-
on(
|
|
14
|
-
if (this.listeners[
|
|
15
|
-
throw new Error(`event listener ${String(
|
|
16
|
-
return this.listeners[
|
|
17
|
-
delete this.listeners[
|
|
13
|
+
on(e, t) {
|
|
14
|
+
if (this.listeners[e])
|
|
15
|
+
throw new Error(`event listener ${String(e)} already exist!`);
|
|
16
|
+
return this.listeners[e] = t, () => {
|
|
17
|
+
delete this.listeners[e];
|
|
18
18
|
};
|
|
19
19
|
}
|
|
20
|
-
async send(
|
|
21
|
-
this.isDebugger && console.log("发送消息", { action:
|
|
20
|
+
async send(e, ...t) {
|
|
21
|
+
this.isDebugger && console.log("发送消息", { action: e, payload: t, tabs: this.tabs });
|
|
22
22
|
for (let s of this.tabs)
|
|
23
23
|
try {
|
|
24
24
|
const i = await d.get(s);
|
|
25
|
-
i && i.id ? d.sendMessage(i.id, { from: this.slug, payload: { action:
|
|
25
|
+
i && i.id ? d.sendMessage(i.id, { from: this.slug, payload: { action: e, payload: t } }) : this.deleteTabById(s);
|
|
26
26
|
} catch (i) {
|
|
27
27
|
console.log(i), this.deleteTabById(s);
|
|
28
28
|
}
|
|
29
29
|
}
|
|
30
30
|
bindMessage() {
|
|
31
|
-
h.onMessageExternal.addListener((
|
|
31
|
+
h.onMessageExternal.addListener((e, t, s) => {
|
|
32
32
|
var i, r;
|
|
33
|
-
if ((i =
|
|
34
|
-
if (this.isDebugger && console.log("收到消息", { request:
|
|
35
|
-
return this.tabs.includes(
|
|
36
|
-
if (
|
|
37
|
-
return this.deleteTabById(
|
|
38
|
-
if (this.listeners[
|
|
39
|
-
return (r =
|
|
40
|
-
this.isDebugger && console.log("send to callback from background", { request:
|
|
33
|
+
if ((i = t == null ? void 0 : t.tab) != null && i.id) {
|
|
34
|
+
if (this.isDebugger && console.log("收到消息", { request: e, sender: t }), e.action === "__connect")
|
|
35
|
+
return this.tabs.includes(t.tab.id) || this.tabs.push(t.tab.id), s();
|
|
36
|
+
if (e.action === "__disconnect")
|
|
37
|
+
return this.deleteTabById(t.tab.id), s();
|
|
38
|
+
if (this.listeners[e.action])
|
|
39
|
+
return (r = t == null ? void 0 : t.tab) != null && r.id ? (this.tabs.includes(t.tab.id) || this.tabs.push(t.tab.id), this.listeners[e.action].bind({ request: e, sender: t })(...e.payload).then((o) => {
|
|
40
|
+
this.isDebugger && console.log("send to callback from background", { request: e, sender: t, res: o }), s(o);
|
|
41
41
|
}), !0) : s();
|
|
42
42
|
}
|
|
43
43
|
});
|
|
44
44
|
}
|
|
45
45
|
bindEvent() {
|
|
46
|
-
d.onRemoved.addListener((
|
|
47
|
-
this.deleteTabById(
|
|
46
|
+
d.onRemoved.addListener((e) => {
|
|
47
|
+
this.deleteTabById(e);
|
|
48
48
|
});
|
|
49
49
|
}
|
|
50
|
-
deleteTabById(
|
|
51
|
-
if (this.tabs.includes(
|
|
52
|
-
const
|
|
53
|
-
this.tabs.splice(
|
|
50
|
+
deleteTabById(e) {
|
|
51
|
+
if (this.tabs.includes(e)) {
|
|
52
|
+
const t = this.tabs.indexOf(e);
|
|
53
|
+
this.tabs.splice(t, 1);
|
|
54
54
|
}
|
|
55
55
|
}
|
|
56
56
|
}
|