webext-messenger 0.15.0-4 → 0.15.0-5

Sign up to get free protection for your applications and to get access to all the features.
@@ -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 } from "webext-detect-page";
5
+ import { getContextName, isBackgroundPage } from "webext-detect-page";
6
6
  import { getActionForMessage, nameThisTarget } from "./thisTarget.js";
7
7
  export function isMessengerMessage(message) {
8
8
  return (isObject(message) &&
@@ -25,7 +25,9 @@ function onMessageListener(message, sender) {
25
25
  }
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
- async function handleMessage(message, sender, action) {
28
+ async function handleMessage(message, sender,
29
+ // Once messages reach handleMessage they cannot be "ignored", they're already being handled
30
+ action) {
29
31
  const { type, target, args, options: { trace } = {} } = message;
30
32
  debug(type, "↘️ received", { sender, args });
31
33
  let handleMessage;
@@ -54,7 +56,9 @@ async function handleMessage(message, sender, action) {
54
56
  });
55
57
  }
56
58
  export function registerMethods(methods) {
57
- void nameThisTarget();
59
+ if (!isBackgroundPage()) {
60
+ void nameThisTarget();
61
+ }
58
62
  for (const [type, method] of Object.entries(methods)) {
59
63
  if (handlers.has(type)) {
60
64
  throw new MessengerError(`Handler already set for ${type}`);
@@ -17,15 +17,15 @@ function makeMessage(type, args, target, options) {
17
17
  };
18
18
  }
19
19
  // Do not turn this into an `async` function; Notifications must turn `void`
20
- function manageConnection(type, options, sendMessage) {
20
+ function manageConnection(type, options, target, sendMessage) {
21
21
  if (!options.isNotification) {
22
- return manageMessage(type, sendMessage);
22
+ return manageMessage(type, target, sendMessage);
23
23
  }
24
24
  void sendMessage().catch((error) => {
25
25
  debug(type, "notification failed", { error });
26
26
  });
27
27
  }
28
- async function manageMessage(type, sendMessage) {
28
+ async function manageMessage(type, target, sendMessage) {
29
29
  const response = await pRetry(async () => {
30
30
  const response = await sendMessage();
31
31
  if (!isMessengerResponse(response)) {
@@ -37,7 +37,9 @@ async function manageMessage(type, sendMessage) {
37
37
  factor: 1.3,
38
38
  maxRetryTime: 4000,
39
39
  onFailedAttempt(error) {
40
- if (error instanceof MessengerError ||
40
+ if (
41
+ // Don't retry sending to the background page unless it really hasn't loaded yet
42
+ (target.page !== "background" && error instanceof MessengerError) ||
41
43
  String(error.message).startsWith(errorNonExistingTarget)) {
42
44
  debug(type, "will retry. Attempt", error.attemptNumber);
43
45
  }
@@ -68,11 +70,11 @@ function messenger(type, options, target, ...args) {
68
70
  debug(type, "↗️ sending message to runtime");
69
71
  return browser.runtime.sendMessage(makeMessage(type, args, target, options));
70
72
  };
71
- return manageConnection(type, options, sendMessage);
73
+ return manageConnection(type, options, target, sendMessage);
72
74
  }
73
75
  // Contexts without direct Tab access must go through background
74
76
  if (!browser.tabs) {
75
- return manageConnection(type, options, async () => {
77
+ return manageConnection(type, options, target, async () => {
76
78
  debug(type, "↗️ sending message to runtime");
77
79
  return browser.runtime.sendMessage(makeMessage(type, args, target, options));
78
80
  });
@@ -80,7 +82,7 @@ function messenger(type, options, target, ...args) {
80
82
  // `frameId` must be specified. If missing, the message is sent to every frame
81
83
  const { tabId, frameId = 0 } = target;
82
84
  // Message tab directly
83
- return manageConnection(type, options, async () => {
85
+ return manageConnection(type, options, target, async () => {
84
86
  debug(type, "↗️ sending message to tab", tabId, "frame", frameId);
85
87
  return browser.tabs.sendMessage(tabId, makeMessage(type, args, target, options), {
86
88
  frameId,
@@ -1,9 +1,4 @@
1
- import { MessengerMeta, Sender } from "./types.js";
2
- interface AnyTarget {
3
- tabId?: number | "this";
4
- frameId?: number;
5
- page?: string;
6
- }
1
+ import { AnyTarget, MessengerMeta, Sender } from "./types.js";
7
2
  export declare function getActionForMessage(from: Sender, { ...to }: AnyTarget): "respond" | "forward" | "ignore";
8
3
  export declare function nameThisTarget(): Promise<void>;
9
4
  declare function __getTabData(this: MessengerMeta): AnyTarget;
@@ -53,11 +53,11 @@ function __getTabData() {
53
53
  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
54
  }
55
55
  export function initPrivateApi() {
56
+ if (isBackgroundPage()) {
57
+ thisTarget = { page: "background" };
58
+ }
56
59
  if (isExtensionContext()) {
57
60
  // Any context can handler this message
58
61
  registerMethods({ __getTabData });
59
62
  }
60
- if (isBackgroundPage()) {
61
- thisTarget = { page: "background" };
62
- }
63
63
  }
@@ -45,6 +45,11 @@ export declare type MessengerMessage = Message & {
45
45
  /** Guarantees that a message is meant to be handled by this library */
46
46
  __webextMessenger: true;
47
47
  };
48
+ export interface AnyTarget {
49
+ tabId?: number | "this";
50
+ frameId?: number;
51
+ page?: string;
52
+ }
48
53
  export interface Target {
49
54
  tabId: number;
50
55
  frameId?: number;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "webext-messenger",
3
- "version": "0.15.0-4",
3
+ "version": "0.15.0-5",
4
4
  "description": "Browser Extension component messaging framework",
5
5
  "keywords": [],
6
6
  "repository": "pixiebrix/webext-messenger",