How to Use Storage in Web Extensions

By  on  

Working on a web extension is an interesting experience -- you get to taste web while working with special extension APIs. One such API is storage -- the web extension flavor of persistence. Let's explore how you can use session and local storage within your Manifest V3 web extensions!

Enabling Extension Storage

The extension storage API isn't available by default. To enable the storage API, you need to cite it in the manifest.json file of your extension:

{
  // more....
  "manifest_version": 3,
  "name": "__MSG_appName__",
  "permissions": [
    "storage",
    // more....
  ],
  // more....
}

Adding storage to the permissions array, which is a top level manifest.json key, provides session and local storage capabilities to your extension.

Get, Set, and Remove Storage Values

Much like traditional localStorage and sessionStorage APIs, extension storage provides get, set, and remove operations:

// set
await chrome.storage.session.set({ name: "David", color: "green" });

// get 
const { name, color } = await chrome.storage.session.get(["name", "color"]);

// remove
await chrome.storage.session.remove(["name", "color"]);

A few things to note:

  • get requires an array argument, not a single value like localStorage and sessionStorage
  • set needs to be an object format
  • remove is also an array, much like get
  • You can use chrome.storage.local or chrome.storage.session depending on how
  • All of the extension storage API methods are promise-based, with await or callback formats

Clear All Storage

In the event that you want to clear all data for local or session storage, there's a clear method:

await chrome.storage.session.clear();

Using clear is effective but you'll want to be sure that you do truly want to clear everything -- clear could become a maintenance issue.

Storage is an essential part of most web extensions. While the API is simple, the async format and method names are different.

Recent Features

  • By
    CSS Filters

    CSS filter support recently landed within WebKit nightlies. CSS filters provide a method for modifying the rendering of a basic DOM element, image, or video. CSS filters allow for blurring, warping, and modifying the color intensity of elements. Let's have...

  • By
    Serving Fonts from CDN

    For maximum performance, we all know we must put our assets on CDN (another domain).  Along with those assets are custom web fonts.  Unfortunately custom web fonts via CDN (or any cross-domain font request) don't work in Firefox or Internet Explorer (correctly so, by spec) though...

Incredible Demos

  • By
    CSS Tooltips

    We all know that you can make shapes with CSS and a single HTML element, as I've covered in my CSS Triangles and CSS Circles posts.  Triangles and circles are fairly simply though, so as CSS advances, we need to stretch the boundaries...

  • By
    5 More HTML5 APIs You Didn’t Know Existed

    The HTML5 revolution has provided us some awesome JavaScript and HTML APIs.  Some are APIs we knew we've needed for years, others are cutting edge mobile and desktop helpers.  Regardless of API strength or purpose, anything to help us better do our job is a...

Discussion

  1. This is great. But this it work in both Firefox and Chrome extensions?

    In my experience, if you store something in storage.session on FF – once the extension gets “inactive” (Manifest V3 feature), it’s lost – but in Chrome it persist. So storage.session on FF is worthless. Do you have the same experience?

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!