【问题标题】:How can I save and retrieve associated array from chrome local storage如何从 chrome 本地存储中保存和检索关联的数组
【发布时间】:2017-06-29 17:33:33
【问题描述】:

如何将关联的数组存储在 chrome 本地存储中,并以相同的格式检索它?我想保存“identityKeyPair”,格式为

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

我尝试了以下方法。但不锻炼。我在尝试检索它时收到 [Object Object]。

保存到本地存储

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

试图找回

chrome.storage.sync.get(["identityKeyPair"], function(items){
  if (items.identityKeyPair) {
alert(JSON.parse(items.identityKeyPair).pubKey);
  }

【问题讨论】:

标签: javascript arrays google-chrome-extension


【解决方案1】:

请在使用chrome.storage.sync时按照这些步骤操作

  1. 在清单中声明存储权限。

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. chrome://extensions 中重新加载您的应用,以便您的 manifest.json 在您的浏览器中得到更新。

  2. 遵循 chrome.storage.sync 的确切语法及其回调函数。

示例.js

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

这对你有用。我在我的 chrome 应用中测试了这段代码。

【讨论】:

  • 我得到了空数组,{"pubKey":{},"privKey":{}}。您是否尝试将数据保存为identityKeyPair -> { pubKey: ArrayBuffer, privKey: ArrayBuffer } 格式?
猜你喜欢
  • 2016-10-29
  • 1970-01-01
  • 2015-06-20
  • 2018-05-15
  • 1970-01-01
  • 1970-01-01
  • 2016-03-31
  • 2020-06-13
  • 1970-01-01
相关资源
最近更新 更多