0

How can I store associated array in chrome local storage, and retrieve that in the same format? I want to save "identityKeyPair", which is in the form

identityKeyPair -> { pubKey: ArrayBuffer, privKey: ArrayBuffer }

I tried the below things. But not working out. I am getting [Object Object] on trying to retrieve it.

Saving to local storage

chrome.storage.sync.set({'identityKeyPair': JSON.stringify(identityKeyPair)}, function() {
          alert('identityKeyPair saved');
        });

Trying to retrieve

chrome.storage.sync.get(["identityKeyPair"], function(items){
  if (items.identityKeyPair) {
alert(JSON.parse(items.identityKeyPair).pubKey);
  }
tarun14110
  • 940
  • 5
  • 26
  • 57
  • Possible duplicate of [saving and retrieving from chrome.storage.sync](https://stackoverflow.com/questions/14531102/saving-and-retrieving-from-chrome-storage-sync) – Patrick Barr Jun 29 '17 at 17:38
  • 3
    I suspect this may have something to do with you stringify-ing ArrayBuffers, see https://stackoverflow.com/questions/8593896/chrome-extension-how-to-pass-arraybuffer-or-blob-from-content-script-to-the-bac – Tom Dunn Jun 29 '17 at 17:49

1 Answers1

-1

Please follow these steps while you are using chrome.storage.sync

  1. Declare storage permissions in your manifest.

manifest.json

{
  "manifest_version": 2,

  "name": "Storage",
  "description": "This extension shows a Google Image search result for the current page",
  "version": "1.0",
  "icons":{
"256":"img/icon.png"
    },
  "permissions": [
    "storage"
  ],
  "app": {
    "launch": {
      "local_path": "options.html"
    }

  },
   "background": {
    "scripts": ["js/app/background.js"]
  }

}
  1. Reload your App in chrome://extensions so that your manifest.json gets updated in your browser.

  2. Follow the exact syntax of chrome.storage.sync with its callback function.

Sample.js

$(".thing").click(function() {
      chrome.storage.sync.set({"myKey": "testPrefs"},function(){
       alert("object stored");
})
    chrome.storage.sync.get("myKey",function(obj){
        alert(obj.myKey);
    })
});

This will work for you. I tested this code in my chrome app.

Ravi Teja Kumar Isetty
  • 1,559
  • 4
  • 21
  • 39
  • I am getting empty arrays, `{"pubKey":{},"privKey":{}}`. Have you tried to save the data in `identityKeyPair -> { pubKey: ArrayBuffer, privKey: ArrayBuffer }` format ? – tarun14110 Jun 29 '17 at 19:02