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

chaz

v0.0.1

Published

A communication library for WebExtensions.

Downloads

12

Readme

Chaz.js

A communication library for WebExtensions.

English document is here.

Chaz.js 可以做什么?

Chaz.js是为WebExtensions打造的通信库,可以极大的简化WebExtensions不同类型的脚本传递消息。

目前支持的类型脚本有:content script、background script、 privileged scripts(如 popup scripts 、page scripts)

Chaz.js为上述脚本提供统一的发送接口,比如:

// background.js

const Content =new Chaz('background','content');
Content.on('hello',function(data ,sender ,callback){
    console.log(`background.js: receive content.js's hello "${data}"`);
});
// content.js

const Background =new Chaz('content','background');
Background.send('hello','hello background.js, this is content.js');

详细的使用可以参考项目中background.jscontent.jspopup.js文件。

需要额外注意的是,若想要content与popup通信,你必须在background中实例化一个Chaz实例,哪怕不传入第二个参数:

// background.js
new Chaz('background');

APIs

Chaz#constructor(selfType,targetType)

  • selfType:当前运行的脚本类型
  • targetType:想与之通信的脚本类型

Chaz#on(type ,listener)

  • type:监听的事件类型
  • listener:监听器

当事件发生,监听器将会被传入:

  • data:被发送的数据
  • sender:有关发送方的信息
  • callback:响应这个事件并返回值

callback

A function to call, at most once, to send a response to the message. The function takes a single argument, which may be any JSON-ifiable object. This argument is passed back to the message sender.

If you have more than one onMessage listener in the same document, then only one may send a response.

To send a response synchronously, call sendResponse before the listener function returns. To send a response asynchronously:

  • either keep a reference to the sendResponse argument and return true from the listener function. You will then be able to call sendResponse after the listener function has returned.
  • or return a Promise from the listener function and resolve the promise when the response is ready.

Chaz#send(eventType ,data ,tabId=null)

  • eventType:发送的事件类型
  • data:发送的消息

该方法返回一个Promise对象,对应Chaz#on的callback