Skip to content

Commit

Permalink
add listenForExternalMessages option
Browse files Browse the repository at this point in the history
  • Loading branch information
SidneyNemzer committed Jun 12, 2024
1 parent 309ffec commit 66cc292
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions src/wrap-store/wrapStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,14 @@ const defaultOpts = {
* Wraps a Redux store so that proxy stores can connect to it. This function
* must be called synchronously when the extension loads to avoid dropping
* messages that woke the service worker.
* @param {boolean} options.listenForExternalMessages Controls if other
* extensions can access the store via onMessageExternal (default is false). Use
* with caution, this allows any other extension to read the store and dispatch
* actions.
* @return {WrapStore} The wrapStore function that accepts a Redux store and
* options. See {@link WrapStore}.
*/
export default () => {
export default ({ listenForExternalMessages } = {}) => {
const browserAPI = getBrowserAPI();

// Setup message listeners synchronously to avoid dropping messages if the
Expand All @@ -76,10 +80,16 @@ export default () => {
browserAPI.runtime.onMessage.addListener(stateProviderListener.listener);
browserAPI.runtime.onMessage.addListener(actionListener.listener);

if (browserAPI.runtime.onMessageExternal) {
browserAPI.runtime.onMessageExternal.addListener(actionListener.listener);
} else {
console.warn("runtime.onMessageExternal is not supported");
if (listenForExternalMessages) {
if (browserAPI.runtime.onMessageExternal) {
// Listen for messages from other extensions
browserAPI.runtime.onMessageExternal.addListener(
stateProviderListener.listener
);
browserAPI.runtime.onMessageExternal.addListener(actionListener.listener);
} else {
console.warn("runtime.onMessageExternal is not supported");
}
}

return (
Expand Down

0 comments on commit 66cc292

Please sign in to comment.