1.0.11 • Published 3 months ago

browser-storage-polyfill v1.0.11

Weekly downloads
-
License
ISC
Repository
github
Last release
3 months ago

browser-storage-polyfill

JavaScript library that enables the use of storage outside of the Chrome runtime in web applications. It provides a way to simulate the chrome.storage API using the localStorage object.

chrome.storage API is not provided for normal web apps as it is build for extension usage only.

Check chrome.storage docs

the main idea is to use localStorage to simulate chrome.storage

Usage

the same way you use the chrome.storage api

import AsyncStorage from "browser-storage-polyfill";

// local storage
AsyncStorage.local.set({ key: value }).then(() => {
  console.log("Value is set");
  // do something
});

AsyncStorage.local.get(["key"]).then((result) => {
  console.log("Value currently is " + result.key);
  // do something
});

// sync storage

AsyncStorage.sync.set({ key: value }).then(() => {
  console.log("Value is set");
  // do something
});

AsyncStorage.sync.get(["key"]).then((result) => {
  console.log("Value currently is " + result.key);
  // do something
});

// session storage

AsyncStorage.session.set({ key: value }).then(() => {
  console.log("Value was set");
  // do something
});

AsyncStorage.session.get(["key"]).then((result) => {
  console.log("Value currently is " + result.key);
  // do something
});

Motivation

The chrome.storage API is primarily designed for use in Chrome extensions. However, if you are developing a Chrome extension and want to test it as a web app, you may encounter limitations in using chrome.storage directly. This library aims to bridge that gap by providing a polyfill that allows you to use localStorage to simulate the chrome.storage behavior.

Challenges

There are main differences between localStorage and chrome.storage.

  • chrome.storage is Async while localStorage is Sync
  • chrome.storage can handle callbacks
  • function naming is different
  • chrome.storage: in get method, if no parameter is passed, all values got returned
1.0.11

3 months ago

1.0.9

4 months ago

1.0.10

4 months ago

1.0.8

5 months ago

1.0.7

5 months ago

1.0.6

5 months ago

1.0.5

5 months ago

1.0.4

5 months ago

1.0.3

5 months ago

1.0.2

5 months ago

1.0.1

5 months ago

1.0.0

5 months ago