@webext-core/messaging
TypeScript icon, indicating that this package has built-in type declarations

1.4.0 • Public • Published

@webext-core/messaging

A light-weight, type-safe wrapper around the browser.runtime messaging APIs. Supports all browsers (Chrome, Firefox, Safari).

// ./messaging.ts
import { ProtocolWithReturn, defineExtensionMessaging } from '@webext-core/messaging';

interface ProtocolMap {
  getStringLength: ProtocolWithReturn<string, number>;
}

export const { sendMessage, onMessage } = defineExtensionMessaging<ProtocolMap>();
// ./background.ts
import { onMessage } from './messaging';

onMessage('getStringLength', message => {
  return message.data.length;
});
// ./content-script.js or anywhere else
import { sendMessage } from './messaging';

const length = await sendMessage('getStringLength', 'hello world');

console.log(length); // 11

Get Started

See documentation to get started!

Package Sidebar

Install

npm i @webext-core/messaging

Weekly Downloads

394

Version

1.4.0

License

MIT

Unpacked Size

102 kB

Total Files

13

Last publish

Collaborators

  • _aklinker1