{"id":16796646,"url":"https://github.com/ecmadao/chrome-utils","last_synced_at":"2025-04-11T00:12:13.641Z","repository":{"id":112025053,"uuid":"89547363","full_name":"ecmadao/chrome-utils","owner":"ecmadao","description":"Some utils that help to build chrome extension","archived":false,"fork":false,"pushed_at":"2022-07-12T04:04:08.000Z","size":61,"stargazers_count":7,"open_issues_count":3,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-11T00:12:09.811Z","etag":null,"topics":["chrome","chrome-extension","chrome-i18n","chrome-runtime","chrome-storage"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ecmadao.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-04-27T02:34:19.000Z","updated_at":"2020-01-09T06:29:12.000Z","dependencies_parsed_at":"2023-07-31T03:30:44.471Z","dependency_job_id":null,"html_url":"https://github.com/ecmadao/chrome-utils","commit_stats":null,"previous_names":[],"tags_count":15,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ecmadao%2Fchrome-utils","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ecmadao%2Fchrome-utils/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ecmadao%2Fchrome-utils/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ecmadao%2Fchrome-utils/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ecmadao","download_url":"https://codeload.github.com/ecmadao/chrome-utils/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248317729,"owners_count":21083530,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["chrome","chrome-extension","chrome-i18n","chrome-runtime","chrome-storage"],"created_at":"2024-10-13T09:19:42.227Z","updated_at":"2025-04-11T00:12:13.633Z","avatar_url":"https://github.com/ecmadao.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# chrome-utils\n\n[![npm version](https://badge.fury.io/js/chrome-utils.svg)](https://badge.fury.io/js/chrome-utils) [![Build Status](https://travis-ci.org/ecmadao/chrome-utils.svg?branch=master)](https://travis-ci.org/ecmadao/chrome-utils) [![Coverage Status](https://coveralls.io/repos/github/ecmadao/chrome-utils/badge.svg?branch=master)](https://coveralls.io/github/ecmadao/chrome-utils?branch=master) [![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](http://standardjs.com) [![chrome-utils](http://img.shields.io/npm/dm/chrome-utils.svg)](https://www.npmjs.com/package/chrome-utils) [![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/ecmadao/chrome-utils/master/LICENSE)\n\nSome utils that help to build chrome extension. **HAVE NO DEPENDENCIES**.\n\n## Usage\n\n```bash\n$ npm i chrome-utils --save\n```\n\n```javascript\n// es6\nimport { store, message, i18n } from 'chrome-utils';\n\n// es5\nconst store = require('chrome-utils').store;\n```\n\n## Api\n\n### store\n\n`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?\n\nOne 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!\n\nActually, by raw `chrome.storage` API:\n\n- you can only get the top key-value\n- you can only get `a` but not `a.b.d`.\n- 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.\n\nWTF?\n\n---\n\n#### get \u0026 set\n\nIf target value exist, then auto to merge it\n\n```javascript\nstore.get(key[, resolve, reject]);\nstore.set(key, value[, options]);\n\n// usage example\nstore.set('a.c', 1);\nstore.get('a'); // {c: 1}\n\nstore.set('a.b', 2);\nstore.get('a'); // inject b, get {b: 2, c: 1}\nstore.get('a.b'); // directly get b, return 2\n\nstore.set('a.b', 3);\nstore.get('a.b'); // update, get 3\n```\n\n#### listen\n\n```javascript\nstore.listen(...listeners);\n\n// listener\nconst listener = {\n\tkey, // the key you wanna to listen change\n\tcallback\n};\nconst listeners = [listener1, listener2, listener3];\n```\n\n#### clear \u0026 remove\n\n```javascript\nstore.clear();\nstore.remove(key[, callback]);\n\n// example\nstore.set({a: {b: 1, c: 2}});\nstore.get('a'); // {b: 1, c: 2}\n\nstore.remove('a.b');\n// after remove\nstore.get('a'); // =\u003e {b: null, c: 2}\n\nstore.remove('a');\n// after remove\nstore.get('a'); // =\u003e null\n```\n\n### storeAsync\n\nUses promises instead of callbacks. Plays well with `async/await`.\n\n```javascript\nimport { storeAsync as store } from 'chrome-utils';\n\n// with promises\nstore.set('a', \"xxx\").then(e =\u003e console.log(\"done\"))\nstore.get('a').then(e =\u003e console.log(e))\nstore.remove('a').then(e =\u003e console.log(\"done\"))\nstore.clear().then(e =\u003e console.log(\"done\"))\n\n// with async/await\n(async function() {\n\t\tawait store.set(\"b\", \"yyy\")\n\t\tconst b = await store.get(\"b\")\n\t\tconsole.debug(\"b\", b)\n})()\n```\n\n\n\n### message\n\nCompare with raw API `chrome.runtime.onMessage` \u0026 `chrome.runtime.sendMessage`, it:\n\n- force user add `type` for each msg\n- if msg listener as a `type` key, it will only response to target type msg\n\n#### send message\n\n```javascript\nmessage.sendMsg(msg[, callback]);\n\n// msg\nconst msg = {\n\ttype, // required\n\tdata\n};\n```\n\n#### send msg to tabs\n\n```javascript\nmessage.sendToTabs(msg[, query]);\n```\n\n#### register listener\n\n```javascript\nmessage.register(...listeners);\n\n// listener\nconst listener = {\n\tcallback, // required,\n\ttype // not required, but if you use it, this listener will only listen same type msg\n};\n```\n\n### i18n\n\n#### get message\n\n```javascript\ni18n.get(...args);\n```\n\n## Todo\n\n- [x] remove `merge` api\n- [ ] expire time for store\n- [x] test\n- [x] more use case\n\n## Contributors\n\n- [berstend](https://github.com/berstend)\n\n## License\n\n[MIT License](./LICENSE)","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fecmadao%2Fchrome-utils","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fecmadao%2Fchrome-utils","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fecmadao%2Fchrome-utils/lists"}