@webextkits/messages-center 0.2.1 → 0.2.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 +35 -33
- package/dist/inject.js +18 -18
- package/package.json +1 -1
package/dist/background.js
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
var g = Object.defineProperty;
|
|
2
|
-
var u = (
|
|
3
|
-
var
|
|
2
|
+
var u = (r, s, t) => s in r ? g(r, s, { enumerable: !0, configurable: !0, writable: !0, value: t }) : r[s] = t;
|
|
3
|
+
var c = (r, s, t) => u(r, typeof s != "symbol" ? s + "" : s, t);
|
|
4
4
|
class p {
|
|
5
5
|
constructor(s, t) {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
6
|
+
c(this, "listeners", {});
|
|
7
|
+
c(this, "tabs", []);
|
|
8
|
+
c(this, "slug");
|
|
9
|
+
c(this, "isDebugger");
|
|
10
|
+
c(this, "callbackMap", /* @__PURE__ */ new Map());
|
|
11
11
|
this.slug = s, this.isDebugger = !!t, this.bindMessage(), this.bindEvent();
|
|
12
12
|
}
|
|
13
13
|
on(s, t) {
|
|
@@ -19,47 +19,49 @@ class p {
|
|
|
19
19
|
async send(s, ...t) {
|
|
20
20
|
this.isDebugger && console.log("发送消息", { action: s, payload: t, tabs: this.tabs });
|
|
21
21
|
for (let e of this.tabs) try {
|
|
22
|
-
const
|
|
23
|
-
|
|
24
|
-
} catch (
|
|
25
|
-
console.log(
|
|
22
|
+
const i = await chrome.tabs.get(e);
|
|
23
|
+
i && i.id ? chrome.tabs.sendMessage(i.id, { from: this.slug, payload: { action: s, payload: t } }) : this.deleteTabById(e);
|
|
24
|
+
} catch (i) {
|
|
25
|
+
console.log(i), this.deleteTabById(e);
|
|
26
26
|
}
|
|
27
27
|
}
|
|
28
28
|
async sendWithCallback(s, t, ...e) {
|
|
29
|
-
const
|
|
30
|
-
this.callbackMap.set(
|
|
31
|
-
for (let
|
|
32
|
-
const
|
|
33
|
-
|
|
34
|
-
} catch (
|
|
35
|
-
console.log(
|
|
29
|
+
const i = l();
|
|
30
|
+
this.callbackMap.set(i, t), this.isDebugger && console.log("发送消息", { action: s, payload: e, tabs: this.tabs });
|
|
31
|
+
for (let n of this.tabs) try {
|
|
32
|
+
const o = await chrome.tabs.get(n);
|
|
33
|
+
o && o.id ? chrome.tabs.sendMessage(o.id, { from: this.slug, payload: { action: s, payload: e }, callbackId: i }) : this.deleteTabById(n);
|
|
34
|
+
} catch (o) {
|
|
35
|
+
console.log(o), this.deleteTabById(n);
|
|
36
36
|
}
|
|
37
37
|
}
|
|
38
38
|
async sendByTabId(s, t, ...e) {
|
|
39
39
|
this.isDebugger && console.log("发送消息(指定 tabId)", { tabId: s, action: t, payload: e });
|
|
40
|
-
const
|
|
41
|
-
return new Promise((
|
|
42
|
-
this.callbackMap.set(
|
|
43
|
-
|
|
44
|
-
}), chrome.tabs.get(s).then((
|
|
45
|
-
|
|
46
|
-
}).catch((
|
|
47
|
-
console.log(
|
|
40
|
+
const i = l();
|
|
41
|
+
return new Promise((n, o) => {
|
|
42
|
+
this.callbackMap.set(i, function(a) {
|
|
43
|
+
n(a);
|
|
44
|
+
}), chrome.tabs.get(s).then((a) => {
|
|
45
|
+
a && a.id ? chrome.tabs.sendMessage(a.id, { from: this.slug, payload: { action: t, payload: e }, callbackId: i }) : this.deleteTabById(s);
|
|
46
|
+
}).catch((a) => {
|
|
47
|
+
console.log(a), this.deleteTabById(s);
|
|
48
48
|
});
|
|
49
49
|
});
|
|
50
50
|
}
|
|
51
51
|
bindMessage() {
|
|
52
52
|
chrome.runtime.onMessageExternal.addListener((s, t, e) => {
|
|
53
|
-
var
|
|
54
|
-
if ((
|
|
53
|
+
var i, n, o;
|
|
54
|
+
if ((i = t == null ? void 0 : t.tab) != null && i.id) {
|
|
55
55
|
if (this.isDebugger && console.log("收到消息", { request: s, sender: t }), s.action === "__connect") return this.tabs.includes(t.tab.id) || this.tabs.push(t.tab.id), e();
|
|
56
56
|
if (s.action === "__disconnect") return this.deleteTabById(t.tab.id), e();
|
|
57
57
|
if (s.action === "__callback") {
|
|
58
|
-
const { callbackId:
|
|
59
|
-
return h === "error" ? console.error(b) : this.callbackMap.has(
|
|
58
|
+
const { callbackId: a, result: d, errorMessage: b, status: h } = s.payload;
|
|
59
|
+
return h === "error" ? console.error(b) : this.callbackMap.has(a) && ((n = this.callbackMap.get(a)) == null || n(d, t)), e();
|
|
60
60
|
}
|
|
61
|
-
if (this.listeners[s.action]) return (
|
|
62
|
-
this.isDebugger && console.log("send to callback from background", { request: s, sender: t, res:
|
|
61
|
+
if (this.listeners[s.action]) return (o = t == null ? void 0 : t.tab) != null && o.id ? (this.tabs.includes(t.tab.id) || this.tabs.push(t.tab.id), this.listeners[s.action].bind({ request: s, sender: t })(...s.payload).then((a) => {
|
|
62
|
+
this.isDebugger && console.log("send to callback from background", { request: s, sender: t, res: a }), e({ status: "success", result: a });
|
|
63
|
+
}).catch((a) => {
|
|
64
|
+
console.error(a), e({ status: "error", errorMessage: a.toString() });
|
|
63
65
|
}), !0) : e();
|
|
64
66
|
}
|
|
65
67
|
});
|
|
@@ -76,7 +78,7 @@ class p {
|
|
|
76
78
|
}
|
|
77
79
|
}
|
|
78
80
|
}
|
|
79
|
-
function
|
|
81
|
+
function l() {
|
|
80
82
|
return (/* @__PURE__ */ new Date()).getTime().toString() + Math.random().toString(36).substring(2, 15);
|
|
81
83
|
}
|
|
82
84
|
export {
|
package/dist/inject.js
CHANGED
|
@@ -1,27 +1,27 @@
|
|
|
1
|
-
var
|
|
2
|
-
var
|
|
3
|
-
var
|
|
1
|
+
var c = Object.defineProperty;
|
|
2
|
+
var l = (n, e, s) => e in n ? c(n, e, { enumerable: !0, configurable: !0, writable: !0, value: s }) : n[e] = s;
|
|
3
|
+
var o = (n, e, s) => l(n, typeof e != "symbol" ? e + "" : e, s);
|
|
4
4
|
class h {
|
|
5
5
|
constructor(e, s) {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
6
|
+
o(this, "id");
|
|
7
|
+
o(this, "listeners", {});
|
|
8
|
+
o(this, "slug");
|
|
9
|
+
o(this, "isDebugger");
|
|
10
10
|
this.slug = e, this.isDebugger = !!s, this.id = window[e].id, this.bindEvent();
|
|
11
11
|
}
|
|
12
12
|
on(e, s) {
|
|
13
13
|
return this.listeners[e] = this.listeners[e] || [], this.listeners[e].push(s), () => {
|
|
14
14
|
if (this.listeners[e].includes(s)) {
|
|
15
|
-
const
|
|
16
|
-
this.listeners[e].splice(
|
|
15
|
+
const r = this.listeners[e].indexOf(s);
|
|
16
|
+
this.listeners[e].splice(r, 1);
|
|
17
17
|
}
|
|
18
18
|
};
|
|
19
19
|
}
|
|
20
20
|
send(e, ...s) {
|
|
21
|
-
return new Promise(async (
|
|
21
|
+
return new Promise(async (r, a) => {
|
|
22
22
|
let i = { action: e, payload: s };
|
|
23
|
-
this.isDebugger && console.log(
|
|
24
|
-
this.isDebugger && console.log("
|
|
23
|
+
this.isDebugger && console.log(`[${i.action}] - 发送`, i), chrome.runtime.sendMessage(this.id, i, {}, (t) => {
|
|
24
|
+
this.isDebugger && ((t == null ? void 0 : t.status) === "success" ? console.log(`[${i.action}] - 成功`, t == null ? void 0 : t.result) : console.error(`[${i.action}] - 失败`, t == null ? void 0 : t.errorMessage)), (t == null ? void 0 : t.status) === "success" ? r(t == null ? void 0 : t.result) : (console.error(t == null ? void 0 : t.errorMessage), a(new Error(t == null ? void 0 : t.errorMessage)));
|
|
25
25
|
});
|
|
26
26
|
});
|
|
27
27
|
}
|
|
@@ -39,12 +39,12 @@ class h {
|
|
|
39
39
|
}
|
|
40
40
|
async _handleReceivingMessage(e) {
|
|
41
41
|
if (e.data && e.data.from === this.slug && e.data.payload) {
|
|
42
|
-
const { action: s, payload:
|
|
43
|
-
if (this.isDebugger && console.log("收到消息", e.data), this.listeners[s]) for (let
|
|
44
|
-
const
|
|
45
|
-
|
|
46
|
-
} catch (
|
|
47
|
-
|
|
42
|
+
const { action: s, payload: r } = e.data.payload, { callbackId: a } = e.data;
|
|
43
|
+
if (this.isDebugger && console.log("收到消息", e.data), this.listeners[s]) for (let i of this.listeners[s]) try {
|
|
44
|
+
const t = await i(...r);
|
|
45
|
+
a && chrome.runtime.sendMessage(this.id, { action: "__callback", payload: { status: "success", callbackId: a, result: t } });
|
|
46
|
+
} catch (t) {
|
|
47
|
+
a && chrome.runtime.sendMessage(this.id, { action: "__callback", payload: { status: "error", callbackId: a, errorMessage: t.toString(), result: void 0 } }), console.error(t);
|
|
48
48
|
}
|
|
49
49
|
}
|
|
50
50
|
}
|