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); } 
2

1 Answer 1

-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.

Sign up to request clarification or add additional context in comments.

1 Comment

I am getting empty arrays, {"pubKey":{},"privKey":{}}. Have you tried to save the data in identityKeyPair -> { pubKey: ArrayBuffer, privKey: ArrayBuffer } format ?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.