@webextkits/messages-center 0.2.2 → 1.0.1
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 +51 -55
- package/dist/contentScript.js +2 -2
- package/dist/hooks.js +29 -23
- package/dist/inject.d.ts +6 -0
- package/dist/inject.js +48 -34
- package/package.json +15 -10
- package/LICENSE +0 -21
package/dist/background.js
CHANGED
|
@@ -1,86 +1,82 @@
|
|
|
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
|
|
41
|
-
return new Promise((n, o) => {
|
|
42
|
-
this.callbackMap.set(
|
|
43
|
-
n(
|
|
44
|
-
}), chrome.tabs.get(s).then((
|
|
45
|
-
|
|
46
|
-
}).catch((
|
|
47
|
-
console.log(
|
|
48
|
-
});
|
|
49
|
-
});
|
|
35
|
+
async sendByTabId(s, e, ...a) {
|
|
36
|
+
this.isDebugger && console.log("发送消息(指定 tabId)", { tabId: s, action: e, payload: a });
|
|
37
|
+
const t = r();
|
|
38
|
+
return new Promise(((n, o) => {
|
|
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);
|
|
45
|
+
}));
|
|
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
|
}
|
|
69
65
|
bindEvent() {
|
|
70
|
-
chrome.tabs.onRemoved.addListener((s) => {
|
|
66
|
+
chrome.tabs.onRemoved.addListener(((s) => {
|
|
71
67
|
this.deleteTabById(s);
|
|
72
|
-
});
|
|
68
|
+
}));
|
|
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/contentScript.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
function a(o, r) {
|
|
2
|
-
chrome.runtime.onMessage.addListener((e, s) => {
|
|
2
|
+
chrome.runtime.onMessage.addListener(((e, s) => {
|
|
3
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
|
package/dist/hooks.js
CHANGED
|
@@ -1,36 +1,42 @@
|
|
|
1
|
-
function l(
|
|
2
|
-
if (
|
|
3
|
-
let
|
|
1
|
+
function l(c, o) {
|
|
2
|
+
if (c === "<all_urls>") return /^(https?|file):\/\//.test(o);
|
|
3
|
+
let r;
|
|
4
4
|
try {
|
|
5
|
-
|
|
5
|
+
r = new URL(o);
|
|
6
6
|
} catch {
|
|
7
7
|
return !1;
|
|
8
8
|
}
|
|
9
|
-
const
|
|
10
|
-
if (!
|
|
11
|
-
const [, a,
|
|
9
|
+
const i = c.match(/^([^:]+):\/\/([^\/]+)(\/.*)?$/);
|
|
10
|
+
if (!i) return !1;
|
|
11
|
+
const [, a, u, n = "/*"] = i;
|
|
12
12
|
if (a === "*") {
|
|
13
|
-
if (
|
|
14
|
-
} else if (
|
|
15
|
-
return !!function(t,
|
|
13
|
+
if (r.protocol !== "http:" && r.protocol !== "https:") return !1;
|
|
14
|
+
} else if (r.protocol !== `${a}:`) return !1;
|
|
15
|
+
return !!(function(t, e) {
|
|
16
16
|
if (t === "*") return !0;
|
|
17
17
|
if (t.startsWith("*.")) {
|
|
18
|
-
const
|
|
19
|
-
return
|
|
18
|
+
const s = t.slice(2);
|
|
19
|
+
return e === s || e.endsWith("." + s);
|
|
20
20
|
}
|
|
21
|
-
return t ===
|
|
22
|
-
}(
|
|
23
|
-
const
|
|
24
|
-
return new RegExp(`^${
|
|
25
|
-
}(
|
|
21
|
+
return t === e;
|
|
22
|
+
})(u, r.hostname) && !!(function(t, e) {
|
|
23
|
+
const s = t.replace(/[.+^${}()|[\]\\]/g, "\\$&").replace(/\*/g, ".*");
|
|
24
|
+
return new RegExp(`^${s}$`, "i").test(e);
|
|
25
|
+
})(n, r.pathname);
|
|
26
26
|
}
|
|
27
|
-
function h(
|
|
28
|
-
const { hostMatch:
|
|
29
|
-
|
|
30
|
-
|
|
27
|
+
function h(c) {
|
|
28
|
+
const { hostMatch: o, extensionId: r, isDev: i = !1 } = c, a = { id: chrome.runtime.id, slug: r, isDev: i };
|
|
29
|
+
function u(n) {
|
|
30
|
+
chrome.scripting.executeScript({ injectImmediately: !0, world: "MAIN", target: { tabId: n, allFrames: !0 }, func: (t) => {
|
|
31
31
|
window[t.slug] = t;
|
|
32
|
-
}, args: [
|
|
33
|
-
|
|
32
|
+
}, args: [a] }).catch((() => {
|
|
33
|
+
}));
|
|
34
|
+
}
|
|
35
|
+
chrome.tabs.onUpdated.addListener((async (n, t, e) => {
|
|
36
|
+
e?.url && t?.status && (t.status !== "loading" && t.status !== "complete" || o.some(((s) => l(s, e.url))) && u(n));
|
|
37
|
+
})), chrome.tabs.query({}, ((n) => {
|
|
38
|
+
for (const t of n) t.id && t.url && o.some(((e) => l(e, t.url))) && u(t.id);
|
|
39
|
+
}));
|
|
34
40
|
}
|
|
35
41
|
export {
|
|
36
42
|
h as injectExtensionData,
|
package/dist/inject.d.ts
CHANGED
|
@@ -3,11 +3,17 @@ export type MessageObj<T> = {
|
|
|
3
3
|
};
|
|
4
4
|
export type SendFn<T extends MessageObj<T>> = MessagesInstance<T, T>["send"];
|
|
5
5
|
export type onFn<T extends MessageObj<T>> = MessagesInstance<T, T>["on"];
|
|
6
|
+
export declare function waitForExtensionData(slug: string, timeoutMs?: number, intervalMs?: number): Promise<{
|
|
7
|
+
id: string;
|
|
8
|
+
slug: string;
|
|
9
|
+
isDev: boolean;
|
|
10
|
+
}>;
|
|
6
11
|
export declare class MessagesInstance<MessageType extends MessageObj<MessageType>, BackgroundMessageType extends MessageObj<BackgroundMessageType>> {
|
|
7
12
|
id: string;
|
|
8
13
|
listeners: Partial<Record<keyof BackgroundMessageType, any>>;
|
|
9
14
|
slug: string;
|
|
10
15
|
isDebugger: boolean;
|
|
16
|
+
private _ready;
|
|
11
17
|
constructor(slug: string, isDebugger?: boolean);
|
|
12
18
|
on<T extends keyof BackgroundMessageType>(action: T, fn: (...args: Parameters<BackgroundMessageType[T]>) => void): () => void;
|
|
13
19
|
send<T extends keyof MessageType>(action: T, ...payload: Parameters<MessageType[T]>): Promise<Awaited<ReturnType<MessageType[T]>>>;
|
package/dist/inject.js
CHANGED
|
@@ -1,35 +1,48 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
1
|
+
function o(r, s = 5e3, t = 50) {
|
|
2
|
+
return new Promise(((i, a) => {
|
|
3
|
+
if (window[r]?.id) return void i(window[r]);
|
|
4
|
+
const n = Date.now(), e = setInterval((() => {
|
|
5
|
+
window[r]?.id ? (clearInterval(e), i(window[r])) : Date.now() - n > s && (clearInterval(e), a(new Error(`[MessagesInstance] 等待 window["${r}"] 超时 (${s}ms),injectExtensionData 可能未正确配置`)));
|
|
6
|
+
}), t);
|
|
7
|
+
}));
|
|
8
|
+
}
|
|
9
|
+
class c {
|
|
10
|
+
id;
|
|
11
|
+
listeners = {};
|
|
12
|
+
slug;
|
|
13
|
+
isDebugger;
|
|
14
|
+
_ready;
|
|
15
|
+
constructor(s, t) {
|
|
16
|
+
this.slug = s, this.isDebugger = !!t, window[s]?.id ? (this.id = window[s].id, this._ready = Promise.resolve()) : (this.id = "", this._ready = o(s).then(((i) => {
|
|
17
|
+
this.id = i.id, this.isDebugger && console.log(`[MessagesInstance] 延迟获取到扩展 ID: ${this.id}`);
|
|
18
|
+
}))), this.bindEvent();
|
|
19
|
+
}
|
|
20
|
+
on(s, t) {
|
|
21
|
+
return this.listeners[s] = this.listeners[s] || [], this.listeners[s].push(t), () => {
|
|
22
|
+
if (this.listeners[s].includes(t)) {
|
|
23
|
+
const i = this.listeners[s].indexOf(t);
|
|
24
|
+
this.listeners[s].splice(i, 1);
|
|
17
25
|
}
|
|
18
26
|
};
|
|
19
27
|
}
|
|
20
|
-
send(
|
|
21
|
-
return new Promise(async (
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
28
|
+
send(s, ...t) {
|
|
29
|
+
return new Promise((async (i, a) => {
|
|
30
|
+
await this._ready;
|
|
31
|
+
let n = { action: s, payload: t };
|
|
32
|
+
this.isDebugger && console.log(`[${n.action}] - 发送`, n), chrome.runtime.sendMessage(this.id, n, {}, ((e) => {
|
|
33
|
+
this.isDebugger && (e?.status === "success" ? console.log(`[${n.action}] - 成功`, e?.result) : console.error(`[${n.action}] - 失败`, e?.errorMessage)), e?.status === "success" ? i(e?.result) : (console.error(e?.errorMessage), a(new Error(e?.errorMessage)));
|
|
34
|
+
}));
|
|
35
|
+
}));
|
|
27
36
|
}
|
|
28
37
|
connect() {
|
|
29
|
-
|
|
38
|
+
this._ready.then((() => {
|
|
39
|
+
chrome.runtime.sendMessage(this.id, { action: "__connect" });
|
|
40
|
+
}));
|
|
30
41
|
}
|
|
31
42
|
disconnect() {
|
|
32
|
-
|
|
43
|
+
this._ready.then((() => {
|
|
44
|
+
chrome.runtime.sendMessage(this.id, { action: "__disconnect" });
|
|
45
|
+
}));
|
|
33
46
|
}
|
|
34
47
|
bindEvent() {
|
|
35
48
|
window.addEventListener("message", this._handleReceivingMessage.bind(this));
|
|
@@ -37,18 +50,19 @@ class h {
|
|
|
37
50
|
destroy() {
|
|
38
51
|
this.disconnect(), this.listeners = {}, window.removeEventListener("message", this._handleReceivingMessage.bind(this));
|
|
39
52
|
}
|
|
40
|
-
async _handleReceivingMessage(
|
|
41
|
-
if (
|
|
42
|
-
const { action:
|
|
43
|
-
if (this.isDebugger && console.log("收到消息",
|
|
44
|
-
const
|
|
45
|
-
a && chrome.runtime.sendMessage(this.id, { action: "__callback", payload: { status: "success", callbackId: a, result:
|
|
46
|
-
} catch (
|
|
47
|
-
a && chrome.runtime.sendMessage(this.id, { action: "__callback", payload: { status: "error", callbackId: a, errorMessage:
|
|
53
|
+
async _handleReceivingMessage(s) {
|
|
54
|
+
if (s.data && s.data.from === this.slug && s.data.payload) {
|
|
55
|
+
const { action: t, payload: i } = s.data.payload, { callbackId: a } = s.data;
|
|
56
|
+
if (this.isDebugger && console.log("收到消息", s.data), this.listeners[t]) for (let n of this.listeners[t]) try {
|
|
57
|
+
const e = await n(...i);
|
|
58
|
+
a && chrome.runtime.sendMessage(this.id, { action: "__callback", payload: { status: "success", callbackId: a, result: e } });
|
|
59
|
+
} catch (e) {
|
|
60
|
+
a && chrome.runtime.sendMessage(this.id, { action: "__callback", payload: { status: "error", callbackId: a, errorMessage: e.toString(), result: void 0 } }), console.error(e);
|
|
48
61
|
}
|
|
49
62
|
}
|
|
50
63
|
}
|
|
51
64
|
}
|
|
52
65
|
export {
|
|
53
|
-
|
|
66
|
+
c as MessagesInstance,
|
|
67
|
+
o as waitForExtensionData
|
|
54
68
|
};
|
package/package.json
CHANGED
|
@@ -1,7 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@webextkits/messages-center",
|
|
3
3
|
"private": false,
|
|
4
|
-
"
|
|
4
|
+
"repository": {
|
|
5
|
+
"type": "git",
|
|
6
|
+
"url": "https://github.com/FrontEndSharedProject/webextkits.git",
|
|
7
|
+
"directory": "packages/messages"
|
|
8
|
+
},
|
|
9
|
+
"version": "1.0.1",
|
|
5
10
|
"type": "module",
|
|
6
11
|
"exports": {
|
|
7
12
|
"./background": "./dist/background.js",
|
|
@@ -28,16 +33,16 @@
|
|
|
28
33
|
"files": [
|
|
29
34
|
"dist"
|
|
30
35
|
],
|
|
31
|
-
"devDependencies": {
|
|
32
|
-
"@rollup/plugin-terser": "^0.4.4",
|
|
33
|
-
"@types/node": "^20.8.8",
|
|
34
|
-
"esno": "^0.17.0",
|
|
35
|
-
"glob": "^10.3.10",
|
|
36
|
-
"typescript": "^5.0.2",
|
|
37
|
-
"vite": "=5.4.10"
|
|
38
|
-
},
|
|
39
36
|
"scripts": {
|
|
40
37
|
"build": "esno ./scripts/build.ts && npm run build:type",
|
|
41
38
|
"build:type": "tsc -p ./tsconfig.json -emitDeclarationOnly"
|
|
39
|
+
},
|
|
40
|
+
"devDependencies": {
|
|
41
|
+
"@rollup/plugin-terser": "^0.4.4",
|
|
42
|
+
"@types/node": "^24.10.0",
|
|
43
|
+
"esno": "^4.8.0",
|
|
44
|
+
"glob": "^11.0.3",
|
|
45
|
+
"typescript": "^5.9.3",
|
|
46
|
+
"vite": "^7.2.1"
|
|
42
47
|
}
|
|
43
|
-
}
|
|
48
|
+
}
|
package/LICENSE
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) CarlJin <carljin.com>
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|