1.0.9 • Published 11 months ago

@0xbigboss/comlink-extension v1.0.9

Weekly downloads
-
License
MIT
Repository
github
Last release
11 months ago

Comlink for Web Extensions Sponsors

Forked from samdenty/comlink-extension. Mostly just updates typescript, formats, and republishes built files to @0xbigboss/comlink-extension.

This module allows you to use Comlink for Background <-> Content/Popup communication, in Chrome/Firefox/Safari extensions.

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

11 months ago