@webextkits/messages-center 0.1.8 → 0.2.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.d.ts +4 -0
- package/dist/background.js +66 -44
- package/dist/contentScript.js +4 -4
- package/dist/inject.d.ts +1 -1
- package/dist/inject.js +29 -24
- package/package.json +1 -1
package/dist/background.d.ts
CHANGED
|
@@ -1,14 +1,18 @@
|
|
|
1
1
|
import type { MessageObj } from "./inject";
|
|
2
|
+
type CallbackId = string;
|
|
2
3
|
export declare class MessageInstance<MessageType extends MessageObj<MessageType>, BackgroundMessageType extends MessageObj<BackgroundMessageType>> {
|
|
3
4
|
listeners: Partial<Record<keyof MessageType, any>>;
|
|
4
5
|
tabs: number[];
|
|
5
6
|
slug: string;
|
|
6
7
|
isDebugger: boolean;
|
|
8
|
+
callbackMap: Map<CallbackId, (result: any, sender: chrome.runtime.MessageSender) => void>;
|
|
7
9
|
constructor(slug: string, isDebugger?: boolean);
|
|
8
10
|
on<T extends keyof MessageType>(action: T, fn: (...args: Parameters<MessageType[T]>) => ReturnType<MessageType[T]>): () => void;
|
|
9
11
|
send<T extends keyof BackgroundMessageType>(action: T, ...payload: Parameters<BackgroundMessageType[T]>): Promise<void>;
|
|
12
|
+
sendWithCallback<T extends keyof BackgroundMessageType>(action: T, callback: (result: Awaited<ReturnType<BackgroundMessageType[T]>>, sender: chrome.runtime.MessageSender) => void, ...payload: Parameters<BackgroundMessageType[T]>): Promise<void>;
|
|
10
13
|
sendByTabId<T extends keyof BackgroundMessageType>(tabId: number, action: T, ...payload: Parameters<BackgroundMessageType[T]>): Promise<void>;
|
|
11
14
|
bindMessage(): void;
|
|
12
15
|
bindEvent(): void;
|
|
13
16
|
deleteTabById(tabId: any): void;
|
|
14
17
|
}
|
|
18
|
+
export {};
|
package/dist/background.js
CHANGED
|
@@ -1,62 +1,84 @@
|
|
|
1
|
-
var
|
|
2
|
-
var
|
|
3
|
-
var
|
|
4
|
-
class
|
|
5
|
-
constructor(
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
this
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
1
|
+
var g = Object.defineProperty;
|
|
2
|
+
var u = (l, s, t) => s in l ? g(l, s, { enumerable: !0, configurable: !0, writable: !0, value: t }) : l[s] = t;
|
|
3
|
+
var r = (l, s, t) => u(l, typeof s != "symbol" ? s + "" : s, t);
|
|
4
|
+
class p {
|
|
5
|
+
constructor(s, t) {
|
|
6
|
+
r(this, "listeners", {});
|
|
7
|
+
r(this, "tabs", []);
|
|
8
|
+
r(this, "slug");
|
|
9
|
+
r(this, "isDebugger");
|
|
10
|
+
r(this, "callbackMap", /* @__PURE__ */ new Map());
|
|
11
|
+
this.slug = s, this.isDebugger = !!t, this.bindMessage(), this.bindEvent();
|
|
12
|
+
}
|
|
13
|
+
on(s, t) {
|
|
14
|
+
if (this.listeners[s]) throw new Error(`event listener ${String(s)} already exist!`);
|
|
15
|
+
return this.listeners[s] = t, () => {
|
|
16
|
+
delete this.listeners[s];
|
|
16
17
|
};
|
|
17
18
|
}
|
|
18
|
-
async send(
|
|
19
|
-
this.isDebugger && console.log("发送消息", { action:
|
|
20
|
-
for (let
|
|
21
|
-
const
|
|
22
|
-
|
|
23
|
-
} catch (
|
|
24
|
-
console.log(
|
|
19
|
+
async send(s, ...t) {
|
|
20
|
+
this.isDebugger && console.log("发送消息", { action: s, payload: t, tabs: this.tabs });
|
|
21
|
+
for (let e of this.tabs) try {
|
|
22
|
+
const a = await chrome.tabs.get(e);
|
|
23
|
+
a && a.id ? chrome.tabs.sendMessage(a.id, { from: this.slug, payload: { action: s, payload: t } }) : this.deleteTabById(e);
|
|
24
|
+
} catch (a) {
|
|
25
|
+
console.log(a), this.deleteTabById(e);
|
|
25
26
|
}
|
|
26
27
|
}
|
|
27
|
-
async
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
28
|
+
async sendWithCallback(s, t, ...e) {
|
|
29
|
+
const a = c();
|
|
30
|
+
this.callbackMap.set(a, t), this.isDebugger && console.log("发送消息", { action: s, payload: e, tabs: this.tabs });
|
|
31
|
+
for (let o of this.tabs) try {
|
|
32
|
+
const n = await chrome.tabs.get(o);
|
|
33
|
+
n && n.id ? chrome.tabs.sendMessage(n.id, { from: this.slug, payload: { action: s, payload: e }, callbackId: a }) : this.deleteTabById(o);
|
|
34
|
+
} catch (n) {
|
|
35
|
+
console.log(n), this.deleteTabById(o);
|
|
34
36
|
}
|
|
35
37
|
}
|
|
38
|
+
async sendByTabId(s, t, ...e) {
|
|
39
|
+
this.isDebugger && console.log("发送消息(指定 tabId)", { tabId: s, action: t, payload: e });
|
|
40
|
+
const a = c();
|
|
41
|
+
return new Promise((o, n) => {
|
|
42
|
+
this.callbackMap.set(a, function(i) {
|
|
43
|
+
o(i);
|
|
44
|
+
}), chrome.tabs.get(s).then((i) => {
|
|
45
|
+
i && i.id ? chrome.tabs.sendMessage(i.id, { from: this.slug, payload: { action: t, payload: e }, callbackId: a }) : this.deleteTabById(s);
|
|
46
|
+
}).catch((i) => {
|
|
47
|
+
console.log(i), this.deleteTabById(s);
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
}
|
|
36
51
|
bindMessage() {
|
|
37
|
-
chrome.runtime.onMessageExternal.addListener((t, e
|
|
38
|
-
var
|
|
39
|
-
if ((
|
|
40
|
-
if (this.isDebugger && console.log("收到消息", { request:
|
|
41
|
-
if (
|
|
42
|
-
if (
|
|
43
|
-
|
|
44
|
-
|
|
52
|
+
chrome.runtime.onMessageExternal.addListener((s, t, e) => {
|
|
53
|
+
var a, o, n;
|
|
54
|
+
if ((a = t == null ? void 0 : t.tab) != null && a.id) {
|
|
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
|
+
if (s.action === "__disconnect") return this.deleteTabById(t.tab.id), e();
|
|
57
|
+
if (s.action === "__callback") {
|
|
58
|
+
const { callbackId: i, result: d, errorMessage: b, status: h } = s.payload;
|
|
59
|
+
return h === "error" ? console.error(b) : this.callbackMap.has(i) && ((o = this.callbackMap.get(i)) == null || o(d, t)), e();
|
|
60
|
+
}
|
|
61
|
+
if (this.listeners[s.action]) return (n = t == null ? void 0 : t.tab) != null && n.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((i) => {
|
|
62
|
+
this.isDebugger && console.log("send to callback from background", { request: s, sender: t, res: i }), e(i);
|
|
63
|
+
}), !0) : e();
|
|
45
64
|
}
|
|
46
65
|
});
|
|
47
66
|
}
|
|
48
67
|
bindEvent() {
|
|
49
|
-
chrome.tabs.onRemoved.addListener((
|
|
50
|
-
this.deleteTabById(
|
|
68
|
+
chrome.tabs.onRemoved.addListener((s) => {
|
|
69
|
+
this.deleteTabById(s);
|
|
51
70
|
});
|
|
52
71
|
}
|
|
53
|
-
deleteTabById(
|
|
54
|
-
if (this.tabs.includes(
|
|
55
|
-
const
|
|
56
|
-
this.tabs.splice(
|
|
72
|
+
deleteTabById(s) {
|
|
73
|
+
if (this.tabs.includes(s)) {
|
|
74
|
+
const t = this.tabs.indexOf(s);
|
|
75
|
+
this.tabs.splice(t, 1);
|
|
57
76
|
}
|
|
58
77
|
}
|
|
59
78
|
}
|
|
79
|
+
function c() {
|
|
80
|
+
return (/* @__PURE__ */ new Date()).getTime().toString() + Math.random().toString(36).substring(2, 15);
|
|
81
|
+
}
|
|
60
82
|
export {
|
|
61
|
-
|
|
83
|
+
p as MessageInstance
|
|
62
84
|
};
|
package/dist/contentScript.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
function
|
|
2
|
-
chrome.runtime.onMessage.addListener((e,
|
|
3
|
-
e.from === o && (
|
|
1
|
+
function a(o, r) {
|
|
2
|
+
chrome.runtime.onMessage.addListener((e, s) => {
|
|
3
|
+
e.from === o && (r && console.log("received message from background script", e), window.postMessage({ from: e.from, payload: e.payload, callbackId: e.callbackId }));
|
|
4
4
|
});
|
|
5
5
|
}
|
|
6
6
|
export {
|
|
7
|
-
|
|
7
|
+
a as useMessageBridgeContentScript
|
|
8
8
|
};
|
package/dist/inject.d.ts
CHANGED
package/dist/inject.js
CHANGED
|
@@ -1,27 +1,27 @@
|
|
|
1
|
-
var
|
|
2
|
-
var
|
|
3
|
-
var
|
|
1
|
+
var r = Object.defineProperty;
|
|
2
|
+
var d = (t, e, s) => e in t ? r(t, e, { enumerable: !0, configurable: !0, writable: !0, value: s }) : t[e] = s;
|
|
3
|
+
var a = (t, e, s) => d(t, typeof e != "symbol" ? e + "" : e, s);
|
|
4
4
|
class h {
|
|
5
|
-
constructor(
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
this.slug =
|
|
11
|
-
}
|
|
12
|
-
on(
|
|
13
|
-
return this.listeners[
|
|
14
|
-
if (this.listeners[
|
|
15
|
-
const
|
|
16
|
-
this.listeners[
|
|
5
|
+
constructor(e, s) {
|
|
6
|
+
a(this, "id");
|
|
7
|
+
a(this, "listeners", {});
|
|
8
|
+
a(this, "slug");
|
|
9
|
+
a(this, "isDebugger");
|
|
10
|
+
this.slug = e, this.isDebugger = !!s, this.id = window[e].id, this.bindEvent();
|
|
11
|
+
}
|
|
12
|
+
on(e, s) {
|
|
13
|
+
return this.listeners[e] = this.listeners[e] || [], this.listeners[e].push(s), () => {
|
|
14
|
+
if (this.listeners[e].includes(s)) {
|
|
15
|
+
const n = this.listeners[e].indexOf(s);
|
|
16
|
+
this.listeners[e].splice(n, 1);
|
|
17
17
|
}
|
|
18
18
|
};
|
|
19
19
|
}
|
|
20
|
-
send(
|
|
21
|
-
return new Promise(async (
|
|
22
|
-
let
|
|
23
|
-
this.isDebugger && console.log("发送消息",
|
|
24
|
-
this.isDebugger && console.log("收到消息",
|
|
20
|
+
send(e, ...s) {
|
|
21
|
+
return new Promise(async (n) => {
|
|
22
|
+
let i = { action: e, payload: s };
|
|
23
|
+
this.isDebugger && console.log("发送消息", i), chrome.runtime.sendMessage(this.id, i, {}, (o) => {
|
|
24
|
+
this.isDebugger && console.log("收到消息", o), n(o);
|
|
25
25
|
});
|
|
26
26
|
});
|
|
27
27
|
}
|
|
@@ -37,10 +37,15 @@ class h {
|
|
|
37
37
|
destroy() {
|
|
38
38
|
this.disconnect(), this.listeners = {}, window.removeEventListener("message", this._handleReceivingMessage.bind(this));
|
|
39
39
|
}
|
|
40
|
-
_handleReceivingMessage(
|
|
41
|
-
if (
|
|
42
|
-
const { action:
|
|
43
|
-
if (this.isDebugger && console.log("收到消息",
|
|
40
|
+
async _handleReceivingMessage(e) {
|
|
41
|
+
if (e.data && e.data.from === this.slug && e.data.payload) {
|
|
42
|
+
const { action: s, payload: n } = e.data.payload, { callbackId: i } = e.data;
|
|
43
|
+
if (this.isDebugger && console.log("收到消息", e.data), this.listeners[s]) for (let o of this.listeners[s]) try {
|
|
44
|
+
const c = await o(...n);
|
|
45
|
+
i && chrome.runtime.sendMessage(this.id, { action: "__callback", payload: { status: "success", callbackId: i, result: c } });
|
|
46
|
+
} catch (c) {
|
|
47
|
+
i && chrome.runtime.sendMessage(this.id, { action: "__callback", payload: { status: "error", callbackId: i, errorMessage: c.toString(), result: void 0 } }), console.error(c);
|
|
48
|
+
}
|
|
44
49
|
}
|
|
45
50
|
}
|
|
46
51
|
}
|