@webext-core/messaging 2.1.0 → 2.3.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.
package/README.md CHANGED
@@ -4,10 +4,10 @@ A light-weight, type-safe wrapper around the `browser.runtime` messaging APIs. S
4
4
 
5
5
  ```ts
6
6
  // ./messaging.ts
7
- import { ProtocolWithReturn, defineExtensionMessaging } from '@webext-core/messaging';
7
+ import { defineExtensionMessaging } from '@webext-core/messaging';
8
8
 
9
9
  interface ProtocolMap {
10
- getStringLength: ProtocolWithReturn<string, number>;
10
+ getStringLength(s: string): number;
11
11
  }
12
12
 
13
13
  export const { sendMessage, onMessage } = defineExtensionMessaging<ProtocolMap>();
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Interface used to log text to the console when sending and recieving messages.
2
+ * Interface used to log text to the console when sending and receiving messages.
3
3
  */
4
4
  interface Logger {
5
5
  debug(...args: any[]): void;
@@ -76,7 +76,7 @@ interface NamespaceMessagingConfig extends BaseMessagingConfig {
76
76
  namespace: string;
77
77
  }
78
78
  /**
79
- * Contains information about the message recieved.
79
+ * Contains information about the message received.
80
80
  */
81
81
  interface Message<TProtocolMap extends Record<string, any>, TType extends keyof TProtocolMap> {
82
82
  /**
@@ -114,15 +114,16 @@ interface GenericMessenger<TProtocolMap extends Record<string, any>, TMessageExt
114
114
  * @param data The data to send with the message.
115
115
  * @param args Different messengers will have additional arguments to configure how a message gets sent.
116
116
  */
117
+ sendMessage<TType extends keyof TProtocolMap>(type: TType, ...args: GetDataType<TProtocolMap[TType]> extends undefined ? [data?: undefined, ...args: TSendMessageArgs] : never): Promise<GetReturnType<TProtocolMap[TType]>>;
117
118
  sendMessage<TType extends keyof TProtocolMap>(type: TType, data: GetDataType<TProtocolMap[TType]>, ...args: TSendMessageArgs): Promise<GetReturnType<TProtocolMap[TType]>>;
118
119
  /**
119
- * Trigger a callback when a message of the requested type is recieved. You cannot setup multiple
120
+ * Trigger a callback when a message of the requested type is received. You cannot setup multiple
120
121
  * listeners for the same message type in the same JS context.
121
122
  *
122
123
  * To remove the listener, call the returned message.
123
124
  *
124
- * @param type The message type to listen for. Call `sendMessage` with the same type to triggern this listener.
125
- * @param onReceived The callback executed when a message is recieved.
125
+ * @param type The message type to listen for. Call `sendMessage` with the same type to trigger this listener.
126
+ * @param onReceived The callback executed when a message is received.
126
127
  */
127
128
  onMessage<TType extends keyof TProtocolMap>(type: TType, onReceived: (message: Message<TProtocolMap, TType> & TMessageExtension) => void | MaybePromise<GetReturnType<TProtocolMap[TType]>>): RemoveListenerCallback;
128
129
  /**
package/lib/index.cjs CHANGED
@@ -174,10 +174,17 @@ function defineGenericMessanging(config) {
174
174
  // src/extension.ts
175
175
  function defineExtensionMessaging(config) {
176
176
  return defineGenericMessanging(__spreadProps(__spreadValues({}, config), {
177
- sendMessage(message, tabId) {
178
- if (tabId == null)
177
+ sendMessage(message, arg) {
178
+ if (arg == null) {
179
179
  return import_webextension_polyfill.default.runtime.sendMessage(message);
180
- return import_webextension_polyfill.default.tabs.sendMessage(tabId, message);
180
+ }
181
+ const options = typeof arg === "number" ? { tabId: arg } : arg;
182
+ return import_webextension_polyfill.default.tabs.sendMessage(
183
+ options.tabId,
184
+ message,
185
+ // Pass frameId if specified
186
+ options.frameId != null ? { frameId: options.frameId } : void 0
187
+ );
181
188
  },
182
189
  addRootListener(processMessage) {
183
190
  const listener = (message, sender) => {
package/lib/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { B as BaseMessagingConfig, G as GenericMessenger } from './generic-ad5087a0.js';
2
- export { a as GetDataType, b as GetReturnType, L as Logger, M as MaybePromise, c as Message, N as NamespaceMessagingConfig, P as ProtocolWithReturn, R as RemoveListenerCallback } from './generic-ad5087a0.js';
1
+ import { B as BaseMessagingConfig, G as GenericMessenger } from './generic-683db69b.js';
2
+ export { a as GetDataType, b as GetReturnType, L as Logger, M as MaybePromise, c as Message, N as NamespaceMessagingConfig, P as ProtocolWithReturn, R as RemoveListenerCallback } from './generic-683db69b.js';
3
3
  import { Runtime } from 'webextension-polyfill';
4
4
 
5
5
  /**
@@ -18,17 +18,27 @@ interface ExtensionMessage {
18
18
  sender: Runtime.MessageSender;
19
19
  }
20
20
  /**
21
- * Send messsage accepts an additional, optional argument `tabId`. Pass it to send a message to a
22
- * specific tab from the background script.
23
- *
24
- * You cannot message between tabs directly. It must go through the background script.
21
+ * Options for sending a message to a specific tab/frame
25
22
  */
26
- type ExtensionSendMessageArgs = [
23
+ interface SendMessageOptions {
27
24
  /**
28
- * The tab to send a message to.
25
+ * The tab to send a message to
29
26
  */
30
- tabId?: number
31
- ];
27
+ tabId: number;
28
+ /**
29
+ * The frame to send a message to. 0 represents the main frame.
30
+ */
31
+ frameId?: number;
32
+ }
33
+ /**
34
+ * Send message accepts either:
35
+ * - No arguments to send to background
36
+ * - A tabId number to send to a specific tab
37
+ * - A SendMessageOptions object to target a specific tab and frame
38
+ *
39
+ * You cannot message between tabs directly. It must go through the background script.
40
+ */
41
+ type ExtensionSendMessageArgs = [arg?: number | SendMessageOptions];
32
42
  /**
33
43
  * Messenger returned by `defineExtensionMessaging`.
34
44
  */
@@ -41,4 +51,4 @@ type ExtensionMessenger<TProtocolMap extends Record<string, any>> = GenericMesse
41
51
  */
42
52
  declare function defineExtensionMessaging<TProtocolMap extends Record<string, any> = Record<string, any>>(config?: ExtensionMessagingConfig): ExtensionMessenger<TProtocolMap>;
43
53
 
44
- export { BaseMessagingConfig, ExtensionMessage, ExtensionMessagingConfig, ExtensionMessenger, ExtensionSendMessageArgs, defineExtensionMessaging };
54
+ export { BaseMessagingConfig, ExtensionMessage, ExtensionMessagingConfig, ExtensionMessenger, ExtensionSendMessageArgs, SendMessageOptions, defineExtensionMessaging };
@@ -1363,10 +1363,17 @@ var webExtCoreMessaging = (() => {
1363
1363
  // src/extension.ts
1364
1364
  function defineExtensionMessaging(config) {
1365
1365
  return defineGenericMessanging(__spreadProps(__spreadValues({}, config), {
1366
- sendMessage(message, tabId) {
1367
- if (tabId == null)
1366
+ sendMessage(message, arg) {
1367
+ if (arg == null) {
1368
1368
  return import_webextension_polyfill.default.runtime.sendMessage(message);
1369
- return import_webextension_polyfill.default.tabs.sendMessage(tabId, message);
1369
+ }
1370
+ const options = typeof arg === "number" ? { tabId: arg } : arg;
1371
+ return import_webextension_polyfill.default.tabs.sendMessage(
1372
+ options.tabId,
1373
+ message,
1374
+ // Pass frameId if specified
1375
+ options.frameId != null ? { frameId: options.frameId } : void 0
1376
+ );
1370
1377
  },
1371
1378
  addRootListener(processMessage) {
1372
1379
  const listener = (message, sender) => {
package/lib/index.js CHANGED
@@ -8,10 +8,17 @@ import {
8
8
  import Browser from "webextension-polyfill";
9
9
  function defineExtensionMessaging(config) {
10
10
  return defineGenericMessanging(__spreadProps(__spreadValues({}, config), {
11
- sendMessage(message, tabId) {
12
- if (tabId == null)
11
+ sendMessage(message, arg) {
12
+ if (arg == null) {
13
13
  return Browser.runtime.sendMessage(message);
14
- return Browser.tabs.sendMessage(tabId, message);
14
+ }
15
+ const options = typeof arg === "number" ? { tabId: arg } : arg;
16
+ return Browser.tabs.sendMessage(
17
+ options.tabId,
18
+ message,
19
+ // Pass frameId if specified
20
+ options.frameId != null ? { frameId: options.frameId } : void 0
21
+ );
15
22
  },
16
23
  addRootListener(processMessage) {
17
24
  const listener = (message, sender) => {
package/lib/page.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { N as NamespaceMessagingConfig, G as GenericMessenger } from './generic-ad5087a0.js';
1
+ import { N as NamespaceMessagingConfig, G as GenericMessenger } from './generic-683db69b.js';
2
2
 
3
3
  /**
4
4
  * Configuration passed into `defineWindowMessaging`.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webext-core/messaging",
3
- "version": "2.1.0",
3
+ "version": "2.3.0",
4
4
  "description": "Light weight, type-safe wrapper around the web extension messaging APIs. Supports all browsers (Chrome, Firefox, Safari)",
5
5
  "license": "MIT",
6
6
  "keywords": [