summaryrefslogtreecommitdiff
path: root/source/helpers/used-storage.ts
blob: 8a95bf18bf03f1005cd1156bde1da21e7e41482f (plain) (blame)
1
2
3
4
5
6
7
8
9
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;
}