diff options
Diffstat (limited to 'source/helpers')
-rw-r--r-- | source/helpers/types.d.ts | 5 | ||||
-rw-r--r-- | source/helpers/used-storage.ts | 10 |
2 files changed, 15 insertions, 0 deletions
diff --git a/source/helpers/types.d.ts b/source/helpers/types.d.ts index 06f4cb80..e3224b49 100644 --- a/source/helpers/types.d.ts +++ b/source/helpers/types.d.ts @@ -15,4 +15,9 @@ declare global { closest<S extends string>(selector: S | readonly S[]): StrictlyParseSelector<S, HTMLElement> | null; matches(selectors: string | readonly string[]): boolean; } + + // This cannot be a regular import because it turns `globals.d.ts` in a "module definition", which it isn't + type Browser = import('webextension-polyfill').Browser; + const browser: Browser; + } diff --git a/source/helpers/used-storage.ts b/source/helpers/used-storage.ts new file mode 100644 index 00000000..8a95bf18 --- /dev/null +++ b/source/helpers/used-storage.ts @@ -0,0 +1,10 @@ +/** `getBytesInUse` polyfill */ +export default async function getStorageBytesInUse(area: 'local' | 'sync'): Promise<any> { + const storage = browser.storage[area]; + // From https://bugzilla.mozilla.org/show_bug.cgi?id=1385832#c20 + return 'getBytesInUse' in storage ? storage.getBytesInUse() : new TextEncoder().encode( + Object.entries(await storage.get()) + .map(([key, value]) => key + JSON.stringify(value)) + .join(''), + ).length; +} |