@webextkits/messages-center 0.1.5 → 0.1.7

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.
@@ -1,6 +1,6 @@
1
1
  var o = Object.defineProperty;
2
2
  var b = (a, e, t) => e in a ? o(a, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : a[e] = t;
3
- var n = (a, e, t) => (b(a, typeof e != "symbol" ? e + "" : e, t), t);
3
+ var n = (a, e, t) => b(a, typeof e != "symbol" ? e + "" : e, t);
4
4
  class h {
5
5
  constructor(e, t) {
6
6
  n(this, "listeners", {});
@@ -10,34 +10,29 @@ class h {
10
10
  this.slug = e, this.isDebugger = !!t, this.bindMessage(), this.bindEvent();
11
11
  }
12
12
  on(e, t) {
13
- if (this.listeners[e])
14
- throw new Error(`event listener ${String(e)} already exist!`);
13
+ if (this.listeners[e]) throw new Error(`event listener ${String(e)} already exist!`);
15
14
  return this.listeners[e] = t, () => {
16
15
  delete this.listeners[e];
17
16
  };
18
17
  }
19
18
  async send(e, ...t) {
20
19
  this.isDebugger && console.log("发送消息", { action: e, payload: t, tabs: this.tabs });
21
- for (let s of this.tabs)
22
- try {
23
- const i = await chrome.tabs.get(s);
24
- i && i.id ? chrome.tabs.sendMessage(i.id, { from: this.slug, payload: { action: e, payload: t } }) : this.deleteTabById(s);
25
- } catch (i) {
26
- console.log(i), this.deleteTabById(s);
27
- }
20
+ for (let s of this.tabs) try {
21
+ const i = await chrome.tabs.get(s);
22
+ i && i.id ? chrome.tabs.sendMessage(i.id, { from: this.slug, payload: { action: e, payload: t } }) : this.deleteTabById(s);
23
+ } catch (i) {
24
+ console.log(i), this.deleteTabById(s);
25
+ }
28
26
  }
29
27
  bindMessage() {
30
28
  chrome.runtime.onMessageExternal.addListener((e, t, s) => {
31
29
  var i, r;
32
30
  if ((i = t == null ? void 0 : t.tab) != null && i.id) {
33
- if (this.isDebugger && console.log("收到消息", { request: e, sender: t }), e.action === "__connect")
34
- return this.tabs.includes(t.tab.id) || this.tabs.push(t.tab.id), s();
35
- if (e.action === "__disconnect")
36
- return this.deleteTabById(t.tab.id), s();
37
- if (this.listeners[e.action])
38
- 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((d) => {
39
- this.isDebugger && console.log("send to callback from background", { request: e, sender: t, res: d }), s(d);
40
- }), !0) : s();
31
+ if (this.isDebugger && console.log("收到消息", { request: e, sender: t }), e.action === "__connect") return this.tabs.includes(t.tab.id) || this.tabs.push(t.tab.id), s();
32
+ if (e.action === "__disconnect") return this.deleteTabById(t.tab.id), s();
33
+ if (this.listeners[e.action]) 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((d) => {
34
+ this.isDebugger && console.log("send to callback from background", { request: e, sender: t, res: d }), s(d);
35
+ }), !0) : s();
41
36
  }
42
37
  });
43
38
  }
package/dist/hooks.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ export declare function matchesPattern(pattern: string, url: string): boolean;
1
2
  export declare function injectExtensionData(payload: {
2
3
  hostMatch: string[];
3
4
  extensionId: string;
package/dist/hooks.js CHANGED
@@ -1,6 +1,5 @@
1
1
  function l(s, i) {
2
- if (s === "<all_urls>")
3
- return /^(https?|file):\/\//.test(i);
2
+ if (s === "<all_urls>") return /^(https?|file):\/\//.test(i);
4
3
  let e;
5
4
  try {
6
5
  e = new URL(i);
@@ -8,35 +7,32 @@ function l(s, i) {
8
7
  return !1;
9
8
  }
10
9
  const c = s.match(/^([^:]+):\/\/([^\/]+)(\/.*)?$/);
11
- if (!c)
12
- return !1;
13
- const [, o, a, r = "/*"] = c;
14
- if (o === "*") {
15
- if (e.protocol !== "http:" && e.protocol !== "https:")
16
- return !1;
17
- } else if (e.protocol !== `${o}:`)
18
- return !1;
10
+ if (!c) return !1;
11
+ const [, a, o, r = "/*"] = c;
12
+ if (a === "*") {
13
+ if (e.protocol !== "http:" && e.protocol !== "https:") return !1;
14
+ } else if (e.protocol !== `${a}:`) return !1;
19
15
  return !!function(t, n) {
20
- if (t === "*")
21
- return !0;
16
+ if (t === "*") return !0;
22
17
  if (t.startsWith("*.")) {
23
18
  const u = t.slice(2);
24
19
  return n === u || n.endsWith("." + u);
25
20
  }
26
21
  return t === n;
27
- }(a, e.hostname) && !!function(t, n) {
22
+ }(o, e.hostname) && !!function(t, n) {
28
23
  const u = t.replace(/[.+^${}()|[\]\\]/g, "\\$&").replace(/\*/g, ".*");
29
24
  return new RegExp(`^${u}$`, "i").test(n);
30
25
  }(r, e.pathname);
31
26
  }
32
27
  function h(s) {
33
28
  const { hostMatch: i, extensionId: e, isDev: c = !1 } = s;
34
- chrome.tabs.onUpdated.addListener(async (o, a, r) => {
35
- r && r.url && r && r.url && a && a.status === "loading" && i.some((t) => l(t, r.url)) && chrome.scripting.executeScript({ injectImmediately: !0, world: "MAIN", target: { tabId: o, allFrames: !0 }, func: (t) => {
29
+ chrome.tabs.onUpdated.addListener(async (a, o, r) => {
30
+ r && r.url && r && r.url && o && o.status === "loading" && i.some((t) => l(t, r.url)) && chrome.scripting.executeScript({ injectImmediately: !0, world: "MAIN", target: { tabId: a, allFrames: !0 }, func: (t) => {
36
31
  window[t.slug] = t;
37
32
  }, args: [{ id: chrome.runtime.id, slug: e, isDev: c }] });
38
33
  });
39
34
  }
40
35
  export {
41
- h as injectExtensionData
36
+ h as injectExtensionData,
37
+ l as matchesPattern
42
38
  };
package/dist/inject.js CHANGED
@@ -1,6 +1,6 @@
1
1
  var d = Object.defineProperty;
2
2
  var r = (i, s, e) => s in i ? d(i, s, { enumerable: !0, configurable: !0, writable: !0, value: e }) : i[s] = e;
3
- var n = (i, s, e) => (r(i, typeof s != "symbol" ? s + "" : s, e), e);
3
+ var n = (i, s, e) => r(i, typeof s != "symbol" ? s + "" : s, e);
4
4
  class h {
5
5
  constructor(s, e) {
6
6
  n(this, "id");
@@ -40,9 +40,7 @@ class h {
40
40
  _handleReceivingMessage(s) {
41
41
  if (s.data && s.data.from === this.slug && s.data.payload) {
42
42
  const { action: e, payload: t } = s.data.payload;
43
- if (this.isDebugger && console.log("收到消息", s.data), this.listeners[e])
44
- for (let o of this.listeners[e])
45
- o(...t);
43
+ if (this.isDebugger && console.log("收到消息", s.data), this.listeners[e]) for (let o of this.listeners[e]) o(...t);
46
44
  }
47
45
  }
48
46
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@webextkits/messages-center",
3
3
  "private": false,
4
- "version": "0.1.5",
4
+ "version": "0.1.7",
5
5
  "type": "module",
6
6
  "exports": {
7
7
  "./background": "./dist/background.js",
@@ -34,7 +34,7 @@
34
34
  "esno": "^0.17.0",
35
35
  "glob": "^10.3.10",
36
36
  "typescript": "^5.0.2",
37
- "vite": "^4.4.5"
37
+ "vite": "=5.4.10"
38
38
  },
39
39
  "scripts": {
40
40
  "build": "esno ./scripts/build.ts && npm run build:type",