sendMessage()
sendMessage(messageId: string, data: any, destination: string)
Sends a message to some other part of your extension.
- If there is no listener on the other side an error will be thrown where
sendMessage
was called. - Listener on the other may want to reply. Get the reply by
await
ing the returnedPromise
- An error thrown in listener callback (in the destination context) will behave as usual, that is, bubble up, but the same error will also be thrown where
sendMessage
was called - If the listener receives the message but the destination disconnects (tab closure for exmaple) before responding,
sendMessage
will throw an error in the sender context.
messageId
Required | string
Any string
that both sides of your extension agree on. Could be get-flag-count
or getFlagCount
or GET_FLAG_COUNT
, as long as it's same on receiver's onMessage
listener.
data
Required | any
Any serializable value you want to pass to other side, latter can access this value by refering to data
property of first argument to onMessage
callback function.
destination
Required | string
The actual identifier of other endpoint.
Example: devtools
or content-script
or background
or content-script@133
or devtools@453
content-script
, window
and devtools
destinations can be suffixed with @<tabId>
to target specific tab. Example: devtools@351
, points to devtools panel inspecting tab with id 351.
For content-script
, a specific frameId
can be specified by appending the frameId
to the suffix @<tabId>.<frameId>
.
Read Notes section to see how destinations (or endpoints) are treated.
For security reasons, if you want to receive or send messages to or from window
context, one of your extension's content script must call allowWindowMessaging(<namespace: string>)
to unlock message routing.
Also call setNamespace(<namespace: string>)
in those window
contexts. Use same namespace string in those two calls, so webext-bridge
knows which message belongs to which extension (in case multiple extensions are using webext-bridge
in one page)