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

chrome-extension-post-message

v0.1.9

Published

TypeSafe browser extension messaging.

Downloads

3

Readme

chrome-extension-post-message

TypeSafe browser extension messaging.

Support: Chrome extension Long-lived connections.

Install

npm i chrome-extension-post-message

Message passing

https://developer.chrome.com/docs/extensions/develop/concepts/messaging#connect

// content-script.js:
var port = chrome.runtime.connect({ name: "knockknock" });
port.postMessage({ joke: "Knock knock" });
port.onMessage.addListener(function (msg) {
  if (msg.question === "Who's there?") port.postMessage({ answer: "Madame" });
  else if (msg.question === "Madame who?")
    port.postMessage({ answer: "Madame... Bovary" });
});

// background.js
chrome.runtime.onConnect.addListener(function (port) {
  console.assert(port.name === "knockknock");
  port.onMessage.addListener(function (msg) {
    if (msg.joke === "Knock knock")
      port.postMessage({ question: "Who's there?" });
    else if (msg.answer === "Madame")
      port.postMessage({ question: "Madame who?" });
    else if (msg.answer === "Madame... Bovary")
      port.postMessage({ question: "I don't get it." });
  });
});

Example

// message.ts:
type Data = {data:string}
type ResponseData = {type:string,data:string}

export const {
    sendData,
    receiveData,
    postData,
    backgroundReceiveData,
    disconnectPostData
} = createPostMessage<Data,ResponseData>("test");

/**
 * cross-extension messaging:
 * https://developer.chrome.com/docs/extensions/reference/runtime#method-connect
 *
 * sending messages from web pages:
 * https://developer.chrome.com/docs/extensions/reference/runtime#method-sendMessage
 *
 export const {
    sendData,
    receiveData,
    postData,
    backgroundReceiveData,
    disconnectPostData
  } = createExternalPostMessage<Data,ResponseData>("test","chrome_extension_id");
 */

// content-script.js:
sendData({data:"hi, from content-script.js"})
receiveData((response)=>{
    console.log("receiveData response: ", response);
})

// combine `sendData` and `receiveData`
postData(
  // sendData
  (sendData)=>{
    const data = {data:"hi, from content-script.js"}
    sendData(data);
  },
  // receiveData
  (responseData)=>{
    console.log("responseData: ", responseData);
  }
)

// backgrounds.js:
backgroundReceiveData((response) => {
  console.log("backgroundReceiveData: ", response);
  response.postMessage({type:"test", data:"hello, from: backgrounds.js"})
})

//  call results in multiple ports at the receiver's end:
//  const port = chrome.runtime.connect({name: "test"});
//  port.disconnect();
disconnectPostData();