docs.rodeo

MDN Web Docs mirror

StorageArea.set()

{{AddonSidebar}} 

Stores one or more items in the storage area or updates stored items.

When you store or update a value using this API, the {{WebExtAPIRef("storage.onChanged")}}  event fires.

Note that when storing items in the sync storage area, the browser enforces quotas on the amount of data each extension can store. See Storage quotas for sync data.

This is an asynchronous function that returns a Promise.

Syntax

let settingItem = browser.storage.<storageType>.set(
  keys             // object
)

Where <storageType> is one of the writable storage types — {{WebExtAPIRef("storage.local")}} , {{WebExtAPIRef("storage.session")}} , or {{WebExtAPIRef("storage.sync")}} .

Parameters

[!NOTE] If you want to remove keys from storage, use {{WebExtAPIRef("storage.storageArea.remove")}} . If you want to overwrite a value with a void value, use null, i.e., key: null.

Return value

A {{jsxref("Promise")}}  that is fulfilled with no arguments if the operation succeeds. If the operation fails, the promise is rejected with an error message.

Examples

function setItem() {
  console.log("OK");
}

function gotKitten(item) {
  console.log(`${item.kitten.name} has ${item.kitten.eyeCount} eyes`);
}

function gotMonster(item) {
  console.log(`${item.monster.name} has ${item.monster.eyeCount} eyes`);
}

function onError(error) {
  console.log(error);
}

// define 2 objects
let monster = {
  name: "Kraken",
  tentacles: true,
  eyeCount: 10,
};

let kitten = {
  name: "Moggy",
  tentacles: false,
  eyeCount: 2,
};

// store the objects
browser.storage.local.set({ kitten, monster }).then(setItem, onError);

browser.storage.local.get("kitten").then(gotKitten, onError);
browser.storage.local.get("monster").then(gotMonster, onError);

{{WebExtExamples}} 

Browser compatibility

{{Compat}} 

[!NOTE] This API is based on Chromium’s chrome.storage API. This documentation is derived from storage.json in the Chromium code.

In this article

View on MDN