@webextkits/messages-center 0.2.2 → 1.0.0
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 +45 -49
- package/dist/hooks.js +3 -3
- package/dist/inject.js +25 -28
- package/package.json +6 -6
package/dist/background.js
CHANGED
|
@@ -1,68 +1,64 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
c(this, "isDebugger");
|
|
10
|
-
c(this, "callbackMap", /* @__PURE__ */ new Map());
|
|
11
|
-
this.slug = s, this.isDebugger = !!t, this.bindMessage(), this.bindEvent();
|
|
1
|
+
class l {
|
|
2
|
+
listeners = {};
|
|
3
|
+
tabs = [];
|
|
4
|
+
slug;
|
|
5
|
+
isDebugger;
|
|
6
|
+
callbackMap = /* @__PURE__ */ new Map();
|
|
7
|
+
constructor(s, e) {
|
|
8
|
+
this.slug = s, this.isDebugger = !!e, this.bindMessage(), this.bindEvent();
|
|
12
9
|
}
|
|
13
|
-
on(s,
|
|
10
|
+
on(s, e) {
|
|
14
11
|
if (this.listeners[s]) throw new Error(`event listener ${String(s)} already exist!`);
|
|
15
|
-
return this.listeners[s] =
|
|
12
|
+
return this.listeners[s] = e, () => {
|
|
16
13
|
delete this.listeners[s];
|
|
17
14
|
};
|
|
18
15
|
}
|
|
19
|
-
async send(s, ...
|
|
20
|
-
this.isDebugger && console.log("发送消息", { action: s, payload:
|
|
21
|
-
for (let
|
|
22
|
-
const
|
|
23
|
-
|
|
24
|
-
} catch (
|
|
25
|
-
console.log(
|
|
16
|
+
async send(s, ...e) {
|
|
17
|
+
this.isDebugger && console.log("发送消息", { action: s, payload: e, tabs: this.tabs });
|
|
18
|
+
for (let a of this.tabs) try {
|
|
19
|
+
const t = await chrome.tabs.get(a);
|
|
20
|
+
t && t.id ? chrome.tabs.sendMessage(t.id, { from: this.slug, payload: { action: s, payload: e } }) : this.deleteTabById(a);
|
|
21
|
+
} catch (t) {
|
|
22
|
+
console.log(t), this.deleteTabById(a);
|
|
26
23
|
}
|
|
27
24
|
}
|
|
28
|
-
async sendWithCallback(s,
|
|
29
|
-
const
|
|
30
|
-
this.callbackMap.set(
|
|
25
|
+
async sendWithCallback(s, e, ...a) {
|
|
26
|
+
const t = r();
|
|
27
|
+
this.callbackMap.set(t, e), this.isDebugger && console.log("发送消息", { action: s, payload: a, tabs: this.tabs });
|
|
31
28
|
for (let n of this.tabs) try {
|
|
32
29
|
const o = await chrome.tabs.get(n);
|
|
33
|
-
o && o.id ? chrome.tabs.sendMessage(o.id, { from: this.slug, payload: { action: s, payload:
|
|
30
|
+
o && o.id ? chrome.tabs.sendMessage(o.id, { from: this.slug, payload: { action: s, payload: a }, callbackId: t }) : this.deleteTabById(n);
|
|
34
31
|
} catch (o) {
|
|
35
32
|
console.log(o), this.deleteTabById(n);
|
|
36
33
|
}
|
|
37
34
|
}
|
|
38
|
-
async sendByTabId(s,
|
|
39
|
-
this.isDebugger && console.log("发送消息(指定 tabId)", { tabId: s, action:
|
|
40
|
-
const
|
|
35
|
+
async sendByTabId(s, e, ...a) {
|
|
36
|
+
this.isDebugger && console.log("发送消息(指定 tabId)", { tabId: s, action: e, payload: a });
|
|
37
|
+
const t = r();
|
|
41
38
|
return new Promise((n, o) => {
|
|
42
|
-
this.callbackMap.set(
|
|
43
|
-
n(
|
|
44
|
-
}), chrome.tabs.get(s).then((
|
|
45
|
-
|
|
46
|
-
}).catch((
|
|
47
|
-
console.log(
|
|
39
|
+
this.callbackMap.set(t, function(i) {
|
|
40
|
+
n(i);
|
|
41
|
+
}), chrome.tabs.get(s).then((i) => {
|
|
42
|
+
i && i.id ? chrome.tabs.sendMessage(i.id, { from: this.slug, payload: { action: e, payload: a }, callbackId: t }) : this.deleteTabById(s);
|
|
43
|
+
}).catch((i) => {
|
|
44
|
+
console.log(i), this.deleteTabById(s);
|
|
48
45
|
});
|
|
49
46
|
});
|
|
50
47
|
}
|
|
51
48
|
bindMessage() {
|
|
52
|
-
chrome.runtime.onMessageExternal.addListener((s,
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
if (
|
|
56
|
-
if (s.action === "__disconnect") return this.deleteTabById(t.tab.id), e();
|
|
49
|
+
chrome.runtime.onMessageExternal.addListener((s, e, a) => {
|
|
50
|
+
if (e?.tab?.id) {
|
|
51
|
+
if (this.isDebugger && console.log("收到消息", { request: s, sender: e }), s.action === "__connect") return this.tabs.includes(e.tab.id) || this.tabs.push(e.tab.id), a();
|
|
52
|
+
if (s.action === "__disconnect") return this.deleteTabById(e.tab.id), a();
|
|
57
53
|
if (s.action === "__callback") {
|
|
58
|
-
const { callbackId:
|
|
59
|
-
return
|
|
54
|
+
const { callbackId: t, result: n, errorMessage: o, status: i } = s.payload;
|
|
55
|
+
return i === "error" ? console.error(o) : this.callbackMap.has(t) && this.callbackMap.get(t)?.(n, e), a();
|
|
60
56
|
}
|
|
61
|
-
if (this.listeners[s.action]) return
|
|
62
|
-
this.isDebugger && console.log("send to callback from background", { request: s, sender:
|
|
63
|
-
}).catch((
|
|
64
|
-
console.error(
|
|
65
|
-
}), !0) :
|
|
57
|
+
if (this.listeners[s.action]) return e?.tab?.id ? (this.tabs.includes(e.tab.id) || this.tabs.push(e.tab.id), this.listeners[s.action].bind({ request: s, sender: e })(...s.payload).then((t) => {
|
|
58
|
+
this.isDebugger && console.log("send to callback from background", { request: s, sender: e, res: t }), a({ status: "success", result: t });
|
|
59
|
+
}).catch((t) => {
|
|
60
|
+
console.error(t), a({ status: "error", errorMessage: t.toString() });
|
|
61
|
+
}), !0) : a();
|
|
66
62
|
}
|
|
67
63
|
});
|
|
68
64
|
}
|
|
@@ -73,14 +69,14 @@ class p {
|
|
|
73
69
|
}
|
|
74
70
|
deleteTabById(s) {
|
|
75
71
|
if (this.tabs.includes(s)) {
|
|
76
|
-
const
|
|
77
|
-
this.tabs.splice(
|
|
72
|
+
const e = this.tabs.indexOf(s);
|
|
73
|
+
this.tabs.splice(e, 1);
|
|
78
74
|
}
|
|
79
75
|
}
|
|
80
76
|
}
|
|
81
|
-
function
|
|
77
|
+
function r() {
|
|
82
78
|
return (/* @__PURE__ */ new Date()).getTime().toString() + Math.random().toString(36).substring(2, 15);
|
|
83
79
|
}
|
|
84
80
|
export {
|
|
85
|
-
|
|
81
|
+
l as MessageInstance
|
|
86
82
|
};
|
package/dist/hooks.js
CHANGED
|
@@ -12,17 +12,17 @@ function l(s, i) {
|
|
|
12
12
|
if (a === "*") {
|
|
13
13
|
if (e.protocol !== "http:" && e.protocol !== "https:") return !1;
|
|
14
14
|
} else if (e.protocol !== `${a}:`) return !1;
|
|
15
|
-
return !!function(t, n) {
|
|
15
|
+
return !!(function(t, n) {
|
|
16
16
|
if (t === "*") return !0;
|
|
17
17
|
if (t.startsWith("*.")) {
|
|
18
18
|
const u = t.slice(2);
|
|
19
19
|
return n === u || n.endsWith("." + u);
|
|
20
20
|
}
|
|
21
21
|
return t === n;
|
|
22
|
-
}(o, e.hostname) && !!function(t, n) {
|
|
22
|
+
})(o, e.hostname) && !!(function(t, n) {
|
|
23
23
|
const u = t.replace(/[.+^${}()|[\]\\]/g, "\\$&").replace(/\*/g, ".*");
|
|
24
24
|
return new RegExp(`^${u}$`, "i").test(n);
|
|
25
|
-
}(r, e.pathname);
|
|
25
|
+
})(r, e.pathname);
|
|
26
26
|
}
|
|
27
27
|
function h(s) {
|
|
28
28
|
const { hostMatch: i, extensionId: e, isDev: c = !1 } = s;
|
package/dist/inject.js
CHANGED
|
@@ -1,27 +1,24 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
this.
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
if (this.listeners[e].includes(s)) {
|
|
15
|
-
const r = this.listeners[e].indexOf(s);
|
|
16
|
-
this.listeners[e].splice(r, 1);
|
|
1
|
+
class o {
|
|
2
|
+
id;
|
|
3
|
+
listeners = {};
|
|
4
|
+
slug;
|
|
5
|
+
isDebugger;
|
|
6
|
+
constructor(e, t) {
|
|
7
|
+
this.slug = e, this.isDebugger = !!t, this.id = window[e].id, this.bindEvent();
|
|
8
|
+
}
|
|
9
|
+
on(e, t) {
|
|
10
|
+
return this.listeners[e] = this.listeners[e] || [], this.listeners[e].push(t), () => {
|
|
11
|
+
if (this.listeners[e].includes(t)) {
|
|
12
|
+
const a = this.listeners[e].indexOf(t);
|
|
13
|
+
this.listeners[e].splice(a, 1);
|
|
17
14
|
}
|
|
18
15
|
};
|
|
19
16
|
}
|
|
20
|
-
send(e, ...
|
|
21
|
-
return new Promise(async (
|
|
22
|
-
let i = { action: e, payload:
|
|
23
|
-
this.isDebugger && console.log(`[${i.action}] - 发送`, i), chrome.runtime.sendMessage(this.id, i, {}, (
|
|
24
|
-
this.isDebugger && (
|
|
17
|
+
send(e, ...t) {
|
|
18
|
+
return new Promise(async (a, n) => {
|
|
19
|
+
let i = { action: e, payload: t };
|
|
20
|
+
this.isDebugger && console.log(`[${i.action}] - 发送`, i), chrome.runtime.sendMessage(this.id, i, {}, (s) => {
|
|
21
|
+
this.isDebugger && (s?.status === "success" ? console.log(`[${i.action}] - 成功`, s?.result) : console.error(`[${i.action}] - 失败`, s?.errorMessage)), s?.status === "success" ? a(s?.result) : (console.error(s?.errorMessage), n(new Error(s?.errorMessage)));
|
|
25
22
|
});
|
|
26
23
|
});
|
|
27
24
|
}
|
|
@@ -39,16 +36,16 @@ class h {
|
|
|
39
36
|
}
|
|
40
37
|
async _handleReceivingMessage(e) {
|
|
41
38
|
if (e.data && e.data.from === this.slug && e.data.payload) {
|
|
42
|
-
const { action:
|
|
43
|
-
if (this.isDebugger && console.log("收到消息", e.data), this.listeners[
|
|
44
|
-
const
|
|
45
|
-
|
|
46
|
-
} catch (
|
|
47
|
-
|
|
39
|
+
const { action: t, payload: a } = e.data.payload, { callbackId: n } = e.data;
|
|
40
|
+
if (this.isDebugger && console.log("收到消息", e.data), this.listeners[t]) for (let i of this.listeners[t]) try {
|
|
41
|
+
const s = await i(...a);
|
|
42
|
+
n && chrome.runtime.sendMessage(this.id, { action: "__callback", payload: { status: "success", callbackId: n, result: s } });
|
|
43
|
+
} catch (s) {
|
|
44
|
+
n && chrome.runtime.sendMessage(this.id, { action: "__callback", payload: { status: "error", callbackId: n, errorMessage: s.toString(), result: void 0 } }), console.error(s);
|
|
48
45
|
}
|
|
49
46
|
}
|
|
50
47
|
}
|
|
51
48
|
}
|
|
52
49
|
export {
|
|
53
|
-
|
|
50
|
+
o as MessagesInstance
|
|
54
51
|
};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@webextkits/messages-center",
|
|
3
3
|
"private": false,
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "1.0.0",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
7
7
|
"./background": "./dist/background.js",
|
|
@@ -30,11 +30,11 @@
|
|
|
30
30
|
],
|
|
31
31
|
"devDependencies": {
|
|
32
32
|
"@rollup/plugin-terser": "^0.4.4",
|
|
33
|
-
"@types/node": "^
|
|
34
|
-
"esno": "^
|
|
35
|
-
"glob": "^
|
|
36
|
-
"typescript": "^5.
|
|
37
|
-
"vite": "
|
|
33
|
+
"@types/node": "^24.10.0",
|
|
34
|
+
"esno": "^4.8.0",
|
|
35
|
+
"glob": "^11.0.3",
|
|
36
|
+
"typescript": "^5.9.3",
|
|
37
|
+
"vite": "^7.2.1"
|
|
38
38
|
},
|
|
39
39
|
"scripts": {
|
|
40
40
|
"build": "esno ./scripts/build.ts && npm run build:type",
|