Package Exports
- webext-storage-cache
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (webext-storage-cache) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
webext-storage-cache 
WebExtensions module: Map-like promised cache storage with expiration. Chrome and Firefox.
This module works on content scripts, background pages and option pages.
Install
You can just download the standalone bundle (it might take a minute to download) and include the file in your manifest.json
, or:
npm install --save webext-storage-cache
// This module is only offered as a ES Module
import storageCache from 'webext-storage-cache';
Usage
This module requires the storage
permission:
// manifest.json
{
"permissions": [
"storage"
]
}
import cache from 'webext-storage-cache';
(async () => {
if (!await cache.has('unique')) {
const cachableItem = await someFunction();
await cache.set('unique', cachableItem, 3 /* days */);
}
console.log(await cache.get('unique'));
})();
The same code could be also written more effectively with cache.function
:
import cache from 'webext-storage-cache';
const cachedFunction = cache.function(someFunction, {
expiration: 3,
cacheKey: () => 'unique'
});
(async () => {
console.log(await cachedFunction());
})();
API
Similar to a Map()
, but all methods a return a Promise
. It also has a memoization method that hides the caching logic and makes it a breeze to use.
cache.has(key)
Checks if the given key is in the cache, returns a boolean
.
key
Type: string
cache.get(key)
Returns the cached value of key if it exists and hasn't expired, returns undefined
otherwise.
key
Type: string
cache.set(key, value, expiration /* in days */)
Caches the given key and value for a given amount of days. It returns the value itself.
key
Type: string
value
Type: string | number | boolean
or array | object
of those three types
expiration
Type: number
Default: 30
The number of days after which the cache item will expire.
cache.delete(key)
Deletes the requested item from the cache.
key
Type: string
cache.function(getter, options)
Caches the return value of the function based on the cacheKey
. It works similarly to lodash.memoize
:
async function getHTML(url, options) {
const response = await fetch(url, options);
return response.text();
}
const cachedGetHTML = cache.function(getHTML);
const html = await cachedGetHTML('https://google.com', {});
// The HTML of google.com will be saved with the key 'https://google.com'
getter
Type: async function
that returns a cacheable value.
options
cacheKey
Type: function
that returns a string
Default: function
that returns the first argument of the call
const cachedOperate = cache.function(operate, {
cacheKey: args => args.join(',')
});
cachedOperate(1, 2, 3);
// The result of `operate(1, 2, 3)` will be stored in the key '1,2,3'
// Without a custom `cacheKey`, it would be stored in the key '1'
expiration
Type: number
Default: 30
The number of days after which the cache item will expire.
Related
- webext-options-sync - Helps you manage and autosave your extension's options.
- webext-domain-permission-toggle - Browser-action context menu to request permission for the current tab.
- webext-dynamic-content-scripts - Automatically inject your
content_scripts
on custom domains. - webext-detect-page - Detects where the current browser extension code is being run.
- webext-content-script-ping - One-file interface to detect whether your content script have loaded.
Awesome WebExtensions
: A curated list of awesome resources for Web Extensions development.
License
MIT © Federico Brigante