为什么 storage.set / storage.get 无法在 Chrome 中获取 set/get 数据?
Why storage.set / storage.get fails to set/get data in Chrome?
我开始学习调试 Chrome 中的扩展。我在 Chrome 中遇到了这个问题,但在 Firefox webextensions 中没有问题。问题是在 Chrome 中我无法获取已保存到存储中的数据。
我在后台脚本中有这个功能。我添加断点以符合 storage.set 。我重新启动插件,我可以访问 MyNamespace.data 中的所有数据...插件打印安装成功的通知,但是当我尝试获取数据时日志显示空对象。
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": ""
}
}
}
我已经添加了这个监听器,以便在保存存储数据时触发:
chrome.storage.onChanged.addListener(function(changes, namespace) {
for (key in changes) {
var storageChange = changes[key];
console.log('Storage key "%s" in namespace "%s" changed. ' +
'Old value was "%s", new value is "%s".',
key,
namespace,
storageChange.oldValue,
storageChange.newValue);
}
});
我得到了这个日志:
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(),白色字母(深色主题),但它不像 link 那样我可以在其中单击并查看对象。这表明数据已加载。但是为什么我看不到Object中的数据呢?
- 我无法进入回调,所以我无法观看数据
- 我无法在控制台中看到对象 {} 中的内容....它看起来是空的,但在侦听器中我得到消息存储已更改。
- result in storage.get 回调结果为空...不包含任何属性
我已经用这段代码检查过了:
for(结果中的 var 键)
如果(hasOwnProperty.call(结果,关键))
console.log("key:" + 键);
在 Chrome 中没有列出任何属性,在 Firefox 工具箱/控制台中列出了它们。
据我所知,storage.set 回调应该在保存更改完成并且 db 文件关闭以进行写入后触发。所以我希望从 storage.set
内部触发的 storage.get 不应该有任何问题
有人可以澄清这里发生了什么吗?为什么在 Firefox 中可以,但在 Chrome 中不行?如何解决问题?
chrome.storage.local.get( {initiated: undefined, easy:undefined},
不会得到任何结果,因为使用参数对象的中间字符串化仅检索定义的键 (1, 2):JSON.stringify({a:undefined,b:undefined})
生成 '{}'
- 它是空的。
当您不提供默认值时,请改用键名数组:
chrome.storage.local.get( ['initiated', 'easy'],
P.S。在 console.log
format string 中使用 %o
或 %O
来显示对象的实际内容。
我开始学习调试 Chrome 中的扩展。我在 Chrome 中遇到了这个问题,但在 Firefox webextensions 中没有问题。问题是在 Chrome 中我无法获取已保存到存储中的数据。
我在后台脚本中有这个功能。我添加断点以符合 storage.set 。我重新启动插件,我可以访问 MyNamespace.data 中的所有数据...插件打印安装成功的通知,但是当我尝试获取数据时日志显示空对象。
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": ""
}
}
}
我已经添加了这个监听器,以便在保存存储数据时触发:
chrome.storage.onChanged.addListener(function(changes, namespace) {
for (key in changes) {
var storageChange = changes[key];
console.log('Storage key "%s" in namespace "%s" changed. ' +
'Old value was "%s", new value is "%s".',
key,
namespace,
storageChange.oldValue,
storageChange.newValue);
}
});
我得到了这个日志:
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(),白色字母(深色主题),但它不像 link 那样我可以在其中单击并查看对象。这表明数据已加载。但是为什么我看不到Object中的数据呢?
- 我无法进入回调,所以我无法观看数据
- 我无法在控制台中看到对象 {} 中的内容....它看起来是空的,但在侦听器中我得到消息存储已更改。
- result in storage.get 回调结果为空...不包含任何属性
我已经用这段代码检查过了: for(结果中的 var 键) 如果(hasOwnProperty.call(结果,关键)) console.log("key:" + 键);
在 Chrome 中没有列出任何属性,在 Firefox 工具箱/控制台中列出了它们。
据我所知,storage.set 回调应该在保存更改完成并且 db 文件关闭以进行写入后触发。所以我希望从 storage.set
内部触发的 storage.get 不应该有任何问题有人可以澄清这里发生了什么吗?为什么在 Firefox 中可以,但在 Chrome 中不行?如何解决问题?
chrome.storage.local.get( {initiated: undefined, easy:undefined},
不会得到任何结果,因为使用参数对象的中间字符串化仅检索定义的键 (1, 2):JSON.stringify({a:undefined,b:undefined})
生成 '{}'
- 它是空的。
当您不提供默认值时,请改用键名数组:
chrome.storage.local.get( ['initiated', 'easy'],
P.S。在 console.log
format string 中使用 %o
或 %O
来显示对象的实际内容。