Package Exports
- @nicebuzzy/userscript-storage
Readme
Userscript Storage
Overview
Userscript Storage is a simple wrapper for GM.getValue, GM.setValue, GM.deleteValue, etc.
Installation
Node
npm install @nicebuzzy/userscript-storageUserscripts
// @require https://cdn.jsdelivr.net/npm/@nicebuzzy/userscript-storage/dist/userscript-storage.umd.jsUsage
const store = new Storage()
store.set('foo', 'bar')
store.set('baz', 'qux')
store.get('foo') // 'bar'
store.get('baz') // 'qux'
store.list() // { foo: 'bar', baz: 'qux' }
store.remove('baz')
store.get('baz') // undefined
store.get('baz', {}) // {}
store.list() // { foo: 'bar' }
store.clear()
store.list() // {}Using scopes
The scope option allows customization of key storage:
const settings = new Storage({ scope: 'settings' })Scoped instances only work with keys that match their scope:
store.set('foo', 'bar')
settings.set('foo', 'bar')
store.list() // { foo: 'bar', settings: { foo: 'bar' } }
settings.list() // { foo: 'bar' }
settings.clear()
settings.list() // {}
store.list() // { foo: 'bar' }
store.clear() // clears all keys, including scoped onesAPI Reference
Constructor
const store = new Storage(options = {})options:Objectoptions.scope:String
Methods
store.clear()
Clears all entries in the storage.
store.get(key, defaultValue = undefined)
Retrieves the value for the given key. Returns defaultValue if the key is not found.
store.keys()
Returns all keys in the storage.
store.list()
Lists all key-value pairs in the storage.
store.remove(key)
Removes the specified key and its value from the storage.
store.set(key, value)
Adds or updates a key-value pair in the storage.
Properties
store.size
Returns the number of keys stored.