JSPM

@nicebuzzy/userscript-storage

1.0.0
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 3
  • Score
    100M100P100Q34275F
  • License MIT

A wrapper for GM.getValue, GM.setValue, GM.deleteValue, etc.

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

Userscripts

// @require https://cdn.jsdelivr.net/npm/@nicebuzzy/userscript-storage/dist/userscript-storage.umd.js

Usage

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 ones

API Reference

Constructor

const store = new Storage(options = {})
  • options: Object
    • options.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.