npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

webext-msgbus

v0.0.5

Published

## Introduction

Readme

webext-msgbus

Introduction

webext-msgbus is a tiny library that provides a simple and consistent API for sending and receiving messages between different parts of web extension, such as background, content-script, devtools, popup, options, sidepanel , and inject-script contexts.

Api

sendMessage()

sendMessage(messageId: string, data: any, destination: string)

Sends a message to some other part of your extension.

  • If there is no listener on the other side an error will be thrown where sendMessage was called.
  • Listener on the other may want to reply. Get the reply by awaiting the returned Promise
  • An error thrown in listener callback (in the destination context) will behave as usual, that is, bubble up, but the same error will also be thrown where sendMessage was called
  • If the listener receives the message but the destination disconnects (tab closure for exmaple) before responding, sendMessage will throw an error in the sender context.

messageId

Required | string

Any string that both sides of your extension agree on. Could be get-flag-count or getFlagCount or GET_FLAG_COUNT, as long as it's same on receiver's onMessage listener.

data

Required | any

Any serializable value you want to pass to other side, latter can access this value by refering to data property of first argument to onMessage callback function.

destination

Required | string

The actual identifier of other endpoint.

  • background
  • content-script@{tabId}
  • inject-script@{tabId}
  • devtools@{tabId}
  • popup
  • options
  • sidepanel

content-script, inject-script and devtools destinations can be suffixed with @{tabId} to target specific tab. Example: devtools@123, points to devtools panel inspecting tab with id 123.

onMessage()

onMessage(messageId: string, callback: function)

Register one and only one listener, per messageId per context. That will be called upon sendMessage from other side.

Optionally, send a response to sender by returning any value or if async a Promise.

messageId

Required | string

Any string that both sides of your extension agree on. Could be get-flag-count or getFlagCount or GET_FLAG_COUNT, as long as it's same in sender's sendMessage call.

callback

Required | function

A callback function webext-msgbus should call when a message is received with same messageId. The callback function will be called with one argument, a message which has sender, data and timestamp as its properties.

Optionally, this callback can return a value or a Promise, resolved value will sent as reply to sender.

Usage

example

content-script

import { sendMessage, onMessage } from 'webext-msgbus/contentScript';

sendMessage('MSG_ID_1', 'Your Data', 'background');

onMessage('MSG_ID_2', (msg) => {
  const { data } = msg;
  console.log(data);
})

background

import { sendMessage, onMessage } from 'webext-msgbus/background';

sendMessage('MSG_ID_2', 'Your Data', `content-script@${tabs[0].id}`);

onMessage('MSG_ID_1', (msg) => {
  const { data } = msg;
  console.log(data);
})