Google chrome extension 为什么storage.set/storage.get无法在Chrome中设置/获取数据?

Google chrome extension 为什么storage.set/storage.get无法在Chrome中设置/获取数据?,google-chrome-extension,firefox-addon-webextensions,Google Chrome Extension,Firefox Addon Webextensions,我开始学习在Chrome中调试扩展。我在Chrome中有这个问题,但在Firefox webextensions中没有这个问题。问题是,在Chrome中,我无法获取已保存到存储器中的数据 我在后台脚本中有这个函数。我将断点添加到storage.set的行中。我重新启动插件,可以访问MyNamespace中的所有数据。数据。。。加载项打印安装成功的通知,但当我尝试获取数据时,日志显示空对象 MyNamespace.checkIfExists = function (item) { if (ch

我开始学习在Chrome中调试扩展。我在Chrome中有这个问题,但在Firefox webextensions中没有这个问题。问题是,在Chrome中,我无法获取已保存到存储器中的数据

我在后台脚本中有这个函数。我将断点添加到storage.set的行中。我重新启动插件,可以访问MyNamespace中的所有数据。数据。。。加载项打印安装成功的通知,但当我尝试获取数据时,日志显示空对象

MyNamespace.checkIfExists = function (item) {
  if (chrome.runtime.lastError) {
    MyNamespace.notify("check-fail");
    console.log("error:" + chrome.runtime.lastError);
    console.log(chrome.runtime.lastError);
  } else {
    if ( ! ( "initiated" in item ) )
      {
      console.log("I'm initiating .. item:");
      console.log(item);
      /* Insert new data object */
      /* This saves default values into storage */       
      chrome.storage.local.set({
        easy:     MyNamespace.data.easy,
        advanced: MyNamespace.data.advanced,
        images:   MyNamespace.data.images,
        inspector:MyNamespace.data.inspector,
        extractor:MyNamespace.data.extractor,
        profiles: MyNamespace.data.profiles,
        default_profiles: MyNamespace.data.default_profiles        
        }, 
        () => {
          if (chrome.runtime.lastError) 
            { 
            MyNamespace.notify("write-fail");            
            console.log("local.set Error: " + chrome.runtime.lastError); }
          else
            {                         
            MyNamespace.notify("write-succeess");
            chrome.storage.local.get( {initiated: undefined, easy:undefined}, 
              (result) => {
                if (typeof result === undefined )
                  MyNamespace.notify("write-fail");
                console.log("found:");
                console.log(result);
                console.log("/end");
                }
              )
            chrome.storage.local.set( {initiated:true} );            
            MyNamespace.initiated = true;
            }
        } );
      }
    else
      MyNamespace.initiated = true;
  }
};
控制台中的结果:

I'm initiating .. item:
namespace.js:125 Object {}
namespace.js:149 found:
namespace.js:150 Object {}
namespace.js:151 /end
控制台中没有错误。在Chrome中,对象为空。在Firefox中,我看到一个要保存的对象,一个未定义的属性

manifest.json

{
  "applications": {
  "gecko": {
    "id": "some-options@mozilla.org",
    "strict_min_version": "48.0a1"
    }
  },
"description": "An example options ui",
"homepage_url": "https://github.com/mdn/webextensions-examples/tree/master/favourite-colour",
"manifest_version": 2,
"name": "Favourite colour",
"version": "1.0",
"icons": {  
  "48": "icons/eye-icon-colored-48.png"    
},
"options_ui": {
  "page": "options.html",
  "open_in_tab": true
  },
"browser_action": {
  "default_title": "Adapt page",
  "browser_style": true,
  "default_popup": "popup/settings.html",
  "default_icon": {
    "48": "icons/eye-icon-48.png"
  }
},
"web_accessible_resources": [
  "icons/hotkeys-icon-32.png"
  ],
"permissions": ["storage", "tabs", "activeTab", "notifications"],
"background": {
  "page": "background_scripts/background_scripts_bug_fix.html"
 },
"content_scripts": [
    {
    "matches": ["<all_urls>" ],
    "run_at": "document_start",
    "js": [
      "content_scripts/jquery-3.0.0.js",
      "content_scripts/namespace.js",
      "content_scripts/easy.js",
      "content_scripts/addListeners.js"
      ]
    }
  ],
"commands": {
  "easy": {
    "suggested_key": {
      "default": "Ctrl+Shift+U"    
    },
    "description": ""
  }
 }
}
我得到了这个日志:

    namespace.js:241 Storage key "default_profiles" in namespace "local" changed. Old value was "Object", new value is "undefined".
    namespace.js:241 Storage key "easy" in namespace "local" changed. Old value was "Object", new value is "undefined".
    namespace.js:241 Storage key "extractor" in namespace "local" changed. Old value was "Object", new value is "undefined".
    namespace.js:241 Storage key "images" in namespace "local" changed. Old value was "Object", new value is "undefined".
    namespace.js:241 Storage key "initiated" in namespace "local" changed. Old value was "true", new value is "undefined".
    namespace.js:241 Storage key "inspector" in namespace "local" changed. Old value was "Object", new value is "undefined".
    namespace.js:241 Storage key "profiles" in namespace "local" changed. Old value was "Object", new value is "undefined".
    namespace.js:124 I'm initiating .. item:
    namespace.js:125 Object {}
    namespace.js:241 Storage key "advanced" in namespace "local" changed. Old value was "undefined", new value is "Object".
    namespace.js:241 Storage key "default_profiles" in namespace "local" changed. Old value was "undefined", new value is "Object".
    namespace.js:241 Storage key "easy" in namespace "local" changed. Old value was "undefined", new value is "Object".
    namespace.js:241 Storage key "extractor" in namespace "local" changed. Old value was "undefined", new value is "Object".
    namespace.js:241 Storage key "images" in namespace "local" changed. Old value was "undefined", new value is "Object".
    namespace.js:241 Storage key "inspector" in namespace "local" changed. Old value was "undefined", new value is "Object".
    namespace.js:241 Storage key "profiles" in namespace "local" changed. Old value was "undefined", new value is "Object".
    namespace.js:152 found:
    namespace.js:153 Object {}
namespace.js:154 /end
namespace.js:241 Storage key "initiated" in namespace "local" changed. Old value was "undefined", new value is "true".
那么这意味着什么呢?我在Chrome中看到了单词Object(),白色字母(黑色主题),但它不像我可以单击并看到对象的链接。这显示数据已加载。但是为什么我不能查看对象中的数据呢

  • 我无法进入回调,因此无法查看数据
  • 我无法在控制台中查看对象{}中的内容。。。。它看起来是空的,但在侦听器中,我收到的消息存储已更改
  • 存储中的结果。获取回调结果为空。。。不包含任何属性
  • 我已使用以下代码进行了检查: for(var输入结果) if(hasOwnProperty.call(result,key)) 控制台日志(“键:+键)

    在Chrome中没有列出属性,在Firefox工具箱/控制台中列出属性

    据我所知,storage.set回调应该在进行保存更改并关闭db文件进行写入后触发。所以我认为storage.get from inside storage.set应该不会有任何问题

    有人能澄清一下这里发生了什么吗?为什么在Firefox中可以这样做,但在Chrome中却不行?如何解决这个问题

    chrome.storage.local.get({initiated:undefined,easy:undefined}

    由于使用parameters对象的中间字符串化只检索(,)已定义的键:
    JSON.stringify({a:undefined,b:undefined})
    生成
    '{}'
    -为空

    当您不提供默认值时,请改用键名数组:

    chrome.storage.local.get( ['initiated', 'easy'], 
    
    注意:使用中的
    %o
    %o
    显示对象的实际内容

    chrome.storage.local.get({initiated:undefined,easy:undefined}

    由于使用parameters对象的中间字符串化只检索(,)已定义的键:
    JSON.stringify({a:undefined,b:undefined})
    生成
    '{}'
    -为空

    当您不提供默认值时,请改用键名数组:

    chrome.storage.local.get( ['initiated', 'easy'], 
    

    另外,在中使用
    %o
    %o
    来显示对象的实际内容。

    谢谢。你救了我。IDK为什么我以前没有尝试过。我花了一周时间试图解决这个问题。谢谢。你救了我。IDK为什么我以前没有尝试过。我花了一周时间试图解决这个问题。