We're looking for a person or people to help audit MDN to find places we could speed up. Is this you or someone you know? Check out the RFP: https://mzl.la/2IHcMiE



翻譯不完整。請協助 翻譯此英文文件


這個非同步函式會回傳 Promise


let gettingItem = browser.storage.<storageType>.get(
  keys    // null, string, object or array of strings

<storageType> 會是以下可覆寫的儲存類型之一:storage.syncstorage.local


用來識別要檢查單元的 key(單個為字串;多個為陣列、或指定預設值的物件)。如果把這裡留空(空字串、空陣列、空物件都可以),就會取得空物件。如果是 null 或 undefined,則會取得所有儲存的內容。


Promise that will be fulfilled with a results object containing every object in keys that was found in the storage area. If the operation failed, the promise will be rejected with an error message.

When used within a content script in Firefox versions prior to 52, the Promise returned by browser.storage.local.get() is fulfilled with an Array containing one Object. The Object in the Array contains the keys found in the storage area, as described above. The Promise is correctly fulfilled with an Object when used in the background context (background scripts, popups, options pages, etc.). When this API is used as chrome.storage.local.get(), it correctly passes an Object to the callback function.


ChromeEdgeFirefoxFirefox for AndroidOpera
Basic support Yes Yes454833



// 兩個單元:「kitten」與「monster」
  kitten:  {name:"Mog", eats:"mice"},
  monster: {name:"Kraken", eats:"people"}

Define success and failure handlers for the promise:

function onGot(item) {

function onError(error) {
  console.log(`Error: ${error}`);

With no keys argument, retrieve everything:

let gettingItem = browser.storage.local.get();
gettingItem.then(onGot, onError);

// -> Object { kitten: Object, monster: Object }

With an empty keys argument, return nothing:

// with an empty array, retrieve nothing
let gettingItem = browser.storage.local.get([]);
gettingItem.then(onGot, onError);

// -> Object { }

With the name of an object, retrieve the match:

let gettingItem = browser.storage.local.get("kitten");
gettingItem.then(onGot, onError);

// -> Object { kitten: Object }

With an array of object names, retrieve all matches:

let gettingItem = browser.storage.local.get(["kitten", "monster", "grapefruit"]);
gettingItem.then(onGot, onError);

// -> Object { kitten: Object, monster: Object } 

With an object with object names as keys and the default value as value:

let gettingItem = browser.storage.local.get({
  kitten: "no kitten",
  monster: "no monster",
  grapefruit: {
    name: "Grape Fruit",
    eats: "Water"

// -> Object { kitten: Object, monster: Object, grapefruit: Object }

Chrome 示例

chrome.storage.local.get("kitten", function(items){
  console.log(items.kitten);  // -> {name:"Mog", eats:"mice"}

Or with an arrow function

chrome.storage.local.get("kitten", items=>{
  console.log(items.kitten); // -> {name:"Mog", eats:"mice"}

This API is based on Chromium's chrome.storage API. This documentation is derived from storage.json in the Chromium code.

Microsoft Edge compatibility data is supplied by Microsoft Corporation and is included here under the Creative Commons Attribution 3.0 United States License.


 此頁面的貢獻者: iigmir
 最近更新: iigmir,