ce
comlink-extension
npm i comlink-extension
ce

comlink-extension

Use comlink with chrome extensions

by Sam Denty

1.0.8 (see all)License:MITTypeScript:Built-In
npm i comlink-extension
Readme

Comlink for Web Extensions

This module allows you to use Comlink for Background <-> Content/Popup scripts.

Usage

Background-script:

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:

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();

Downloads/wk

8

GitHub Stars

19

LAST COMMIT

5mos ago

MAINTAINERS

1

CONTRIBUTORS

0

OPEN ISSUES

1

OPEN PRs

2
VersionTagPublished
1.0.8
latest
2yrs ago
No alternatives found
No tutorials found
Add a tutorial

Rate & Review

100
No reviews found
Be the first to rate