Engaged on an internet extension is an fascinating expertise — you get to style internet whereas working with particular extension APIs. One such API is storage
— the net extension taste of persistence. Let’s discover how you should utilize session
and native
storage inside your Manifest V3 internet extensions!
Enabling Extension Storage
The extension storage
API is not obtainable by default. To allow the storage
API, it’s essential cite it within the manifest.json
file of your extension:
{ // extra.... "manifest_version": 3, "identify": "__MSG_appName__", "permissions": [ "storage", // more.... ], // extra.... }
Including storage
to the permissions
array, which is a high stage 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({ identify: "David", coloration: "inexperienced" }); // get const { identify, coloration } = await chrome.storage.session.get(["name", "color"]); // take away await chrome.storage.session.take away(["name", "color"]);
A couple of issues to notice:
get
requires an array argument, not a single worth likelocalStorage
andsessionStorage
set
must be an object formattake away
can also be an array, very similar toget
- You need to use
chrome.storage.native
orchrome.storage.session
relying on how - The entire extension storage API strategies are promise-based, with
await
or callback codecs
Clear All Storage
Within the occasion that you just wish to clear all knowledge for native or session storage, there is a clear
methodology:
await chrome.storage.session.clear();
Utilizing clear
is efficient however you will wish to make certain that you do really wish to clear the whole lot — clear
might turn out to be a upkeep difficulty.
Storage is an important a part of most internet extensions. Whereas the API is straightforward, the async format and methodology names are totally different.
Source_link