webext-messenger 0.20.0 → 0.20.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,8 +1,4 @@
1
- // Imports must use the .js extension because of ESM requires it and TS refuses to rewrite .ts to .js
2
- // This works in TS even if the .js doesn't exist, but it breaks Parcel (the tests builder)
3
- // For this reason, there's an `alias` field in package.json to redirect these imports.
4
- // If you see "@parcel/resolver-default: Cannot load file './yourNewFile.js'" you need to add it to the `alias` list
5
- // 🥲
1
+ // Imports must use the .js extension because ESM requires it and TS refuses to rewrite .ts to .js
6
2
  export * from "./receiver.js";
7
3
  export * from "./sender.js";
8
4
  export * from "./types.js";
@@ -43,11 +43,13 @@ action) {
43
43
  args,
44
44
  wasForwarded: trace.length > 1,
45
45
  });
46
- if (!didUserRegisterMethods()) {
47
- throw new MessengerError(`No handlers registered in ${getContextName()}`);
48
- }
49
46
  const localHandler = handlers.get(type);
50
47
  if (!localHandler) {
48
+ if (!didUserRegisterMethods()) {
49
+ // TODO: Test the handling of __getTabData in contexts that have no registered methods
50
+ // https://github.com/pixiebrix/webext-messenger/pull/82
51
+ throw new MessengerError(`No handlers registered in ${getContextName()}`);
52
+ }
51
53
  throw new MessengerError(`No handler registered for ${type} in ${getContextName()}`);
52
54
  }
53
55
  handleMessage = async () => localHandler.apply(meta, args);
@@ -5,7 +5,9 @@ import { debug } from "./shared.js";
5
5
  // Soft warning: Race conditions are possible.
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
- let thisTarget;
8
+ let thisTarget = isBackground()
9
+ ? { page: "background" }
10
+ : undefined;
9
11
  function compareTargets(to, thisTarget) {
10
12
  for (const [key, value] of Object.entries(to)) {
11
13
  if (thisTarget[key] === value) {
@@ -70,11 +72,10 @@ export function __getTabData() {
70
72
  return { tabId: this.trace[0]?.tab?.id, frameId: this.trace[0]?.frameId };
71
73
  }
72
74
  export function initPrivateApi() {
73
- if (isBackground()) {
74
- thisTarget = { page: "background" };
75
- }
76
75
  if (isExtensionContext()) {
77
- // Any context can handler this message
76
+ // Only `runtime` pages can handle this message but I can't remove it because its listener
77
+ // also serves the purpose of throwing a specific error when no methods have been registered.
78
+ // https://github.com/pixiebrix/webext-messenger/pull/80
78
79
  registerMethods({ __getTabData });
79
80
  }
80
81
  }
@@ -40,7 +40,9 @@ export declare type Message<LocalArguments extends Arguments = Arguments> = {
40
40
  /** If the message is being sent to an intermediary receiver, also set the options */
41
41
  options?: Options;
42
42
  };
43
- export declare type Sender = Runtime.MessageSender;
43
+ export declare type Sender = Runtime.MessageSender & {
44
+ origin?: string;
45
+ };
44
46
  export declare type MessengerMessage = Message & {
45
47
  /** Guarantees that a message is meant to be handled by this library */
46
48
  __webextMessenger: true;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "webext-messenger",
3
- "version": "0.20.0",
3
+ "version": "0.20.1",
4
4
  "description": "Browser Extension component messaging framework",
5
5
  "keywords": [],
6
6
  "repository": "pixiebrix/webext-messenger",