@webext-core/messaging
Version:
Light weight, type-safe wrapper around the web extension messaging APIs. Supports all browsers (Chrome, Firefox, Safari)
37 lines (25 loc) • 844 B
Markdown
Supports all browsers (Chrome, Firefox, Safari).
```ts
// ./messaging.ts
import { defineExtensionMessaging } from '@webext-core/messaging';
interface ProtocolMap {
getStringLength(s: string): number;
}
export const { sendMessage, onMessage } = defineExtensionMessaging<ProtocolMap>();
```
```ts
// ./background.ts
import { onMessage } from './messaging';
onMessage('getStringLength', message => {
return message.data.length;
});
```
```ts
// ./content-script.js or anywhere else
import { sendMessage } from './messaging';
const length = await sendMessage('getStringLength', 'hello world');
console.log(length); // 11
```
See [documentation](https://webext-core.aklinker1.io/guide/messaging/) to get started!
A light-weight, type-safe wrapper around the `browser.runtime` messaging APIs.