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-utils

v0.0.3

Published

Some utils that help to build chrome extension

Downloads

32

Readme

chrome-utils

npm version Build Status Coverage Status js-standard-style chrome-utils GitHub license

Some utils that help to build chrome extension. HAVE NO DEPENDENCIES.

Usage

$ npm i chrome-utils --save
// es6
import { store, message, i18n } from 'chrome-utils';

// es5
const store = require('chrome-utils').store;

Api

store

chrome.storage API is anti-human, for example, if you saved a non-plain object to store, {a: {b: 1, c: {d: 2}}}, then how to directly get the value of d from it?

One more question, if you have already save {a1: {b: 1}, a2: {c: 2}} to store, then wanna update b to 2, then how to do it? If we use raw API like chrome.storage.sync.set({a1: {b: 2}}), then we'll find a2 was totally disappeard!

Actually, by raw chrome.storage API:

  • you can only get the top key-value
  • you can only get a but not a.b.d.
  • if you wanna update a value in a object, you must get it from store first, then update the whole object, finally, save it to store.

WTF?


get & set

If target value exist, then auto to merge it

store.get(key[, resolve, reject]);
store.set(key, value[, options]);

// usage example
store.set('a.c', 1);
store.get('a'); // {c: 1}

store.set('a.b': 2);
store.get('a'); // inject b, get {a: {b: 2, c: 1}}
store.get('a.b'); // directly get b, return 2

store.set('a.b': 3);
store.get('a.b'); // update, get 3

listen

store.listen(...listeners);

// listener
const listener = {
	key, // the key you wanna to listen change
	callback
};
const listeners = [listener1, listener2, listener3];

clear & remove

store.clear();
store.remove(key[, callback]);

// example
store.set({a: {b: 1, c: 2}});
store.get('a'); // {b: 1, c: 2}

store.remove('a.b');
// after remove
store.get('a'); // => {b: null, c: 2}

store.remove('a');
// after remove
store.get('a'); // => null

storeAsync

Uses promises instead of callbacks. Plays well with async/await.

import { storeAsync as store } from 'chrome-utils';

// with promises
store.set('a', "xxx").then(e => console.log("done"))
store.get('a').then(e => console.log(e))
store.remove('a').then(e => console.log("done"))
store.clear().then(e => console.log("done"))

// with async/await
(async function() {
		await store.set("b", "yyy")
		const b = await store.get("b")
		console.debug("b", b)
})()

message

Compare with raw API chrome.runtime.onMessage & chrome.runtime.sendMessage, it:

  • force user add type for each msg
  • if msg listener as a type key, it will only response to target type msg

send message

message.sendMsg(msg[, callback]);

// msg
const msg = {
	type, // required
	data
};

send msg to tabs

message.sendToTabs(msg[, query]);

register listener

message.register(...listeners);

// listener
const listener = {
	callback, // required,
	type // not required, but if you use it, this listener will only listen same type msg
};

i18n

get message

i18n.get(...args);

Todo

  • [x] remove merge api
  • [ ] expire time for store
  • [x] test
  • [x] more use case

Contributors