webext-storage 0.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/// <reference types="chrome" />
|
|
2
|
+
export type StorageItemOptions = {
|
|
3
|
+
area?: chrome.storage.AreaName;
|
|
4
|
+
};
|
|
5
|
+
export declare class StorageItem<T> {
|
|
6
|
+
readonly key: string;
|
|
7
|
+
readonly area: chrome.storage.AreaName;
|
|
8
|
+
constructor(key: string, { area }?: StorageItemOptions);
|
|
9
|
+
get(): Promise<T | undefined>;
|
|
10
|
+
set(value: T): Promise<void>;
|
|
11
|
+
onChange(callback: (value: T) => void, signal?: AbortSignal): void;
|
|
12
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import chromeP from 'webext-polyfill-kinda';
|
|
2
|
+
export class StorageItem {
|
|
3
|
+
key;
|
|
4
|
+
area;
|
|
5
|
+
constructor(key, { area = 'local' } = {}) {
|
|
6
|
+
this.key = key;
|
|
7
|
+
this.area = area;
|
|
8
|
+
}
|
|
9
|
+
async get() {
|
|
10
|
+
const result = await chromeP.storage[this.area].get(this.key);
|
|
11
|
+
if (!Object.hasOwn(result, this.key)) {
|
|
12
|
+
return;
|
|
13
|
+
}
|
|
14
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-return -- Assumes the user never uses the Storage API directly
|
|
15
|
+
return result[this.key];
|
|
16
|
+
}
|
|
17
|
+
async set(value) {
|
|
18
|
+
await chromeP.storage[this.area].set({ [this.key]: value });
|
|
19
|
+
}
|
|
20
|
+
onChange(callback, signal) {
|
|
21
|
+
const changeHandler = (changes, area) => {
|
|
22
|
+
console.log('changeHandler', changes, area);
|
|
23
|
+
const changedItem = changes[this.key];
|
|
24
|
+
if (area === this.area && changedItem) {
|
|
25
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument -- Assumes the user never uses the Storage API directly
|
|
26
|
+
callback(changedItem.newValue);
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
chrome.storage.onChanged.addListener(changeHandler);
|
|
30
|
+
signal?.addEventListener('abort', () => {
|
|
31
|
+
chrome.storage.onChanged.removeListener(changeHandler);
|
|
32
|
+
}, { once: true });
|
|
33
|
+
}
|
|
34
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/* eslint-disable no-new -- Type tests only */
|
|
2
|
+
import { expectType, expectNotAssignable, expectAssignable } from 'tsd';
|
|
3
|
+
import { StorageItem } from './storage-item.js';
|
|
4
|
+
new StorageItem('key', { area: 'local' });
|
|
5
|
+
new StorageItem('key', { area: 'sync' });
|
|
6
|
+
const unknownItem = new StorageItem('key');
|
|
7
|
+
expectAssignable(unknownItem.get());
|
|
8
|
+
const objectItem = new StorageItem('key');
|
|
9
|
+
expectType(objectItem.get());
|
|
10
|
+
expectType(objectItem.set({ name: 'new name' }));
|
|
11
|
+
const stringItem = new StorageItem('key');
|
|
12
|
+
expectAssignable(stringItem.get());
|
|
13
|
+
expectNotAssignable(stringItem.get());
|
|
14
|
+
expectType(stringItem.get());
|
|
15
|
+
expectType(stringItem.set('some string'));
|
|
16
|
+
// @ts-expect-error Type is string
|
|
17
|
+
await stringItem.set(1);
|
|
18
|
+
// @ts-expect-error Type is string
|
|
19
|
+
await stringItem.set(true);
|
|
20
|
+
// @ts-expect-error Type is string
|
|
21
|
+
await stringItem.set([true, 'string']);
|
|
22
|
+
// @ts-expect-error Type is string
|
|
23
|
+
await stringItem.set({ wow: [true, 'string'] });
|
|
24
|
+
// @ts-expect-error Type is string
|
|
25
|
+
await stringItem.set(1, { days: 1 });
|
package/license
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) Federico Brigante <me@fregante.com> (https://fregante.com)
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
6
|
+
|
|
7
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/package.json
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "webext-storage",
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"description": "A more usable typed storage API for Web Extensions",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"browser",
|
|
7
|
+
"extension",
|
|
8
|
+
"chrome",
|
|
9
|
+
"firefox",
|
|
10
|
+
"safari",
|
|
11
|
+
"webextension",
|
|
12
|
+
"storage",
|
|
13
|
+
"session",
|
|
14
|
+
"sync",
|
|
15
|
+
"local",
|
|
16
|
+
"event",
|
|
17
|
+
"web-ext"
|
|
18
|
+
],
|
|
19
|
+
"repository": "fregante/webext-storage",
|
|
20
|
+
"funding": "https://github.com/sponsors/fregante",
|
|
21
|
+
"license": "MIT",
|
|
22
|
+
"author": "Federico Brigante <me@fregante.com> (https://fregante.com)",
|
|
23
|
+
"type": "module",
|
|
24
|
+
"exports": "./distribution/storage-item.js",
|
|
25
|
+
"main": "./distribution/storage-item.js",
|
|
26
|
+
"types": "./distribution/storage-item.d.ts",
|
|
27
|
+
"files": [
|
|
28
|
+
"distribution"
|
|
29
|
+
],
|
|
30
|
+
"scripts": {
|
|
31
|
+
"build": "tsc",
|
|
32
|
+
"prepack": "tsc --sourceMap false",
|
|
33
|
+
"test": "tsc --noEmit && xo && tsd && vitest run",
|
|
34
|
+
"test:watch": "vitest",
|
|
35
|
+
"watch": "tsc --watch"
|
|
36
|
+
},
|
|
37
|
+
"xo": {
|
|
38
|
+
"envs": [
|
|
39
|
+
"browser"
|
|
40
|
+
],
|
|
41
|
+
"globals": [
|
|
42
|
+
"chrome"
|
|
43
|
+
]
|
|
44
|
+
},
|
|
45
|
+
"dependencies": {
|
|
46
|
+
"webext-polyfill-kinda": "^1.0.2"
|
|
47
|
+
},
|
|
48
|
+
"devDependencies": {
|
|
49
|
+
"@sindresorhus/tsconfig": "^5.0.0",
|
|
50
|
+
"@types/chrome": "^0.0.248",
|
|
51
|
+
"@types/sinon-chrome": "^2.2.13",
|
|
52
|
+
"jest-chrome": "^0.8.0",
|
|
53
|
+
"sinon-chrome": "^3.0.1",
|
|
54
|
+
"tsd": "^0.28.1",
|
|
55
|
+
"typescript": "^5.2.2",
|
|
56
|
+
"vitest": "^0.34.6",
|
|
57
|
+
"xo": "^0.56.0"
|
|
58
|
+
},
|
|
59
|
+
"engines": {
|
|
60
|
+
"node": ">=20"
|
|
61
|
+
},
|
|
62
|
+
"tsd": {
|
|
63
|
+
"directory": "source"
|
|
64
|
+
}
|
|
65
|
+
}
|
package/readme.md
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
# webext-storage [![][badge-gzip]][link-bundlephobia]
|
|
2
|
+
|
|
3
|
+
[badge-gzip]: https://img.shields.io/bundlephobia/minzip/webext-storage.svg?label=gzipped
|
|
4
|
+
[link-bundlephobia]: https://bundlephobia.com/result?p=webext-storage
|
|
5
|
+
|
|
6
|
+
> A more usable typed storage API for Web Extensions
|
|
7
|
+
|
|
8
|
+
## Install
|
|
9
|
+
|
|
10
|
+
```sh
|
|
11
|
+
npm install webext-storage
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
Or download the [standalone bundle](https://bundle.fregante.com/?pkg=webext-storage&name=StorageItem) to include in your `manifest.json`.
|
|
15
|
+
|
|
16
|
+
## Usage
|
|
17
|
+
|
|
18
|
+
```ts
|
|
19
|
+
import {StorageItem} from "webext-storage";
|
|
20
|
+
|
|
21
|
+
const username = new StorageItem<string>('username')
|
|
22
|
+
|
|
23
|
+
await username.set('Ugo');
|
|
24
|
+
// Promise<void>
|
|
25
|
+
|
|
26
|
+
await username.get();
|
|
27
|
+
// Promise<string>
|
|
28
|
+
|
|
29
|
+
await username.set({name: 'Ugo'});
|
|
30
|
+
// TypeScript Error: Argument of type '{ name: string; }' is not assignable to parameter of type 'string'.
|
|
31
|
+
|
|
32
|
+
username.onChange(newName => {
|
|
33
|
+
console.log('The user’s new name is', newName);
|
|
34
|
+
});
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## Related
|
|
38
|
+
|
|
39
|
+
- [webext-storage-cache](https://github.com/fregante/webext-storage-cache) - Detects where the current browser extension code is being run.
|
|
40
|
+
- [webext-tools](https://github.com/fregante/webext-tools) - Utility functions for Web Extensions.
|
|
41
|
+
- [webext-content-scripts](https://github.com/fregante/webext-content-scripts) - Utility functions to inject content scripts in WebExtensions.
|
|
42
|
+
- [webext-base-css](https://github.com/fregante/webext-base-css) - Extremely minimal stylesheet/setup for Web Extensions’ options pages (also dark mode)
|
|
43
|
+
- [webext-options-sync](https://github.com/fregante/webext-options-sync) - Helps you manage and autosave your extension's options.
|
|
44
|
+
- [More…](https://github.com/fregante/webext-fun)
|
|
45
|
+
|
|
46
|
+
## License
|
|
47
|
+
|
|
48
|
+
MIT © [Federico Brigante](https://fregante.com)
|