https://github.com/samdenty/comlink-extension
Use comlink with chrome extensions
https://github.com/samdenty/comlink-extension
Last synced: about 1 year ago
JSON representation
Use comlink with chrome extensions
- Host: GitHub
- URL: https://github.com/samdenty/comlink-extension
- Owner: samdenty
- Created: 2020-07-06T23:02:47.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2024-12-20T17:04:28.000Z (over 1 year ago)
- Last Synced: 2025-05-11T03:02:47.999Z (about 1 year ago)
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/comlink-extension
- Size: 15.6 KB
- Stars: 29
- Watchers: 3
- Forks: 5
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Comlink for Web Extensions [](https://github.com/sponsors/samdenty)
[Sponsor this project](https://github.com/sponsors/samdenty)
This module allows you to use [Comlink](https://github.com/GoogleChromeLabs/comlink) for `Background <-> Content/Popup` communication, in Chrome/Firefox/Safari extensions.
## Usage
Background-script:
```ts
import { createBackgroundEndpoint, isMessagePort } from "comlink-extension";
import * as Comlink from "comlink";
chrome.runtime.onConnect.addListener((port) => {
if (isMessagePort(port)) return;
Comlink.expose(
{
test() {
console.log("called");
},
},
createBackgroundEndpoint(port)
);
});
```
Content / popup / devtool script:
```ts
import { createEndpoint, forward } from "comlink-extension";
import * as Comlink from "comlink";
// Wrap a chrome.runtime.Port
const obj = Comlink.wrap(createEndpoint(chrome.runtime.connect()));
obj.test();
// Or, wrap an existing Message Channel:
const { port1, port2 } = new MessageChannel();
forward(port1, chrome.runtime.connect());
const obj = Comlink.wrap(port2);
obj.test();
```