Engaged on an online extension is an fascinating expertise — you get to style net whereas working with particular extension APIs. One such API is storage
— the online extension taste of persistence. Let’s discover how you should utilize session
and native
storage inside your Manifest V3 net extensions!
Enabling Extension Storage
The extension storage
API is not accessible by default. To allow the storage
API, you want to cite it within the manifest.json
file of your extension:
{ // extra.... "manifest_version": 3, "title": "__MSG_appName__", "permissions": [ "storage", // more.... ], // extra.... }
Including storage
to the permissions
array, which is a high degree manifest.json
key, gives session
and native
storage capabilities to your extension.
Get, Set, and Take away Storage Values
Very similar to conventional localStorage
and sessionStorage
APIs, extension storage gives get
, set
, and take away
operations:
// set await chrome.storage.session.set({ title: "David", colour: "inexperienced" }); // get const { title, colour } = await chrome.storage.session.get(["name", "color"]); // take away await chrome.storage.session.take away(["name", "color"]);
A number of issues to notice:
get
requires an array argument, not a single worth likelocalStorage
andsessionStorage
set
must be an object formattake away
can be an array, very likeget
- You need to use
chrome.storage.native
orchrome.storage.session
relying on how - All the extension storage API strategies are promise-based, with
await
or callback codecs
Clear All Storage
Within the occasion that you just need to clear all information for native or session storage, there is a clear
methodology:
await chrome.storage.session.clear();
Utilizing clear
is efficient however you may need to make certain that you do actually need to clear every part — clear
might change into a upkeep challenge.
Storage is a necessary a part of most net extensions. Whereas the API is straightforward, the async format and methodology names are completely different.