@webextkits/messages-center 0.1.2 → 0.1.5

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,13 +1,12 @@
1
- var l = Object.defineProperty;
2
- var b = (n, e, t) => e in n ? l(n, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : n[e] = t;
3
- var a = (n, e, t) => (b(n, typeof e != "symbol" ? e + "" : e, t), t);
4
- import { tabs as d, runtime as h } from "webextension-polyfill";
5
- class u {
1
+ var o = Object.defineProperty;
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);
4
+ class h {
6
5
  constructor(e, t) {
7
- a(this, "listeners", {});
8
- a(this, "tabs", []);
9
- a(this, "slug");
10
- a(this, "isDebugger");
6
+ n(this, "listeners", {});
7
+ n(this, "tabs", []);
8
+ n(this, "slug");
9
+ n(this, "isDebugger");
11
10
  this.slug = e, this.isDebugger = !!t, this.bindMessage(), this.bindEvent();
12
11
  }
13
12
  on(e, t) {
@@ -21,14 +20,14 @@ class u {
21
20
  this.isDebugger && console.log("发送消息", { action: e, payload: t, tabs: this.tabs });
22
21
  for (let s of this.tabs)
23
22
  try {
24
- const i = await d.get(s);
25
- i && i.id ? d.sendMessage(i.id, { from: this.slug, payload: { action: e, payload: t } }) : this.deleteTabById(s);
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);
26
25
  } catch (i) {
27
26
  console.log(i), this.deleteTabById(s);
28
27
  }
29
28
  }
30
29
  bindMessage() {
31
- h.onMessageExternal.addListener((e, t, s) => {
30
+ chrome.runtime.onMessageExternal.addListener((e, t, s) => {
32
31
  var i, r;
33
32
  if ((i = t == null ? void 0 : t.tab) != null && i.id) {
34
33
  if (this.isDebugger && console.log("收到消息", { request: e, sender: t }), e.action === "__connect")
@@ -36,14 +35,14 @@ class u {
36
35
  if (e.action === "__disconnect")
37
36
  return this.deleteTabById(t.tab.id), s();
38
37
  if (this.listeners[e.action])
39
- 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((o) => {
40
- this.isDebugger && console.log("send to callback from background", { request: e, sender: t, res: o }), s(o);
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);
41
40
  }), !0) : s();
42
41
  }
43
42
  });
44
43
  }
45
44
  bindEvent() {
46
- d.onRemoved.addListener((e) => {
45
+ chrome.tabs.onRemoved.addListener((e) => {
47
46
  this.deleteTabById(e);
48
47
  });
49
48
  }
@@ -55,5 +54,5 @@ class u {
55
54
  }
56
55
  }
57
56
  export {
58
- u as MessageInstance
57
+ h as MessageInstance
59
58
  };
@@ -1,6 +1,5 @@
1
- import * as r from "webextension-polyfill";
2
1
  function n(o, s) {
3
- r.runtime.onMessage.addListener((e, a) => {
2
+ chrome.runtime.onMessage.addListener((e, a) => {
4
3
  e.from === o && (s && console.log("received message from background script", e), window.postMessage({ from: e.from, payload: e.payload }));
5
4
  });
6
5
  }
package/dist/hooks.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export declare function injectExtensionData(payload: {
2
2
  hostMatch: string[];
3
3
  extensionId: string;
4
+ isDev?: boolean;
4
5
  }): void;
package/dist/hooks.js CHANGED
@@ -1,15 +1,42 @@
1
- import { tabs as c, runtime as s, scripting as l } from "webextension-polyfill";
2
- function f(i) {
3
- const { hostMatch: a, extensionId: o } = i;
4
- c.onUpdated.addListener(async (d, e, t) => {
5
- if (t && t.url && t && t.url && e && e.status === "loading") {
6
- const r = new URL(t.url).hostname;
7
- a.find((n) => !!~n.indexOf(r)) && (s.getManifest(), l.executeScript({ injectImmediately: !0, world: "MAIN", target: { tabId: d, allFrames: !0 }, func: (n) => {
8
- window[n.slug] = n;
9
- }, args: [{ id: s.id, slug: o }] }));
1
+ function l(s, i) {
2
+ if (s === "<all_urls>")
3
+ return /^(https?|file):\/\//.test(i);
4
+ let e;
5
+ try {
6
+ e = new URL(i);
7
+ } catch {
8
+ return !1;
9
+ }
10
+ 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;
19
+ return !!function(t, n) {
20
+ if (t === "*")
21
+ return !0;
22
+ if (t.startsWith("*.")) {
23
+ const u = t.slice(2);
24
+ return n === u || n.endsWith("." + u);
10
25
  }
26
+ return t === n;
27
+ }(a, e.hostname) && !!function(t, n) {
28
+ const u = t.replace(/[.+^${}()|[\]\\]/g, "\\$&").replace(/\*/g, ".*");
29
+ return new RegExp(`^${u}$`, "i").test(n);
30
+ }(r, e.pathname);
31
+ }
32
+ function h(s) {
33
+ 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) => {
36
+ window[t.slug] = t;
37
+ }, args: [{ id: chrome.runtime.id, slug: e, isDev: c }] });
11
38
  });
12
39
  }
13
40
  export {
14
- f as injectExtensionData
41
+ h as injectExtensionData
15
42
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@webextkits/messages-center",
3
3
  "private": false,
4
- "version": "0.1.2",
4
+ "version": "0.1.5",
5
5
  "type": "module",
6
6
  "exports": {
7
7
  "./background": "./dist/background.js",
@@ -34,8 +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",
38
- "webextension-polyfill": "^0.10.0"
37
+ "vite": "^4.4.5"
39
38
  },
40
39
  "scripts": {
41
40
  "build": "esno ./scripts/build.ts && npm run build:type",