webext-messenger is a focused JavaScript library designed to streamline inter-component communication within browser extensions. It provides a robust framework for message passing between different parts of an extension, such as background scripts, content scripts, and web pages, as well as offscreen documents. The current stable version is 0.35.0, with frequent minor releases indicating active development, often introducing new features and internal optimizations. A key differentiator is its emphasis on minimizing external dependencies, aiming for a lightweight footprint, as evidenced by recent efforts to drop libraries like `p-retry` and `webextension-polyfill` for core functionality. This library simplifies complex messaging patterns, abstracting away the underlying browser `runtime.sendMessage` and `runtime.onMessage` APIs to offer a more developer-friendly interface for building robust and scalable browser extensions.
npm install webext-messengerVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates basic inter-component communication between a background service worker and a content script using `webext-messenger`. It shows how to initialize messengers in different contexts, set up message listeners, and send messages with data, including handling responses and errors across extension components. The manifest is provided for a complete example.
If your extension relies on `browser` APIs, ensure `webextension-polyfill` is installed as a dependency in your project (`npm install webextension-polyfill`) and correctly initialized, or update your code to use `chrome` APIs directly where possible.
Review any parts of your extension where message sending might fail and rely on automatic retries. Test behavior thoroughly with v0.35.0+ to ensure it meets your fault tolerance requirements. Consider implementing custom retry logic if the new default behavior is insufficient.
When receiving messages in background scripts, always validate the `sender` origin and any received data, especially when messages originate from web pages (e.g., `sender.url` or `sender.origin`). Implement strict allow-lists for message types and data schemas.
Ensure you are using `import messenger from 'webext-messenger';` at the top of your module. If in a CommonJS context (e.g., older Node.js scripts), you might need `const messenger = require('webext-messenger').default;` or transpile your code to ESM.Verify that the target script (`background.js`, `content.js`) is correctly registered in your `manifest.json`, is loaded in the browser, and has initialized `messenger.init()` with the correct name and an `onMessage` listener for the message type being sent. Ensure contexts can communicate (e.g., content script cannot directly message another content script).