webext-messenger 0.25.1 → 0.26.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -128,9 +128,16 @@ async function manageMessage(type, target, seq, retry, sendMessage) {
128
128
  log.debug(type, seq, "↘️ replied successfully", response.value);
129
129
  return response.value;
130
130
  }
131
+ // Not a UID nor a truly global sequence. Signal / console noise compromise.
132
+ // The time part is a pseudo-random number between 0 and 99 that helps visually
133
+ // group messages from the same context. Keeping it a number also gives it a different
134
+ // color in the console log.
135
+ // Example log when seen in the background page:
136
+ // Tab 1 sends: 33000, 33001, 33002
137
+ // Tab 2 sends: 12000, 12001, 12002
138
+ let globalSeq = (Date.now() % 100) * 10000;
131
139
  function messenger(type, options, target, ...args) {
132
- // Not a UID. Signal / console noise compromise. They repeat every 100 seconds
133
- options.seq = Date.now() % 100000;
140
+ options.seq = globalSeq++;
134
141
  const { seq } = options;
135
142
  // Message goes to extension page
136
143
  if ("page" in target) {
@@ -160,9 +167,11 @@ function messenger(type, options, target, ...args) {
160
167
  // Message tab directly
161
168
  return manageConnection(type, options, target, async (attemptCount) => {
162
169
  log.debug(type, seq, "↗️ sending message to tab", tabId, "frame", frameId, attemptLog(attemptCount));
163
- return browser.tabs.sendMessage(tabId, makeMessage(type, args, target, options), {
164
- frameId,
165
- });
170
+ return browser.tabs.sendMessage(tabId, makeMessage(type, args, target, options), frameId === "allFrames"
171
+ ? {}
172
+ : {
173
+ frameId,
174
+ });
166
175
  });
167
176
  }
168
177
  function getMethod(type, target) {
@@ -1,4 +1,4 @@
1
- import { isBackground, isContentScript, isExtensionContext, } from "webext-detect-page";
1
+ import { getContextName, isBackground, isContentScript, isExtensionContext, } from "webext-detect-page";
2
2
  import { messenger } from "./sender.js";
3
3
  import { registerMethods } from "./receiver.js";
4
4
  import { MessengerError, once } from "./shared.js";
@@ -112,11 +112,17 @@ const storeTabData = once(async () => {
112
112
  export function __getTabData() {
113
113
  return { tabId: this.trace[0]?.tab?.id, frameId: this.trace[0]?.frameId };
114
114
  }
115
+ // TODO: Add tests
115
116
  export async function getThisFrame() {
116
117
  await storeTabData(); // It should already have been called but we still need to await it
117
118
  const { tabId, frameId } = thisTarget;
118
119
  if (typeof tabId !== "number" || typeof frameId !== "number") {
119
- throw new TypeError("This target is not in a frame");
120
+ let moreInfo = "(error retrieving context information)";
121
+ try {
122
+ moreInfo = `(context: ${getContextName()}, url: ${globalThis.location?.href})`;
123
+ }
124
+ catch { }
125
+ throw new TypeError(`This target is not in a frame ${moreInfo}`);
120
126
  }
121
127
  // Rebuild object to return exactly these two properties and nothing more
122
128
  return { tabId, frameId };
@@ -57,7 +57,7 @@ export type MessengerMessage = Message & {
57
57
  };
58
58
  export interface AnyTarget {
59
59
  tabId?: number | "this";
60
- frameId?: number;
60
+ frameId?: number | "allFrames";
61
61
  page?: string;
62
62
  }
63
63
  export interface TopLevelFrame {
@@ -75,7 +75,7 @@ export interface KnownTarget {
75
75
  }
76
76
  export interface Target {
77
77
  tabId: number;
78
- frameId?: number;
78
+ frameId?: number | "allFrames";
79
79
  }
80
80
  export interface PageTarget {
81
81
  tabId?: number | "this";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "webext-messenger",
3
- "version": "0.25.1",
3
+ "version": "0.26.0",
4
4
  "description": "Browser Extension component messaging framework",
5
5
  "keywords": [],
6
6
  "repository": "pixiebrix/webext-messenger",