StorageArea.set()

Stores one or more items in the storage area, or update existing items.

Syntax

chrome.storage.<storageType>.set(keys,callback);

<storageType> will be one of the writable storage types — storage.sync or storage.local.

Parameters

  • Primitive values (such as numbers) and arrays will serialize as expected.
  • Functions will be omitted.
  • Dates, and Regexes will serialize using their String representation.
keys
An object containing one or more key/value pairs to be stored in storage. If an item already exists, its value will be updated.
callback Optional
A callback function that is run when the operation completes. The callback takes no arguments. If the operation failed, runtime.lastError is set.

Browser compatibility

EdgeFirefoxChromeOpera
Basic support?45.0Yes33
Firefox
Basic support48.0

Examples

// callback to set() just checks for errors
function onSet() {
  if (chrome.runtime.lastError) {
    console.log(chrome.runtime.lastError);
  } else {
    console.log("OK");
  }
}

// define 2 objects, that contain:
// a string, a function, a Date
var monster = {
  name: "Kraken",
  speak: function() {console.log("ROARR!!!")},
  birthday: new Date(2012, 11, 17)
}

var kitten = {
  name: "Moggy",
  speak: function() {console.log("Miaow")},
  birthday: new Date(2006, 7, 12)
}

// store the objects
chrome.storage.local.set({kitten, monster}, onSet);

function gotItem(item) {
  if (chrome.runtime.lastError) {
    console.log(chrome.runtime.lastError);
  } else {
    console.log(item);
  }
}

chrome.storage.local.get("kitten", gotItem);
// the function is omitted
// the Date is stored as a string
// -> {birthday: "2006-08-12T07:00:00.000Z", name: "Moggy"}

Example add-ons

Document Tags and Contributors

 Contributors to this page: wbamberg, chrisdavidmills
 Last updated by: wbamberg,