Methods to Use Storage in Net Extensions


Engaged on an online extension is an attention-grabbing expertise — you get to style net whereas working with particular extension APIs. One such API is storage — the net extension taste of persistence. Let’s discover how you should use 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’ll want to 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 degree manifest.json key, gives session and native storage capabilities to your extension.

Get, Set, and Take away Storage Values

Very like conventional localStorage and sessionStorage APIs, extension storage gives get, set, and take away operations:

// set
await chrome.storage.session.set({ identify: "David", colour: "inexperienced" });

// get 
const { identify, 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 like localStorage and sessionStorage
  • set must be an object format
  • take away can also be an array, very similar to get
  • You should use chrome.storage.native or chrome.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 simply wish to clear all information for native or session storage, there is a clear technique:

await chrome.storage.session.clear();

Utilizing clear is efficient however you will wish to ensure that you do actually wish to clear all the pieces — clear might grow to be a upkeep problem.

Storage is an important a part of most net extensions. Whereas the API is easy, the async format and technique names are completely different.



Source_link

Leave a Reply

Your email address will not be published.