@webext-core/messagingBaseMessagingConfiginterface BaseMessagingConfig {
logger?: Logger;
breakError?: boolean;
}
Shared configuration between all the different messengers.
logger?: Logger (default: console)null to disable logging.breakError?: boolean (default: undefined)CustomEventMessageinterface CustomEventMessage {
event: CustomEvent;
}
Additional fields available on the Message from a CustomEventMessenger.
event: CustomEventCustomEventMessagingConfiginterface CustomEventMessagingConfig extends NamespaceMessagingConfig {}
Configuration passed into defineCustomEventMessaging.
CustomEventMessengertype CustomEventMessenger<TProtocolMap extends Record<string, any>> =
GenericMessenger<TProtocolMap, CustomEventMessage, []>;
Messenger returned by defineCustomEventMessenger.
defineCustomEventMessagingfunction defineCustomEventMessaging<
TProtocolMap extends Record<string, any> = Record<string, any>,
>(config: CustomEventMessagingConfig): CustomEventMessenger<TProtocolMap> {
// ...
}
Creates a CustomEventMessenger. This messenger is backed by the CustomEvent APIs. It can be
used to communicate between:
sendMessage does not accept any additional arguments..
interface WebsiteMessengerSchema {
initInjectedScript(data: ...): void;
}
export const websiteMessenger = defineCustomEventMessenger<initInjectedScript>();
// Content script
websiteMessenger.sendMessage("initInjectedScript", ...);
// Injected script
websiteMessenger.onMessage("initInjectedScript", (...) => {
// ...
})
*
defineExtensionMessagingfunction defineExtensionMessaging<
TProtocolMap extends Record<string, any> = Record<string, any>,
>(config?: ExtensionMessagingConfig): ExtensionMessenger<TProtocolMap> {
// ...
}
Returns an ExtensionMessenger that is backed by the browser.runtime.sendMessage and
browser.tabs.sendMessage APIs.
It can be used to send messages to and from the background page/service worker.
defineWindowMessagingfunction defineWindowMessaging<
TProtocolMap extends Record<string, any> = Record<string, any>,
>(config: WindowMessagingConfig): WindowMessenger<TProtocolMap> {
// ...
}
Returns a WindowMessenger. It is backed by the window.postMessage API. It can be used to
communicate between:
interface WebsiteMessengerSchema {
initInjectedScript(data: ...): void;
}
export const websiteMessenger = defineWindowMessaging<initInjectedScript>();
// Content script
websiteMessenger.sendMessage("initInjectedScript", ...);
// Injected script
websiteMessenger.onMessage("initInjectedScript", (...) => {
// ...
})
ExtensionMessageinterface ExtensionMessage {
sender: Runtime.MessageSender;
}
Additional fields available on the Message from an ExtensionMessenger.
sender: Runtime.MessageSenderRuntime.MessageSender.ExtensionMessagingConfiginterface ExtensionMessagingConfig extends BaseMessagingConfig {}
Configuration passed into defineExtensionMessaging.
ExtensionMessengertype ExtensionMessenger<TProtocolMap extends Record<string, any>> =
GenericMessenger<TProtocolMap, ExtensionMessage, ExtensionSendMessageArgs>;
Messenger returned by defineExtensionMessaging.
ExtensionSendMessageArgstype ExtensionSendMessageArgs = [arg?: number | SendMessageOptions];
Send message accepts either:
You cannot message between tabs directly. It must go through the background script.
GenericMessengerinterface GenericMessenger<
TProtocolMap extends Record<string, any>,
TMessageExtension,
TSendMessageArgs extends any[],
> {
sendMessage<TType extends keyof TProtocolMap>(
type: TType,
...args: GetDataType<TProtocolMap[TType]> extends undefined
? [data?: undefined, ...args: TSendMessageArgs]
: never
): Promise<GetReturnType<TProtocolMap[TType]>>;
sendMessage<TType extends keyof TProtocolMap>(
type: TType,
data: GetDataType<TProtocolMap[TType]>,
...args: TSendMessageArgs
): Promise<GetReturnType<TProtocolMap[TType]>>;
onMessage<TType extends keyof TProtocolMap>(
type: TType,
onReceived: (
message: Message<TProtocolMap, TType> & TMessageExtension,
) => void | MaybePromise<GetReturnType<TProtocolMap[TType]>>,
): RemoveListenerCallback;
removeAllListeners(): void;
}
Messaging interface shared by all messengers.
Type parameters accept:
TProtocolMap to define the data and return types of messages.TMessageExtension to define additional fields that are available on a message inside
onMessage's callbackTSendMessageArgs to define a list of additional arguments for sendMessageGetDataTypetype GetDataType<T> = T extends (...args: infer Args) => any
? Args["length"] extends 0 | 1
? Args[0]
: never
: T extends ProtocolWithReturn<any, any>
? T["BtVgCTPYZu"]
: T;
Given a function declaration, ProtocolWithReturn, or a value, return the message's data type.
GetReturnTypetype GetReturnType<T> = T extends (...args: any[]) => infer R
? R
: T extends ProtocolWithReturn<any, any>
? T["RrhVseLgZW"]
: void;
Given a function declaration, ProtocolWithReturn, or a value, return the message's return type.
Loggerinterface Logger {
debug(...args: any[]): void;
log(...args: any[]): void;
warn(...args: any[]): void;
error(...args: any[]): void;
}
Interface used to log text to the console when sending and receiving messages.
MaybePromisetype MaybePromise<T> = Promise<T> | T;
Either a Promise of a type, or that type directly. Used to indicate that a method can by sync or async.
Messageinterface Message<
TProtocolMap extends Record<string, any>,
TType extends keyof TProtocolMap,
> {
id: number;
data: GetDataType<TProtocolMap[TType]>;
type: TType;
timestamp: number;
}
Contains information about the message received.
id: numberdata: GetDataType<TProtocolMap[TType]>sendMessagetype: TTypetimestamp: numberNamespaceMessagingConfiginterface NamespaceMessagingConfig extends BaseMessagingConfig {
namespace: string;
}
namespace: stringProtocolWithReturn:::danger Deprecated Use the function syntax instead: https://webext-core.aklinker1.io/messaging/protocol-maps.html#syntax :::
interface ProtocolWithReturn<TData, TReturn> {
BtVgCTPYZu: TData;
RrhVseLgZW: TReturn;
}
Used to add a return type to a message in the protocol map.
Internally, this is just an object with random keys for the data and return types.
BtVgCTPYZu: TDataRrhVseLgZW: TReturninterface ProtocolMap {
// data is a string, returns undefined
type1: string;
// data is a string, returns a number
type2: ProtocolWithReturn<string, number>;
}
RemoveListenerCallbacktype RemoveListenerCallback = () => void;
Call to ensure an active listener has been removed.
If the listener has already been removed with Messenger.removeAllListeners, this is a noop.
SendMessageOptionsinterface SendMessageOptions {
tabId: number;
frameId?: number;
}
Options for sending a message to a specific tab/frame
tabId: numberframeId?: numberWindowMessagingConfiginterface WindowMessagingConfig extends NamespaceMessagingConfig {}
Configuration passed into defineWindowMessaging.
WindowMessengertype WindowMessenger<TProtocolMap extends Record<string, any>> =
GenericMessenger<TProtocolMap, {}, WindowSendMessageArgs>;
WindowSendMessageArgstype WindowSendMessageArgs = [targetOrigin?: string];
For a WindowMessenger, sendMessage requires an additional argument, the targetOrigin. It
defines which frames inside the page should receive the message.
See https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage#targetorigin for more details.
API reference generated by docs/generate-api-references.ts