webext-messenger 0.15.0-5 → 0.17.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.
@@ -2,7 +2,7 @@ import browser from "webextension-polyfill";
2
2
  import { serializeError } from "serialize-error";
3
3
  import { messenger } from "./sender.js";
4
4
  import { handlers, isObject, MessengerError, debug, __webextMessenger, } from "./shared.js";
5
- import { getContextName, isBackgroundPage } from "webext-detect-page";
5
+ import { getContextName, isBackground } from "webext-detect-page";
6
6
  import { getActionForMessage, nameThisTarget } from "./thisTarget.js";
7
7
  export function isMessengerMessage(message) {
8
8
  return (isObject(message) &&
@@ -16,7 +16,7 @@ function onMessageListener(message, sender) {
16
16
  // TODO: Add test for this eventuality: ignore unrelated messages
17
17
  return;
18
18
  }
19
- // Target check must be synchronous (`await` means we're handing the message)
19
+ // Target check must be synchronous (`await` means we're handling the message)
20
20
  const action = getActionForMessage(sender, message.target);
21
21
  if (action === "ignore") {
22
22
  return;
@@ -26,37 +26,36 @@ function onMessageListener(message, sender) {
26
26
  // This function can only be called when the message *will* be handled locally.
27
27
  // Returning "undefined" or throwing an error will still handle it.
28
28
  async function handleMessage(message, sender,
29
- // Once messages reach handleMessage they cannot be "ignored", they're already being handled
29
+ // Once messages reach this function they cannot be "ignored", they're already being handled
30
30
  action) {
31
- const { type, target, args, options: { trace } = {} } = message;
32
- debug(type, "↘️ received", { sender, args });
31
+ const { type, target, args, options = {} } = message;
32
+ const { trace = [] } = options;
33
+ trace.push(sender);
34
+ const meta = { trace };
35
+ debug(type, "↘️ received", { sender, args, wasForwarded: trace.length > 1 });
33
36
  let handleMessage;
34
37
  if (action === "forward") {
35
38
  debug(type, "🔀 forwarded", { sender, target });
36
- handleMessage = async () => messenger(type, { trace }, target, ...args);
39
+ handleMessage = async () => messenger(type, meta, target, ...args);
37
40
  }
38
41
  else {
39
42
  const localHandler = handlers.get(type);
40
43
  if (!localHandler) {
41
44
  throw new MessengerError(`No handler registered for ${type} in ${getContextName()}`);
42
45
  }
43
- debug(type, "➡️ will be handled here");
44
- const meta = { trace: [sender] };
46
+ debug(type, "➡️ will be handled here,", getContextName());
45
47
  handleMessage = async () => localHandler.apply(meta, args);
46
48
  }
47
- return handleMessage()
48
- .then((value) => ({ value }), (error) => ({
49
- // Errors must be serialized because the stacktraces are currently lost on Chrome
49
+ const response = await handleMessage().then((value) => ({ value }), (error) => ({
50
+ // Errors must be serialized because the stack traces are currently lost on Chrome
50
51
  // and https://github.com/mozilla/webextension-polyfill/issues/210
51
52
  error: serializeError(error),
52
- }))
53
- .then((response) => {
54
- debug(type, "↗️ responding", response);
55
- return { ...response, __webextMessenger };
56
- });
53
+ }));
54
+ debug(type, "↗️ responding", response);
55
+ return { ...response, __webextMessenger };
57
56
  }
58
57
  export function registerMethods(methods) {
59
- if (!isBackgroundPage()) {
58
+ if (!isBackground()) {
60
59
  void nameThisTarget();
61
60
  }
62
61
  for (const [type, method] of Object.entries(methods)) {
@@ -1,6 +1,6 @@
1
1
  import browser from "webextension-polyfill";
2
2
  import pRetry from "p-retry";
3
- import { isBackgroundPage } from "webext-detect-page";
3
+ import { isBackground } from "webext-detect-page";
4
4
  import { deserializeError } from "serialize-error";
5
5
  import { isObject, MessengerError, __webextMessenger, handlers, debug, warn, } from "./shared.js";
6
6
  export const errorNonExistingTarget = "Could not establish connection. Receiving end does not exist.";
@@ -58,7 +58,7 @@ async function manageMessage(type, target, sendMessage) {
58
58
  function messenger(type, options, target, ...args) {
59
59
  // Message goes to extension page
60
60
  if ("page" in target) {
61
- if (target.page === "background" && isBackgroundPage()) {
61
+ if (target.page === "background" && isBackground()) {
62
62
  const handler = handlers.get(type);
63
63
  if (handler) {
64
64
  warn(type, "is being handled locally");
@@ -1,4 +1,4 @@
1
- import { isBackgroundPage, isContentScript, isExtensionContext, } from "webext-detect-page";
1
+ import { isBackground, isContentScript, isExtensionContext, } from "webext-detect-page";
2
2
  import { messenger } from "./sender.js";
3
3
  import { registerMethods } from "./receiver.js";
4
4
  import { debug } from "./shared.js";
@@ -6,6 +6,27 @@ import { debug } from "./shared.js";
6
6
  // This CANNOT be awaited because waiting for it means "I will handle the message."
7
7
  // If a message is received before this is ready, it will just have to be ignored.
8
8
  let thisTarget;
9
+ function compareTargets(to, thisTarget) {
10
+ for (const [key, value] of Object.entries(to)) {
11
+ if (thisTarget[key] === value) {
12
+ continue;
13
+ }
14
+ if (key !== "page") {
15
+ return false;
16
+ }
17
+ const toUrl = new URL(to.page, location.origin);
18
+ const thisUrl = new URL(thisTarget.page, location.origin);
19
+ if (toUrl.pathname !== thisUrl.pathname) {
20
+ return false;
21
+ }
22
+ for (const [parameterKey, parameterValue] of toUrl.searchParams) {
23
+ if (thisUrl.searchParams.get(parameterKey) !== parameterValue) {
24
+ return false;
25
+ }
26
+ }
27
+ }
28
+ return true;
29
+ }
9
30
  export function getActionForMessage(from, { ...to } // Clone object because we're editing it
10
31
  ) {
11
32
  var _a;
@@ -26,14 +47,12 @@ export function getActionForMessage(from, { ...to } // Clone object because we'r
26
47
  // If this *was* the target, then probably no one else answered
27
48
  return "ignore";
28
49
  }
29
- // If requests "this" tab, then set it to allow the next condition
50
+ // Set "this" tab to the current tabId
30
51
  if (to.tabId === "this" && thisTarget.tabId === ((_a = from.tab) === null || _a === void 0 ? void 0 : _a.id)) {
31
52
  to.tabId = thisTarget.tabId;
32
53
  }
33
54
  // Every `target` key must match `thisTarget`
34
- const isThisTarget = Object.entries(to).every(
35
- // @ts-expect-error Optional properties
36
- ([key, value]) => thisTarget[key] === value);
55
+ const isThisTarget = compareTargets(to, thisTarget);
37
56
  if (!isThisTarget) {
38
57
  debug("The message’s target is", to, "but this is", thisTarget);
39
58
  }
@@ -45,7 +64,7 @@ export async function nameThisTarget() {
45
64
  if (!nameRequested && !thisTarget && !isContentScript()) {
46
65
  nameRequested = true;
47
66
  thisTarget = await messenger("__getTabData", {}, { page: "any" });
48
- thisTarget.page = location.pathname;
67
+ thisTarget.page = location.pathname + location.search;
49
68
  }
50
69
  }
51
70
  function __getTabData() {
@@ -53,7 +72,7 @@ function __getTabData() {
53
72
  return { tabId: (_b = (_a = this.trace[0]) === null || _a === void 0 ? void 0 : _a.tab) === null || _b === void 0 ? void 0 : _b.id, frameId: (_c = this.trace[0]) === null || _c === void 0 ? void 0 : _c.frameId };
54
73
  }
55
74
  export function initPrivateApi() {
56
- if (isBackgroundPage()) {
75
+ if (isBackground()) {
57
76
  thisTarget = { page: "background" };
58
77
  }
59
78
  if (isExtensionContext()) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "webext-messenger",
3
- "version": "0.15.0-5",
3
+ "version": "0.17.0-0",
4
4
  "description": "Browser Extension component messaging framework",
5
5
  "keywords": [],
6
6
  "repository": "pixiebrix/webext-messenger",
@@ -107,19 +107,19 @@
107
107
  "dependencies": {
108
108
  "p-retry": "^5.0.0",
109
109
  "serialize-error": "^9.0.0",
110
- "type-fest": "^2.6.0",
111
- "webext-detect-page": "^3.1.0",
110
+ "type-fest": "^2.8.0",
111
+ "webext-detect-page": "^4.0.0",
112
112
  "webextension-polyfill": "^0.8.0"
113
113
  },
114
114
  "devDependencies": {
115
115
  "@parcel/config-webextension": "^2.0.1",
116
116
  "@sindresorhus/tsconfig": "^2.0.0",
117
- "@types/chrome": "^0.0.164",
117
+ "@types/chrome": "^0.0.171",
118
118
  "@types/tape": "^4.13.2",
119
119
  "@types/webextension-polyfill": "^0.8.2",
120
- "@typescript-eslint/eslint-plugin": "^5.4.0",
121
- "@typescript-eslint/parser": "^5.4.0",
122
- "eslint": "^8.3.0",
120
+ "@typescript-eslint/eslint-plugin": "^5.7.0",
121
+ "@typescript-eslint/parser": "^5.7.0",
122
+ "eslint": "^8.4.1",
123
123
  "eslint-config-prettier": "^8.3.0",
124
124
  "eslint-config-xo": "^0.39.0",
125
125
  "eslint-config-xo-typescript": "^0.47.1",
@@ -128,8 +128,8 @@
128
128
  "npm-run-all": "^4.1.5",
129
129
  "parcel": "^2.0.1",
130
130
  "tape": "^5.3.2",
131
- "typescript": "^4.5.2",
132
- "webext-content-scripts": "^0.10.1"
131
+ "typescript": "^4.5.4",
132
+ "webext-content-scripts": "^0.12.0"
133
133
  },
134
134
  "targets": {
135
135
  "main": false,