How to Use Storage in Web Extensions

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 […]

The post How to Use Storage in Web Extensions appeared first on David Walsh Blog.


This content originally appeared on David Walsh Blog and was authored by David Walsh

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.

The post How to Use Storage in Web Extensions appeared first on David Walsh Blog.


This content originally appeared on David Walsh Blog and was authored by David Walsh


Print Share Comment Cite Upload Translate Updates
APA

David Walsh | Sciencx (2022-10-19T22:59:18+00:00) How to Use Storage in Web Extensions. Retrieved from https://www.scien.cx/2022/10/19/how-to-use-storage-in-web-extensions/

MLA
" » How to Use Storage in Web Extensions." David Walsh | Sciencx - Wednesday October 19, 2022, https://www.scien.cx/2022/10/19/how-to-use-storage-in-web-extensions/
HARVARD
David Walsh | Sciencx Wednesday October 19, 2022 » How to Use Storage in Web Extensions., viewed ,<https://www.scien.cx/2022/10/19/how-to-use-storage-in-web-extensions/>
VANCOUVER
David Walsh | Sciencx - » How to Use Storage in Web Extensions. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/10/19/how-to-use-storage-in-web-extensions/
CHICAGO
" » How to Use Storage in Web Extensions." David Walsh | Sciencx - Accessed . https://www.scien.cx/2022/10/19/how-to-use-storage-in-web-extensions/
IEEE
" » How to Use Storage in Web Extensions." David Walsh | Sciencx [Online]. Available: https://www.scien.cx/2022/10/19/how-to-use-storage-in-web-extensions/. [Accessed: ]
rf:citation
» How to Use Storage in Web Extensions | David Walsh | Sciencx | https://www.scien.cx/2022/10/19/how-to-use-storage-in-web-extensions/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.