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 🙏

© 2024 – Pkg Stats / Ryan Hefner

chromeps

v0.1.0

Published

Simple pubsub implementation for Chrome extensions, with a few extra features.

Downloads

4

Readme

Original: https://github.com/anderspitman/chromeps

Simple pubsub implementation for Chrome extensions, with a few extra features.

Messages are propagated to every part of your extension (by default), no matter where they originate from.

This module was created to address 2 fundamental challenges with writing Chrome extensions:

Purpose

  1. Handling lots of message passing cleanly.
  2. Passing messages between a content script and iframes

The second point is the most interesting. If you have an iframe as part of your content script, it cannot communicate directly with the rest of your content script, so you end up having to go through the background page. This gets messy real fast. chromeps provides a clean abstraction over this process, using a generic pubsub interface.

Installation

Copy chromeps.js into your project directory

Usage

You need to include chromeps.js in your background page as well as any content scripts or iframes that load up.

Load chromeps.js

If all your code is in Javascript files all you need to do is edit your manifest.json as such:

{
  ...
  "background" : {
    "scripts" : [
      "chromeps.js",
      "background.js"
    ]
  },
  "content_scripts" : [
    {
      "js" : [
        "chromeps.js",
        "content.js"
      ]
    }
  ]
  ...
}

Where background.js is your background page and content.js is one of your content scripts. So basically chromeps will be loaded for the background page and each of your content scripts.

Subscribe to Messages

chromeps.subscribe('mytopic', function(message) {
    // do something
}

Publish Messages

chromeps.publish('mytopic', {'content': 'Hi there'});

You can use an Javascript objects or strings as messages. By default, messages are propagated to the background page and every tab. See below for cusumization options.

Extras

There are a few extra features to help in using chromeps

Control where messages are sent

Often times you don't need your messages to be sent to every tab, and it's more efficient to avoid blasting all your tabs with messages. You can customize where messages are sent using the following methods:

chromeps.publishActive('mytopic', 'hi there'); // only publishes to active tab
chromeps.publishSame('mytopic', 'hi there'); // only publishes to this tab

For example if you publish a message from a content script on a tab using publishSame, only content scripts and iframe on that tab will see the messages.

Determine tab ID

Often it is very useful to filter received messages on the subscriber side, so we know whether the message affects the tab that receives it. Unfortunately the only way for a tab to identify itself is to ask the background page. The getTabId method provides this functionality transparently. It only makes the request the first time then saves the value for future queries. However, since the first request is asynchronous the return value is retrieved via a callback.

chromeps.getTabId(function(tabId) {
  // tabId ready
});